前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP.NET MVC5下载数据到Excel文件

ASP.NET MVC5下载数据到Excel文件

作者头像
雪飞鸿
发布2018-09-05 10:34:31
7010
发布2018-09-05 10:34:31
举报
文章被收录于专栏:me的随笔me的随笔

项目中的一个功能是将数据导入到Excel文件中,这里使用NPOI操作Excel,代码如下:

代码语言:javascript
复制
public class Excel : IDataTransfer
{
    public Stream Export(string[] titles, List<string>[] dataSource)
    {
        return ExportData(titles, dataSource);
    }

    protected virtual Stream ExportData(string[] titles, List<string>[] dataSource)
    {
        if (dataSource == null)
        {
            throw new ArgumentNullException();
        }
        HSSFWorkbook book = new HSSFWorkbook();
        ISheet sheet = book.CreateSheet("sheet1");
        int rowCount = dataSource.Length;
        int cellCount = dataSource[0].Count;

        var titleRow = sheet.CreateRow(0);
        #region header style
            // 该行的高度
            titleRow.HeightInPoints = 18;
            // 列的样式
            ICellStyle headStyle = book.CreateCellStyle();
            // 单元格内容居中显示
            headStyle.Alignment = HorizontalAlignment.Center;
            // 字体样式
            IFont font = book.CreateFont();
            // 字体大小
            font.FontHeightInPoints = 12;
            // 粗体
            font.Boldweight = 1200;
            // 字体颜色
            font.Color = NPOI.HSSF.Util.HSSFColor.Green.Index;
            headStyle.SetFont(font);
            // 边框
            headStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            headStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            headStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            headStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            #endregion

        for (int i = 0; i < titles.Length; i++)
        {
            ICell cell = titleRow.CreateCell(i);
            cell.SetCellValue(titles[i]);
            cell.CellStyle = headStyle;
        }

        for (int i = 0; i < rowCount; i++)
        {
            IRow row = sheet.CreateRow(i + 1);
            for (int j = 0; j < cellCount; j++)
            {
                row.CreateCell(j).SetCellValue(dataSource[i][j]);
            }
        }

        Stream stream = new MemoryStream();
        book.Write(stream);
        book.Close();
        stream.Position = 0;
        return stream;
    }
}

Contorller中的代码:

代码语言:javascript
复制
Excel excel = new Excel();
Stream dataStream = excel.Export(titles.ToArray(), data);
return new FileStreamResult(dataStream, "application/ms-excel") { FileDownloadName = "exportInfo.xlsx" };

整个功能的实现并没有太大难度,这里有一点需要注意就是Excel类中的protected virtual Stream ExportData(string[] titles, List<string>[] dataSource)方法,这个方法返回一个流,流中包含要导出的数据。方法的倒数第二行:stream.Position = 0;,这里需要特别注意,将数据写入流中之后,流的位置在最末端,我们要将流的位置重置到起始位置,否则无法读取流中的数据,也就无法导出流中的数据了。

参考文章:

asp.net MVC4.0 将数据 导出 excel 表格 MemoryStream类

版权声明

本文为作者原创,版权归作者雪飞鸿所有。 转载必须保留文章的完整性,且在页面明显位置处标明原文链接

如有问题, 请发送邮件和作者联系。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考文章:
    • 版权声明
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档