NPOI+SharpZipLib实现压缩多个Excel下载

news/2024/7/8 3:13:41 标签: ui

导出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;
}
}
}
复制代码
本文转自Rt-张雪飞博客园博客,原文链接http://www.cnblogs.com/mszhangxuefei/archive/2012/01/10/worknotes_4.html如需转载请自行联系原作者

张雪飞

http://www.niftyadmin.cn/n/1664056.html

相关文章

C#设计模式之十六观察者模式(Observer Pattern)【行为型】

一、引言 今天是2017年11月份的最后一天&#xff0c;也就是2017年11月30日&#xff0c;利用今天再写一个模式&#xff0c;争取下个月&#xff08;也就是12月份&#xff09;把所有的模式写完&#xff0c;2018年&#xff0c;新的一年写一些新的东西。今天我们开始讲“行为型”设…

java.lang.NoClassDefFoundError: org/apache/ibatis/mapping/DatabaseIdProvider

我用的方案是:mavenstruts2springmybatis 出现上述错误的原因是&#xff1a; <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.0.1</version> …

常用SQL语言概述(DDL、DML、DQL)

众所周知&#xff0c;SQL&#xff08;Structure Query Language&#xff09;是数据库的核心语言&#xff0c;近段时间学习数据库&#xff0c;部分基础概念有点模棱两可&#xff0c;今天索性把这几个常用的SQL概念简单记录下以作区分。分类&#xff1a;DDL&#xff1a;数据定义语…

Linux 6.5增加对高通开源GPU Adreno 690的支持

即将推出的Linux 6.5内核将把对高通Adreno 690 GPU的支持添加到开源的MSM内核图形/显示驱动程序中。A690主要用于骁龙8cx第三代&#xff08;SC8280XP&#xff09;平台&#xff0c;而联想ThinkPad X13s笔记本电脑和其他硬件也采用了该平台。 新的支持将包含近200行代码&#xf…

Clipboard.js : 移动端浏览器实现网页内容复制

最近在项目中遇到这样一个需求&#xff1a; 点击按钮&#xff0c;将指定的URL复制到剪贴板&#xff0c;然后用户可以粘贴到浏览器(Safari为主)自行打开。方案一&#xff1a;zeroClipboard.js github地址&#xff1a;https://github.com/zeroclipboard/zeroclipboard 复制原理&a…

NIO学习一、NIO简介

最近在学习NIO,根据学习总结了一下&#xff0c;如果有不对的地方&#xff0c;请大佬指出。 一、NIO的简介 NIO&#xff0c;就是new io&#xff0c;从jdk 1.4开始引入的新的api&#xff0c;它跟IO的作用相同。它与传统的IO相比&#xff0c;有如下特性&#xff1a;1&#xff09;…

重构——44移除参数(Remove Parameter)

移除参数&#xff08;Remove Parameter&#xff09; 函数本体不再需要某个函数&#xff1b;将该参数去除 一、动机 不去掉多余的参数&#xff0c;就让用户多费一份心 二、做法 1、检查函数签名是否被超类或者子类实现&#xff0c;如果是&#xff0c;则需要针对每份实现分别进行…

GNU make manual 翻译(四)

继续翻译复制代码1.2 Problems and Bugs If you have problems with GNU make or think youve found a bug, please report it to the developers; we cannot promise to do anything but we might well want to fix it. Before reporting a bug, make sure yo…