Recently to do MVC Export Word Report feature. Check the data found a good plug-in is Aspose.word. This plugin is also very famous, but also very useful.
1. The first step is to refer to the plugin
2. Populate Word Templates
3. Background operation
Private list<double> Quaterairpm10avgvolreport (String stns, datetime start, DateTime end, Aspose.Words.DocumentBuilder Builder, out DataTable DT, out list<double> widthlist,string ismax) {dt = Quat Erpm10avgvol (Stns, start, End,ismax); Widthlist = new list<double> (); double[] colwidth = new double[] {50, 118, 117, 50, 118, 117}; string[] colname = new string[] {"Sort", "City", start. Year + "years" + start. Month + "~" +end. month+ "Month concentration (μg/m3)", "Sort", "City", "more" + start. AddYears (-1). Year + "YoY increase"}; Builder. MoveToBookmark ("Table3"); Aspose.Words.Tables.Table Table = Builder. Starttable ();//Start Drawing table Builder. InsertCell (); Builder. CellFormat.Borders.LineStyle = Linestyle.single; Builder. CellFormat.Borders.Color = System.Drawing.Color.Black; Builder. Cellformat.verticalmerge = Cellmerge.first; Builder. Cellformat.width = 285; Builder. ParagraphFormat.Alignment = Paragraphalignment.center; Builder. Cellformat.verticAlalignment = Aspose.Words.Tables.CellVerticalAlignment.Center; Builder. Write ("Sort by average concentration"); Builder. InsertCell (); Builder. CellFormat.Borders.LineStyle = Linestyle.single; Builder. CellFormat.Borders.Color = System.Drawing.Color.Black; Builder. ParagraphFormat.Alignment = Paragraphalignment.center; Builder. Cellformat.verticalalignment = Aspose.Words.Tables.CellVerticalAlignment.Center; Builder. Cellformat.verticalmerge = Cellmerge.none; Builder. Cellformat.width = 285; Builder. Write ("Press" + start.) AddYears (-1). Year + "annual increase in the same period"); Builder. Endrow (); Asposecreatecell (builder, colwidth[0], colname[0]); Asposecreatecell (builder, colwidth[1], colname[1]); Asposecreatecell (builder, colwidth[2], colname[2]); Asposecreatecell (builder, colwidth[3], colname[3]); Asposecreatecell (builder, colwidth[4], colname[4]); Asposecreatecell (builder, colwidth[5], colname[5]); Builder. Endrow (); Start adding value for (var i = 0; I < Dt. Rows.Count; i++) {if (dt). rows[i]["CityName"] = = "12 Cities to be evaluated" | | Dt. rows[i]["CityName"] = = "province") {Builder. InsertCell (); builder. CellFormat.Borders.LineStyle = Linestyle.single; Builder. CellFormat.Borders.Color = System.Drawing.Color.Black; Builder. Cellformat.verticalmerge = Cellmerge.first; Builder. Cellformat.width = 168;builder. ParagraphFormat.Alignment = Paragraphalignment.center; Builder. Write (dt. rows[i]["CityName"]. ToString ()); Builder. InsertCell (); builder. CellFormat.Borders.LineStyle = Linestyle.single; Builder. CellFormat.Borders.Color = System.drawing.color.black;builder. ParagraphFormat.Alignment = Paragraphalignment.center; Builder. Cellformat.verticalmerge = Cellmerge.none; Builder. Cellformat.width = 117; Builder. Write (dt. rows[i]["Pm10ati"]. ToString ()); Builder. InsertCell (); builder. CellFormat.Borders.LineStyle = Linestyle.single; Builder. Cellformat.borDERs. Color = System.drawing.color.black;builder. ParagraphFormat.Alignment = Paragraphalignment.center; Builder. Cellformat.verticalmerge = Cellmerge.none; Builder. Cellformat.width = 168; Builder. Write (dt. rows[i]["Qncityname"]. ToString ()); Builder. InsertCell (); builder. CellFormat.Borders.LineStyle = Linestyle.single; Builder. CellFormat.Borders.Color = System.drawing.color.black;builder. ParagraphFormat.Alignment = Paragraphalignment.center; Builder. Cellformat.verticalmerge = Cellmerge.none; Builder. Cellformat.width = 117; Builder. Write (dt. rows[i]["TQBH"]. ToString () + "%"); } else {Asposecreatecell (builder, colwidth[0), dt. rows[i]["Sort"]. ToString ()); Asposecreatecell (builder, colwidth[1], dt. rows[i]["CityName"]. ToString ()); Asposecreatecell (builder, colwidth[2], dt. rows[i]["Pm10ati"]. ToString ()); Asposecreatecell (builder, colwidth[3], dt. rows[i]["Qnsort"]. TostriNg ()); Asposecreatecell (builder, colwidth[4], dt. rows[i]["Qncityname"]. ToString ()); Asposecreatecell (builder, colwidth[5], dt. rows[i]["TQBH"]. ToString () + "%"); } builder. Endrow (); } builder. Endtable (); return widthlist; }
There are several places to pay attention to builder. Cellformat.verticalmerge = Cellmerge.none; Cellmerge are enumeration types, often used to draw complex tables, or to merge cells. There are first and previous. First we need to get the DataTable data, and finally manipulate the data.
4. Output document
Public Jsonresult Quaterresponse () {bool result; String quarter = request["Quarter"]. ToString (); String stns = request["Stns"]. ToString (); String Ismax = request["Ismax"]. ToString (); DateTime startTime = Convert.todatetime (request["StartDate"]); DateTime endTime = Convert.todatetime (request["EndDate"]); String tmppath = Server.MapPath ("~/document/model/quaterreport.docx"); String path = Server.MapPath ("~/document/export/quaterreport.doc"); Aspose.Words.Document doc = new Document (Tmppath); Aspose.Words.DocumentBuilder builder = new Documentbuilder (DOC); Doc. range.bookmarks["title"]. Text = starttime.year+ "Year" +quarter+ "review of Environmental air quality monitoring in Hubei province"; Doc. range.bookmarks["Title1"]. Text = "Table 1" +quarter+ "Air quality rating"; Doc. range.bookmarks["Title2"]. Text = "Table 2" +quarter+ "good days compliance rate table"; Doc. range.bookmarks["Title3"]. Text = "Table 3" +quarter+ "Air respirable particulate matter (PM10) average concentration of the table"; Doc. range.bookmarks["Title4"]. Text = "Table 4" +quarter+ "Air respirable particulate matter (PM2.5) average concentration of the table"; Doc. range.bookmarks["Title5"]. Text = "Table 5" +quarter+ "ambient air gaseous pollutant average concentration of the table"; Doc. range.bookmarks["Title6"]. Text = "Table 6" +quarter+ "Environmental air quality Comprehensive index situation table"; DataTable DT; List<double> widthlist; try {Doc. range.bookmarks["Table1"]. Text = ""; Clear Mark Quaterairperencetreport (Stns, StartTime, EndTime, builder, out DT, out Widthlist,ismax); Doc. range.bookmarks["Table2"]. Text = ""; Quaterairyldblreport (Stns, StartTime, EndTime, Builder,quarter, out DT, out Widthlist,ismax); Doc. range.bookmarks["Table3"]. Text = ""; Quaterairpm10avgvolreport (Stns, StartTime, EndTime, builder, out DT, out Widthlist,ismax); Doc. range.bookmarks["Table4"]. Text = ""; Quaterairpm25avgvolreport (Stns, StartTime, EndTime, builder, out DT, out Widthlist,ismax); Doc. range.bookmarks["Table5"]. Text = ""; Quaterotheravgvolreport (Stns, StartTime, EndTime, builder, out DT, out Widthlist,ismax); Doc. range.bookmarks["Table6"]. Text = ""; Quaterzhindexreport (Stns, StartTime, EndTime, builder, out DT, out Widthlist,ismax); Doc. Save (path, ASPOSE.WORDS.SAVEFORMAT.DOC); System.Diagnostics.Process.Start (path)///Open Document//Return View ("Quaterreport"); result = true; } catch (Exception) {result = false; } return Json (result); }