ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)
网上提供了很多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的下载地址是http://npoi.codeplex.com/,目前最新版本是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,下载地址http://epplus.codeplex.com/。
//引入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();
ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)的更多相关文章
- Asp.net导出Excel续章(自定义合并单元格,非Office组件)
结合上次写的导出Excel方法,这次上头要求我将列头进行一下合并 以前的效果: 改进后的效果: 在上篇文章中写到了Excel的导出方法,这次为了避免在生产环境中使用Office组件,服务器各种权限配置 ...
- ASP.NETCore -----导出Excel文件并下载
本事例分为nopi(安装DotNetCore.NPOI)下载和EPPlus(EPPlus.Core.dll)下载,其中npoi下载演示的是根据执行的模板进行数据下载 npoi帮助类NpoiExcelU ...
- asp.net导出excel示例代码
asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> ); ; ...
- [转] Asp.Net 导出 Excel 数据的9种方案
湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...
- ASP.NET导出EXCEL类
最新ASP.NET导出EXCEL类 说明:可以导出ASP.NET页面和DATAGRID(WebControl)数据,可以导出表单头 using System;using System.Data;usi ...
- asp.net导出excel并弹出保存提示框
asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1 转:78 | 分享 腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报 ...
- .NET导出Excel之NPOI
前段时间研究过微软的Excel导出.table输出Excel,而它们也存在一些弊端: 1.对于微软的Excel导出存在一些弊端,如:需要安装Office软件.速度问题: 2.table输出Excel在 ...
- asp.net 导出Excel
分享一个asp.net 导出假Excel代码.优点,不用借助于任何插件比如(NPOI),复制代码,修改grid.DataSource直接导出. 先看导出后的效果图 System.Web.UI.WebC ...
- ASP.NET导出Excel文件
第一种最常见,并且最简单的方式,直接把GridView导出,导出格式为文本表格形式. protected void btnSaveExcel_Click(object sender, EventArg ...
随机推荐
- JavaWeb学习总结(1-53)
本文转自孤傲苍狼 博客,JavaWeb学习总结 专题,一共53集,讲解简洁清晰,适合入门,链接和截图如下 http://www.cnblogs.com/xdp-gacl/category/574705 ...
- 创建maven项目出现的问题
右击项目无法显示maven图标,创建的pom.xml无法识别,并且创建maven项目时,一直显示Loading archetype list...
- todoing
1.如果系类没有数据需要返回么? 2.需要增加系列的门店打点么?
- 【Linux常用工具】1.1 diff命令的三种格式
diff是用来比较两个文本文件的差异的工具,它有三种格式,下面用实例介绍一下: 准备三个测试文件1.txt 2.txt 3.txt bixiaopeng@bixiaopengtekiMacBook-P ...
- ssl https服务 需要 php5.3以上
php 5.2 升级 5.3 http://wdlinux.cn/bbs/viewthread.php?tid=37512&highlight=5.3 默认的升级不支持 pdo 升级前编辑升级 ...
- The Arduino IDE(compiler)'s float bug
1.通常直接使用串口的print函数就ok Serial.println(DHT.humidity); //assuming DHT.humidity is float 2.但是习惯问题,还是偏好s ...
- 关于MySQL的各种总结
https://blog.atime.me/note/mysql-summary.html 总结使用MySQL过程中遇到的各种问题和一些有用的资源,配置等等.将之前的若干篇零散的文章汇总到一起,备忘. ...
- JAVA正则忽略大小写
java正则表达式: (?i)abc 表示abc都忽略大小写 a(?i)bc 表示bc忽略大小写 a((?i)b)c 表示只有b忽略大小写 也可以用Pattern.compile(re ...
- Java解析XML文档(简单实例)——dom解析xml
一.前言 用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object ...
- JS快速排序和去重
JS的快速排序和JS去重在面试的时候问的挺多的.下面是我对快速排序的理解,和快速排序,去重的代码. 1.什么是快速排序? 第一步: 快速排序就是去个中间值,把比中间值小的放在左边设为arrLeft,比 ...