http://www.cnblogs.com/qingyuan/archive/2012/11/08/2760034.htmlhtml
http://www.cnblogs.com/gaoshuai/archive/2010/06/08/1753695.htmlapp
//內存流,行列ide
public static MemoryStream RenderToExcel(DataTable table) { MemoryStream ms = new MemoryStream(); using (table) { using (IWorkbook workbook = new HSSFWorkbook()) { using (ISheet sheet = workbook.CreateSheet()) { IRow headerRow = sheet.CreateRow(0); // handling header. foreach (DataColumn column in table.Columns) headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value // handling value. int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; } } } return ms; }
//調用輸出頭設置spa
PersonDAL pdal = new PersonDAL(); DataTable dt = pdal.GetPersonDataTableAll(); MemoryStream ms = RenderToExcel(dt); string fileName = "pdb.xls"; HttpContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); HttpContext.Response.BinaryWrite(ms.ToArray()); HttpContext.Response.End(); ms.Close(); ms = null;
HttpContext.Response.ContentType = "application/vnd.ms-excel";excel
全面樣式幫助:code
http://tonyqus.sinaapp.com/tutorialhtm
http://www.cnblogs.com/knowledgesea/archive/2012/11/16/2772547.htmlblog