网上提供了很多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、sexforeach(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、sexfor (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"; //也可以设置成downloadResponse.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();