ASP. net mvc exports Word reports, asp. netmvc

Source: Internet
Author: User

ASP. net mvc exports Word reports, asp. netmvc

Recently, I want to use MVC to export Word reports. After checking the information, we found that a useful plug-in is Aspose. Word. This plug-in is also very famous and useful.

1. reference the plug-in first

2. Fill in the Word template

3. Background operations

Private List <double> QuaterAirPM10AvgVolReport (string stns, DateTime start, DateTime end, Aspose. words. documentBuilder builder, out DataTable dt, out List <double> widthList, string isMax) {dt = QuaterPM10AvgVol (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 + "Year" + st Art. Month + "~ "+ End. month + "monthly concentration (μg/m3)", "sort", "city", "Compare" + start. addYears (-1 ). year + "Year-over-Year growth"}; builder. moveToBookmark ("table3"); Aspose. words. tables. table table = builder. startTable (); // start to draw 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 + "Year-on-Year growth sort"); 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 to add the value for (var I = 0; I <dt. rows. count; I ++) {if (dt. rows [I] ["CityName"] = "12 cities" | 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 ;}

Here are a few notes: builder. CellFormat. VerticalMerge = CellMerge. None; CellMerge is an enumeration type and is often used to draw complex tables or merge cells. There are also First and Previous. You must first obtain the DataTable data and then perform operations on 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 + "Overview of environmental air quality monitoring in Hubei Province"; doc. range. bookmarks ["title1"]. text = "table 1" + quarter + "Air Quality Grade"; doc. range. bookmarks ["title2"]. text = "Table 2" + quarter + "Excellent days compliance rate table"; doc. range. bookmarks ["title3"]. text = "Table 3" + quarter + "table of average concentrations of air inhaled particulate matter (PM10)"; doc. range. bookmarks ["title4"]. text = "Table 4" + quarter + "table of average concentrations of air inhaled particulate matter (PM2.5)"; doc. range. bookmarks ["title5"]. text = "Table 5" + quarter + "table of average concentrations of air gaseous pollutants"; doc. range. bookmarks ["title6"]. text = "Table 6" + quarter + "Comprehensive Environmental Air Quality Index Table"; DataTable dt; List <double> widthList; try {doc. range. bookmarks ["table1"]. text = ""; // clear the 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 the document // return View ("QuaterReport"); result = true;} catch (Exception) {result = false ;} return Json (result );}

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.