博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NPOI+SharpZipLib实现压缩多个Excel下载
阅读量:5810 次
发布时间:2019-06-18

本文共 4158 字,大约阅读时间需要 13 分钟。

导出excel是再常见不过的功能,其中NPOI是我一直使用的导出excel的框架。你可以了解NPOI,最近有这样一个需求,当我想一次性导出多个excel文件时。我想在内存里面压缩后输出到浏览器进行下载。由此接触到了另外一个框架SharpZipLib,了解。demo的代码贴在这里,以备后面查阅。

using System; using System.IO; using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.SS.UserModel; using ICSharpCode.SharpZipLib.Zip; namespace NpoiAndZip {
public partial class ExportDownloadExcel : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
} protected void btnOK_Click(object sender, EventArgs e) {
string filename = "test.zip"; Response.ContentType = "application/zip"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename)); Response.Clear(); InitializeWorkbook(); GenerateData(); MemoryStream ms1 = new MemoryStream(); MemoryStream ms2=new MemoryStream(); MemoryStream ms3=new MemoryStream(); ms1 = WriteToStream(hssfworkbook); ms2 = WriteToStream(hssfworkbook2); ms3 = WriteToStream(hssfworkbook3); MemoryStream st = new MemoryStream(); using (ZipFile zip = ZipFile.Create(st)) {
zip.BeginUpdate(); StreamDataSource d1=new StreamDataSource(ms1); StreamDataSource d2 = new StreamDataSource(ms2); StreamDataSource d3 = new StreamDataSource(ms3); //添加文件 zip.Add(d1, "test1.xls"); zip.Add(d2, "test2.xls"); zip.Add(d3, "test3.xls"); zip.CommitUpdate(); } Response.BinaryWrite(st.GetBuffer()); Response.End(); } HSSFWorkbook hssfworkbook; HSSFWorkbook hssfworkbook2; HSSFWorkbook hssfworkbook3; MemoryStream WriteToStream(HSSFWorkbook workbook) {
//Write the stream data of workbook to the root directory MemoryStream file = new MemoryStream(); workbook.Write(file); return file; } void GenerateData() {
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); ISheet sheet2 = hssfworkbook2.CreateSheet("Sheet1"); ISheet sheet3 = hssfworkbook3.CreateSheet("Sheet1"); sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample"); sheet2.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample"); sheet3.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample"); int x = 1; for (int i = 1; i <= 15; i++) {
IRow row = sheet1.CreateRow(i); for (int j = 0; j < 15; j++) {
row.CreateCell(j).SetCellValue(x++); } } for (int i = 1; i <= 15; i++) {
IRow row = sheet2.CreateRow(i); for (int j = 0; j < 15; j++) {
row.CreateCell(j).SetCellValue(x++); } } for (int i = 1; i <= 15; i++) {
IRow row = sheet3.CreateRow(i); for (int j = 0; j < 15; j++) {
row.CreateCell(j).SetCellValue(x++); } } } void InitializeWorkbook() {
hssfworkbook = new HSSFWorkbook(); hssfworkbook2 = new HSSFWorkbook(); hssfworkbook3 = new HSSFWorkbook(); ////create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; ////create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; ////create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi2 = PropertySetFactory.CreateDocumentSummaryInformation(); dsi2.Company = "NPOI Team"; hssfworkbook2.DocumentSummaryInformation = dsi2; ////create a entry of SummaryInformation SummaryInformation si2 = PropertySetFactory.CreateSummaryInformation(); si2.Subject = "NPOI SDK Example"; hssfworkbook2.SummaryInformation = si2; ////create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi3 = PropertySetFactory.CreateDocumentSummaryInformation(); dsi3.Company = "NPOI Team"; hssfworkbook3.DocumentSummaryInformation = dsi3; ////create a entry of SummaryInformation SummaryInformation si3 = PropertySetFactory.CreateSummaryInformation(); si3.Subject = "NPOI SDK Example"; hssfworkbook3.SummaryInformation = si3; } } //只有实现IStaticDataSource接口才能实现流操作 class StreamDataSource : IStaticDataSource {
public byte[] bytes { get; set; } public StreamDataSource(MemoryStream ms) {
bytes = ms.GetBuffer(); } public Stream GetSource() {
Stream s = new MemoryStream(bytes); return s; } } }

转载地址:http://qbnbx.baihongyu.com/

你可能感兴趣的文章
AIX 配置vncserver
查看>>
windows下Python 3.x图形图像处理库PIL的安装
查看>>
【IL】IL生成exe的方法
查看>>
network
查看>>
SettingsNotePad++
查看>>
centos7安装cacti-1.0
查看>>
3个概念,入门 Vue 组件开发
查看>>
没有JS的前端:体积更小、速度更快!
查看>>
数据指标/表现度量系统(Performance Measurement System)综述
查看>>
GitHub宣布推出Electron 1.0和Devtron,并将提供无限制的私有代码库
查看>>
Angular2, NativeScript 和 React Native比较[翻译]
查看>>
论模式在领域驱动设计中的重要性
查看>>
国内首例:飞步无人卡车携手中国邮政、德邦投入日常运营
查看>>
微软将停止对 IE 8、9和10的支持
查看>>
微服务架构会和分布式单体架构高度重合吗
查看>>
如何测试ASP.NET Core Web API
查看>>
《The Age of Surge》作者访谈
查看>>
测试人员的GitHub
查看>>
Spring Web Services 3.0.4.RELEASE和2.4.3.RELEASE发布
查看>>
有关GitHub仓库分支的几个问题
查看>>