最近要做MVC导出Word报表功能。查了查资料发现一个好用的插件就是Aspose.Word。这个插件也很有名气,也很好用。
1.首先就是引用该插件
2.填充Word模版
3.后台操作
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[] { "排序", "城市", start.Year + "年" + start.Month + "~"+end.Month+"月浓度(μg/m3)", "排序", "城市", "较" + start.AddYears(-1).Year + "年同期增幅" }; builder.MoveToBookmark("table3"); Aspose.Words.Tables.Table table = builder.StartTable();//开始画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("按平均浓度排序"); 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("按" + start.AddYears(-1).Year + "年同期增幅排序"); 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(); //开始添加值 for (var i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["CityName"] == "12个考核地市" || dt.Rows[i]["CityName"] == "全省") { 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; }
其中有几个注意的地方 builder.CellFormat.VerticalMerge = CellMerge.None;CellMerge是枚举类型,经常用到画复杂的表格,或者是合并单元格。还有First和Previous。先要得到DataTable数据,最后对数据进行操作就行了。
4.输出文档
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+"年"+quarter+"湖北省环境空气质量监测情况综述"; doc.Range.Bookmarks["title1"].Text = "表1 "+quarter+"空气质量等级"; doc.Range.Bookmarks["title2"].Text = "表2" +quarter+"优良天数达标率情况表"; doc.Range.Bookmarks["title3"].Text = "表3 "+quarter+"空气可吸入颗粒物(PM10)平均浓度情况表"; doc.Range.Bookmarks["title4"].Text = "表4 "+quarter+"空气可吸入颗粒物(PM2.5)平均浓度情况表"; doc.Range.Bookmarks["title5"].Text = "表5"+quarter+" 境空气气态污染物平均浓度情况表"; doc.Range.Bookmarks["title6"].Text = "表6 "+quarter+"环境空气质量综合指数情况表"; DataTable dt; List<double> widthList; try { doc.Range.Bookmarks["table1"].Text = ""; // 清掉标示 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);//打开文档 // return View("QuaterReport"); result = true; } catch (Exception) { result = false; } return Json(result); }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持小牛知识库!
本文向大家介绍ASP.NET导出word实例,包括了ASP.NET导出word实例的使用技巧和注意事项,需要的朋友参考一下 最近遇到一个题目就是如何在asp.net中将数据导出到word中,由于数据是动态的,所以需要在后台拼出想要的的格式,翻遍了网页找出了一个比较满意的代码,感谢那位高手。代码如下: 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持呐喊
我需要将一些HTML导出到Word中,发现了以下链接:https://www.codexworld.com/export-html-to-word-doc-docx-using-javascript 代码如下。它生成一个。doc文件(旧的Word格式),但我更喜欢。docx。文章说只要稍加修改就可以完成,但没有说明如何完成。有人知道怎么做这个改变吗?
有人给我发了一个janalyiler报告,但我没有它,我也不打算购买它。他们在理解这一点时遇到了一些困难。jProfiler是否允许用户以text/html/pdf/etc格式导出报告?
我试图在jrxml中创建表格。表格中的列数可能会很大,当我试图以PDF格式导出报告时,由于列数很大,数据变得不可读。有什么方法可以让我们以更大的页面尺寸(如A3)导出报告,或者在A4尺寸上增加滚动功能。注意:报告只需要导出为pdf格式,而不是CSV或任何其他格式。 谢谢,任何帮助感谢。
本文向大家介绍Java用freemarker导出word实用示例,包括了Java用freemarker导出word实用示例的使用技巧和注意事项,需要的朋友参考一下 最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子。 一、模板的制作 先用Word做一个模板,如下图: (注意,上面是有表格的,
本文向大家介绍ajax实现excel报表导出,包括了ajax实现excel报表导出的使用技巧和注意事项,需要的朋友参考一下 利用ajax实现excel报表导出【解决乱码问题】,供大家参考,具体内容如下 背景 项目中遇到一个场景,要导出一个excel报表。由于需要token验证,所以不能用a标签;由于页面复杂,所以不能使用表单提交。初步考虑前端使用ajax,后端返回流,定义指定的header。 第一