Get the App
SLTechnology News&Howtos  ›  Development  › 

How to export Word report with Aspose.Word by MVC

Shulou Source: shulou.com Published: 2022-06-03 13:25:50 09月20日 Update

Editor to share with you how MVC uses Aspose.Word to export Word reports, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. The first thing is to reference the plug-in

two。 Fill Word template

3. Background operation

Private List QuaterAirPM10AvgVolReport (string stns, DateTime start, DateTime end, Aspose.Words.DocumentBuilder builder, out DataTable dt, out List widthList,string isMax) {dt = QuaterPM10AvgVol (stns, start, end,isMax); widthList = new List (); double [] colWidth = new double [] {50,118,117,50,118,117} String [] colName = new string [] {"order", "city", start.Year + "year" + start.Month + "~" + end.Month+ "monthly concentration (μ g/m3)", "order", "city", "more" + start.AddYears (- 1). Year + "year 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 ("by" + start.AddYears (- 1). Year + "year-on-year increase"); 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; iTunes +) {if (dt.Rows [I] ["CityName"] = "12 assessment cities" | | dt.Rows [I] ["CityName"] = = "whole 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 th builder.Paragraph Format.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 a few things to note that builder.CellFormat.VerticalMerge = CellMerge.None;CellMerge is an enumerated type, which is often used to draw complex tables or merge cells. And First and Previous. First you need to get the DataTable data, and finally you can 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+ "Summary of Environmental Air quality Monitoring in Hubei Province"; doc.Range.Bookmarks ["title1"]. Text = "Table 1" + quarter+ "Air quality level" Doc.Range.Bookmarks ["title2"]. Text = "Table 2" + quarter+ "excellent days attainment rate Table"; doc.Range.Bookmarks ["title3"]. Text = "Table 3" + quarter+ "Air respirable particulate matter (PM10) average concentration Table"; doc.Range.Bookmarks ["title4"]. Text = "Table 4" + quarter+ "Air respirable particulate matter (PM2.5) average concentration Table" Doc.Range.Bookmarks ["title5"]. Text = "Table 5" + quarter+ "average concentration of ambient gaseous pollutants"; doc.Range.Bookmarks ["title6"]. Text = "Table 6" + quarter+ "Ambient Air quality Comprehensive Index"; DataTable dt; List widthList; try {doc.Range.Bookmarks ["table1"]. Text = "" / remove the label 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) } these are all the contents of the article "how MVC exports Word reports with Aspose.Word". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Situation air situation table concentration sorting air quality article quality report content respirable particulate matter city increase data document environment complex less province unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn macOS Shulou Technology Shulou Information