• C# MVC NPOI导出


    前台:

    <form id="fmexp" method="post" target="_blank">
    </form>

    function export() {
    var $end = $("#endate").val();
    $("#fmexp").form('submit', {
    url: '@Url.Action("ApplyExp")/' + $end,
    onSubmit: function () {
    },
    success: function (result) {
    $("#exportdate").linkbutton('disable');
    $end = "";
    }
    })
    }

    后台:

    public ContentResult ApplyExp(string id)   //这个值是路由配置里的值。
    {
    ApplylistBLL alb = new ApplylistBLL();
    DataTable dt = alb.EndData(id);
    dt.TableName = "培训汇总";
    if (dt != null && dt.Rows.Count > 0)
    {
    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
    NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);
    NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);

    // //标题头与列宽

    for (int i = 0; i < dt.Columns.Count; i++)
    {
    row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
    }
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
    for (int j = 0; j < dt.Columns.Count; j++)
    {
    row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
    }
    }

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
    book.Write(ms);
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
    Response.BinaryWrite(ms.ToArray());
    book = null;
    ms.Close();
    ms.Dispose();
    }
    }
    return Content("");
    }

    引用dll---------NPOI.dll,   NPOI.OOXML.dll,  NPOI.OpenXml4Net.dll ,   NPOI.OpenXmlFormats.dll

    已上传至文件。

  • 相关阅读:
    [YTU]_2417 C语言习题 字符串长度
    最小生成树学习笔记
    后缀数组学习笔记
    网络流的几个小优化
    面向对象
    Manacher(马拉车)学习笔记
    EXKMP学习笔记QAQ
    GDOI DAY1游记
    GDOI--DAY2 游记
    caioj:1348: [NOIP普及组2012]质因数分解 C++
  • 原文地址:https://www.cnblogs.com/w1-y2-q5/p/6879420.html
Copyright © 2020-2023  润新知