anyuan2002.com - vwin网

查找: 您的方位主页 > 电脑频道 > 编程代码 > 阅览资讯:经过ASP.Net mvc导出word报表的办法

经过ASP.Net mvc导出word报表的办法

2017-06-25 17:15:54 来历:www.anyuan2002.com 【

经过ASP.Net mvc导出word报表的办法

最近要做MVC导出Word报表功用。查了查资料发现一个好用的插件便是Aspose.Word。这个插件也很有名望,也很好用。

1.首要便是引证该插件

经过ASP.Net mvc导出word报表的办法

2.填充Word模版

经过ASP.Net mvc导出word报表的办法

3.后台操作

vwin娱乐场
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);
  }

以上便是本文的全部内容,期望本文的内容对咱们的学习或许作业能带来必定的协助,一起也期望多多支撑vwin!


本文地址:http://www.anyuan2002.com/bcdm/89149.html
Tags: 导出 ASP.NET MVC
修改:vwin网
推行内容
引荐阅览
抢手引荐
引荐文章
关于咱们 | 联络咱们 | 友情链接 | 网站地图 | Sitemap | App | 回来顶部