[源码分享]NPOI导出数据到EXCEl(输出文件,web输出下载)
来源:互联网 发布:h5登录界面模板带源码 编辑:程序博客网 时间:2024/06/14 04:20
//从datereader(也可以使用datatable) 读取数据添加到excel工作簿,然后转换到内存流
public static MemoryStream RenderToExcel(IDataReader reader){ MemoryStream ms = new MemoryStream(); using (reader) { using (IWorkbook workbook = new HSSFWorkbook()) { using (ISheet sheet = workbook.CreateSheet()) { IRow headerRow = sheet.CreateRow(0); int cellCount = reader.FieldCount; // handling header. for (int i = 0; i < cellCount; i++) { headerRow.CreateCell(i).SetCellValue(reader.GetName(i)); } // handling value. int rowIndex = 1; while (reader.Read()) { IRow dataRow = sheet.CreateRow(rowIndex); for (int i = 0; i < cellCount; i++) { dataRow.CreateCell(i).SetCellValue(reader[i].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; } } } return ms;}
//将内存流保存到文件
static void SaveToFile(MemoryStream ms, string fileName){ using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); data = null; }}
//将内存流输出为下载文件
static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName){ if (context.Request.Browser.Browser == "IE") fileName = HttpUtility.UrlEncode(fileName); context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName); context.Response.BinaryWrite(ms.ToArray());}
- [源码分享]NPOI导出数据到EXCEl(输出文件,web输出下载)
- NPOI导出数据到Excel
- NPOI导出Excel并下载到客户端
- [源码分享]NPOI读取Excel到 dataTable
- 使用NPOI导出数据到Excel
- 使用npoi.dll导出数据到excel
- NPOI 通用导出数据到Excel
- 把DataGrid数据输出到Excel文件
- C#输出DataTable数据到Excel文件
- npoi导出到EXCEL
- NPOI导出到Excel
- 【NPOI】导出Excel文件
- c# 导出excel 填充数据 输出到浏览器
- 怎么实现NPOI导出excel保存到服务器上,然后返回文件地址下载?
- 导出数据到Excel 2007 多个Sheet页(NPOI)(web版)
- NPOI操作EXCEL 将Table中数据导出到Excel
- .net导出excel 向客户端推送文件 下载文件 ClosedXML导出Excel NPOI导出Excel
- .NET使用NPOI将数据导出/导入到Excel文件介绍
- JS判断浏览器类型与版本
- Spider-定向抓取
- C语言简单的三个示例
- 总结一下java中的线程Thread
- CodeIgniter Utf8.php
- [源码分享]NPOI导出数据到EXCEl(输出文件,web输出下载)
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- Oracle PL/SQL语言基础ORACLE教程 教程作者:佚名 教程来源:不详 教程栏目:ORACLE教程
- Extjs与Arcgis javascript结合完成FindTask
- 驱动程序层次
- 关于altium designer 中如何按1:1生成PDF文档的解决方案
- 编写可维护的JavaScript
- x64安装Oracle 10.2.0.4无法启动EM dbconsole问题解决
- Socket客户端和服务端通讯 要求客户端发送数据后回显同样数据