博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)
阅读量:7038 次
发布时间:2019-06-28

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

网上提供了很多Asp.net中操作Excel的方法,其中大部分是调用微软的Office组件,下面提供三个无须安装Office即可从Asp.net输出Excel的方法。

1 简单方法

//下面代码输出的Excel有三列(姓名、年龄、性别)

//列之间用\t隔开
StringWriter sw = new StringWriter();
sw.WriteLine("姓名\t年龄\t性别"); //Excel表格的列标题
sw.WriteLine("张三\t29\t男"); //行数据
sw.WriteLine("李四\t35\t男");
sw.WriteLine("王五\t20\t女");

/*如果从数据库返回的数据

DataTable dt; //假设dt已经有数据,数据格式name、age、sex
foreach(DataRow row in dt.Rows)
{
sw.WriteLine(string.Format("{0}\t{1}\t{2}", row["name"], row["age"], row["sex"]));
}
*/

//asp.net输出的Excel文件名

//如果文件名是中文的话,需要进行编码转换,否则浏览器看到的下载文件是乱码。
string fileName = HttpUtility.UrlEncode("Excel.xls");;

Response.Buffer = true;

Response.Clear();

//asp.net返回的数据类型,这里设置download。

//浏览器会把返回的数据当成文件下载来处理。
Response.ContentType = "application/download";

//设置返回数据的编码,如果不设置该项,Excel的中文是乱码。

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";");

Response.Write(sw); //把数据写入输出流。

Response.Flush();
Response.Close();

2  NPOI导出Excel 2003

NPOI是一个开源库,支持各种Excel操作,不过只能操作97~2003版的Excel,不兼容Excel 2007。

NPOI的下载地址是,目前最新版本是1.2.5。

//引入NPOI库

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.SS.UserModel;

//

HSSFWorkbook hssfworkbook = new HSSFWorkbook();

//Excel文件的摘要信息

DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "blog.csdn.net";
hssfworkbook.DocumentSummaryInformation = dsi;

SummaryInformation si = PropertySetFactory.CreateSummaryInformation();

si.Subject = "Export Excel";
hssfworkbook.SummaryInformation = si;

//下面代码输出的Excel有三列(姓名、年龄、性别)

ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");

IRow row0 = sheet1.CreateRow(0);

row0.CreateCell(0).SetCellValue("姓名");
row0.CreateCell(1).SetCellValue("年龄");
row0.CreateCell(2).SetCellValue("性别");

IRow row1 = sheet1.CreateRow(1);

row1.CreateCell(0).SetCellValue("张三");
row1.CreateCell(1).SetCellValue(29);
row1.CreateCell(2).SetCellValue("男");

IRow row2 = sheet1.CreateRow(2);

row2.CreateCell(0).SetCellValue("李四");
row2.CreateCell(1).SetCellValue(35);
row2.CreateCell(2).SetCellValue("男");

IRow row3 = sheet1.CreateRow(3);

row3.CreateCell(0).SetCellValue("王五");
row3.CreateCell(1).SetCellValue(20);
row3.CreateCell(2).SetCellValue("女");

/*如果从数据库获取的数据

DataTable dt = null; //假设dt已经有数据,数据格式name、age、sex

for (int i = 0; i < dt.Rows.Count; i++)

{
//DataTable中的行和Excel中的行对应
DataRow row = dt.Rows[i];
IRow excelRow = sheet1.CreateRow(i);
excelRow.CreateCell(0).SetCellValue(row["name"].ToString());
excelRow.CreateCell(1).SetCellValue(row["age"].ToString());
excelRow.CreateCell(2).SetCellValue(row["sex"].ToString());

//DataTable中的Column和Excel中的Cell对应

//也可以对DataTable中的列进行循环获取字段值
for (int j = 0; j < dt.Columns.Count; j++)
{
string value = dt.Rows[i][j].ToString();
excelRow.CreateCell(j).SetCellValue(value);
}
}
*/

MemoryStream ms = new MemoryStream();

hssfworkbook.Write(ms);

//asp.net输出的Excel文件名

//如果文件名是中文的话,需要进行编码转换,否则浏览器看到的下载文件是乱码。
string fileName = HttpUtility.UrlEncode("Excel.xls");

Response.ContentType = "application/vnd.ms-excel";

//Response.ContentType = "application/download"; //也可以设置成download
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));

Response.Buffer = true;

Response.Clear();
Response.BinaryWrite(ms.GetBuffer());
Response.End();

3 EPPlus库生成Excel 2007

EPPlus支持Excel 2007,下载地址。

//引入EPPlus的命名空间

//using OfficeOpenXml;

ExcelPackage excel = new ExcelPackage();

ExcelWorksheet sheet = excel.Workbook.Worksheets.Add("sheet1");
sheet.Cells["A1"].Value = "姓名";
sheet.Cells["B1"].Value = "年龄";
sheet.Cells["C1"].Value = "性别";

sheet.Cells["A2"].Value = "张三";

sheet.Cells["B2"].Value = 29;
sheet.Cells["C2"].Value = "男";

sheet.Cells["A3"].Value = "李四";

sheet.Cells["B3"].Value = 35;
sheet.Cells["C3"].Value = "男";

sheet.Cells["A4"].Value = "王五";

sheet.Cells["B4"].Value = 20;
sheet.Cells["C4"].Value = "女";

/*

//也可以用2维数组
//数组的索引从1开始
sheet.Cells[1, 1].Value = "姓名";
sheet.Cells[1, 2].Value = "年龄";
sheet.Cells[1, 3].Value = "性别";

sheet.Cells[2, 1].Value = "张三";

sheet.Cells[2, 2].Value = 29;
sheet.Cells[2, 3].Value = "男";

sheet.Cells[3, 1].Value = "李四";

sheet.Cells[3, 2].Value = 35;
sheet.Cells[3, 3].Value = "男";

sheet.Cells[4, 1].Value = "王五";

sheet.Cells[4, 2].Value = 20;
sheet.Cells[4, 3].Value = "女";
*/

MemoryStream ms = new MemoryStream();

excel.SaveAs(ms);

//asp.net输出的Excel文件名

//如果文件名是中文的话,需要进行编码转换,否则浏览器看到的下载文件是乱码。
string fileName = HttpUtility.UrlEncode("Excel.xlsx");

Response.ContentType = "application/vnd.ms-excel";

//Response.ContentType = "application/download"; //也可以设置成download
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));

Response.Buffer = true;

Response.Clear();
Response.BinaryWrite(ms.GetBuffer());
Response.End();

转载于:https://www.cnblogs.com/Andy-Blog/p/4748890.html

你可能感兴趣的文章
Vmware虚拟机快速使用桥接模式上网
查看>>
PHP写WebService
查看>>
microsoft office 2013 完全 卸载 工具 来自微软官方
查看>>
AlwaysOn业务IP和高可用IP分开使用(二)
查看>>
Kubernetes 配置管理 ConfigMap(十二)
查看>>
PHP - 获取音频长度
查看>>
关于使用ASP.NET和数据库的笔记
查看>>
为什么还是穷人:工作的态度
查看>>
在JFinal的Controller中接收json数据
查看>>
linux系统中对逻辑卷(lvm)的实现
查看>>
php代码上传到linux服务器无法正常显示
查看>>
一起学Shell之(二)输出以及其它
查看>>
ASP.NET的后台Long-Running任务
查看>>
为WP7添加动态Tile
查看>>
使用rrdtool自定义绘图监控Oracle数据库
查看>>
Linux下配置Squid基础教程
查看>>
.NET应用架构设计—面向查询服务的参数化查询设计(分解业务点,单独配置各自的数据查询契约)...
查看>>
Java中final和static关键字总结
查看>>
FileNet更改文件类型名称后,Document不能刷新ClassDescription的解决方案
查看>>
Lync与Exchange 2013 UM集成:Lync Server配置
查看>>