欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC 导出Word报表

程序员文章站 2022-07-18 15:22:54
最近要做mvc导出word报表功能。查了查资料发现一个好用的插件就是aspose.word。这个插件也很有名气,也很好用。 1.首先就是引用该插件 2.填充word...

最近要做mvc导出word报表功能。查了查资料发现一个好用的插件就是aspose.word。这个插件也很有名气,也很好用。

1.首先就是引用该插件

ASP.NET MVC 导出Word报表

2.填充word模版

ASP.NET MVC 导出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);
    }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!