文章摘自:http://blog.csdn.net/accountwcx/article/details/8144970
網(wǎng)上提供了很多Asp.net中操作Excel的方法谚殊,其中大部分是調(diào)用微軟的Office組件料按,下面提供三個無須安裝Office即可從Asp.net輸出Excel的方法银萍。
1 簡單方法
//下面代碼輸出的Excel有三列(姓名拔疚、年齡肥隆、性別)
//列之間用\t隔開
StringWriter sw = new StringWriter();
sw.WriteLine("姓名\t年齡\t性別"); //Excel表格的列標題
sw.WriteLine("張三\t29\t男"); //行數(shù)據(jù)
sw.WriteLine("李四\t35\t男");
sw.WriteLine("王五\t20\t女");
/*如果從數(shù)據(jù)庫返回的數(shù)據(jù)
DataTable dt; //假設(shè)dt已經(jīng)有數(shù)據(jù),數(shù)據(jù)格式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文件名
//如果文件名是中文的話,需要進行編碼轉(zhuǎn)換句各,否則瀏覽器看到的下載文件是亂碼吸占。
string fileName = HttpUtility.UrlEncode("Excel.xls");;
Response.Buffer = true;
Response.Clear();
//asp.net返回的數(shù)據(jù)類型,這里設(shè)置download凿宾。
//瀏覽器會把返回的數(shù)據(jù)當(dāng)成文件下載來處理矾屯。
Response.ContentType = "application/download";
//設(shè)置返回數(shù)據(jù)的編碼,如果不設(shè)置該項初厚,Excel的中文是亂碼件蚕。
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";");
Response.Write(sw); //把數(shù)據(jù)寫入輸出流。
Response.Flush();
Response.Close();
2 NPOI導(dǎo)出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("女");
/*如果從數(shù)據(jù)庫獲取的數(shù)據(jù)
DataTable dt = null; //假設(shè)dt已經(jīng)有數(shù)據(jù)裳瘪,數(shù)據(jù)格式name、age罪针、sex
for (int i = 0; i < dt.Rows.Count; i++)
{
//DataTable中的行和Excel中的行對應(yīng)
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對應(yīng)
//也可以對DataTable中的列進行循環(huán)獲取字段值
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文件名
//如果文件名是中文的話彭羹,需要進行編碼轉(zhuǎn)換,否則瀏覽器看到的下載文件是亂碼站故。
string fileName = HttpUtility.UrlEncode("Excel.xls");
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/download"; //也可以設(shè)置成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維數(shù)組
//數(shù)組的索引從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文件名
//如果文件名是中文的話西篓,需要進行編碼轉(zhuǎn)換愈腾,否則瀏覽器看到的下載文件是亂碼。
string fileName = HttpUtility.UrlEncode("Excel.xlsx");
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/download"; //也可以設(shè)置成download
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
Response.Buffer = true;
Response.Clear();
Response.BinaryWrite(ms.GetBuffer());
Response.End();