http://www.cnblogs.com/qingyuan/archive/2012/11/08/2760034.html
http://www.cnblogs.com/gaoshuai/archive/2010/06/08/1753695.html
//内存流,行列
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; }
//调用输出头设置
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";
全面样式帮助:
http://tonyqus.sinaapp.com/tutorial
http://www.cnblogs.com/knowledgesea/archive/2012/11/16/2772547.html