最近要做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[] { , , , , , };
string[] colName = new string[] { "排序", "城市", start.Year + "年" + start.Month + "~"+end.Month+"月浓度(μg/m3)", "排序", "城市", "较" + start.AddYears(-).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 = ;
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 = ; builder.Write("按" + start.AddYears(-).Year + "年同期增幅排序");
builder.EndRow();
AsposeCreateCell(builder, colWidth[], colName[]);
AsposeCreateCell(builder, colWidth[], colName[]);
AsposeCreateCell(builder, colWidth[], colName[]);
AsposeCreateCell(builder, colWidth[], colName[]);
AsposeCreateCell(builder, colWidth[], colName[]);
AsposeCreateCell(builder, colWidth[], colName[]);
builder.EndRow();
//开始添加值
for (var i = ; 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 = ;
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 = ;
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 = ;
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 = ;
builder.Write(dt.Rows[i]["tqbh"].ToString() + "%"); }
else
{
AsposeCreateCell(builder, colWidth[], dt.Rows[i]["Sort"].ToString());
AsposeCreateCell(builder, colWidth[], dt.Rows[i]["CityName"].ToString());
AsposeCreateCell(builder, colWidth[], dt.Rows[i]["PM10ATI"].ToString()); AsposeCreateCell(builder, colWidth[], dt.Rows[i]["qnSort"].ToString());
AsposeCreateCell(builder, colWidth[], dt.Rows[i]["qnCityName"].ToString());
AsposeCreateCell(builder, colWidth[], 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 MVC 导出Word报表的更多相关文章

  1. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  2. ASP.NET MVC 导出CSV文件

    ASP.NET MVC   导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...

  3. ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  4. 【吉光片羽】MVC 导出Word的两种方式

    1.直接将Html转成Word.MVC自带FileResult很好用.Html中我们也可以嵌入自己的样式. html: <div id="target"> <st ...

  5. asp.net mvc 导出表格

    适合使用的场合: .net 中从前台中的table导出成excel文件,兼容各种浏览器. 使用工具: org.in2bits.MyXls.dll 从前台获取表格的thead和表格的tbody,将其转化 ...

  6. Asp.net MVC 填充word并下载

    使用Aspose.word填充内容并下载(免费版Aspose) 填充固定模式的Word文档,需要先制作Word模板: Aspose.Words主要通过Words里域(Fields)来控制内容.     ...

  7. ASP.NET MVC导出excel npoi

    使用npoi组件 前端代码: @Html.ActionLink("导出Excel", "ExportWarehouseInOutDetailTable", ne ...

  8. asp.net MVC 导出查询结果到Excel

    首先在View视图中有一表单form,导出按钮<input class="btn export" type="button" value="导出 ...

  9. asp.net mvc导出execl_转载

    public FileResult ExportExcel() { var sbHtml = new StringBuilder(); sbHtml.Append("<table bo ...

随机推荐

  1. bitmap资源回收

    这个问题哎,困扰本宫一天! bitmap不完全解决method: http://blog.csdn.net/hahahacff/article/details/8540942 http://blog. ...

  2. 理解javascript:void(0);和href="#"

    我们经常可以看见这样的代码: <a href="javascript:void(0);">链接</a> void的意思是空的,无效的意思,但是在js中是表示 ...

  3. YII 1.0 隐藏单入口index.php 设置路由与伪静态

    隐藏 index.php 保证apache配置文件httpd.conf里的LoadModulerewrite_module modules/mod_rewrite.so开启(去掉#)将相对应目录的Al ...

  4. YII 1.0 增删改查

    查询 查询多条返回数据集合 //1.该方法是根据一个条件查询一个集合 $admin=Admin::model()->findAll($condition,$params); $admin=Adm ...

  5. QT第三天学习

    回顾: 布局: 绝对位置法: 手工布局:QLayout 可视化布局:设计器 VB MFC JAVA C# cocos QT信号和槽 ---------------------------------- ...

  6. jquery 组合键键盘事件

    jQuery处理键盘事件,比如小说网站中常见的按左右键来实现上一篇文章和下一篇文章,按ctrl+回车实现表单提交,google reader和有道阅读中的全快捷键操作... 本文讲述jQuery处理按 ...

  7. Android中的AutoCompleteTextView的使用

    最终的效果如下: main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  8. Zookeeper以Windows服务安装运行

    1.下载的Zookeeper是.cmd的批处理命令运行的,默认没有提供以windows服务的方式运行的方案 下载地址:http://zookeeper.apache.org/ 2.下载prunsrv ...

  9. python之FTP程序(支持多用户在线)

    转发注明出处:http://www.cnblogs.com/0zcl/p/6259128.html 一.需求 1. 用户加密认证 (完成)2. 允许同时多用户登录 (完成)3. 每个用户有自己的家目录 ...

  10. Word,Excel,PowerPoint协作实用功能

    Word,Excel,PowerPoint协作实用功能 纯手打,可能有错别字,使用的版本是office2013 转载请注明出处,谢谢 将Word表格复制到Excel中 点击Word表格左上角的按钮-- ...