標籤:
代碼準備:
一:實體準備
代碼如下:
/// <summary> /// 一個能添加到將要匯出到指定行的實體類型規範 /// data:{int StartColIndex ? 0, int ColSpan, object Value, bool Center} /// </summary> public interface IExcelModel { /// <summary> /// 開始列的索引(即使是有合併儲存格的情況,也得按未合併儲存格時算) /// </summary> int StartColIndex { get; set; } /// <summary> /// 這個值一共佔多少個儲存格 /// </summary> int ColSpan { get; set; } /// <summary> /// 值 /// </summary> object Value { get; set; } /// <summary> /// 是否置中 /// </summary> bool Center { get; set; } }
上面的介面代碼,是對要產生的Excel的儲存格的規範,Center:表示是否置中,Value:表示顯示的值,ColSpan:表示儲存格(列)的合并數,
StartColIndex:表示是第幾個儲存格,而且跟儲存格的合并沒有關係。
二:協助方法類(Helper)準備
/// <summary> /// 使用NPOI由IList<List<IExcelModel>> datas匯出Excel /// </summary> public static void ExportDataSetToExcel(string fileName, HSSFWorkbook book, ISheet sheet, IList<List<IExcelModel>> datas) { MemoryStream ms = ExportDataSetToExcel(book,sheet,datas) as MemoryStream; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8)); HttpContext.Current.Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.End(); ms.Close(); ms = null; }
上面方法中,IList<List<IExcelModel>>表示的是一個List<IExcelModel>的集合,而每一個List<IExcelModel>就是一些個儲存格的集合,也就是一行。IList<List<IExcelModel>>就是表示要產生的Excel表是由多行的資料群組成的。
fileName表示最後要儲存的檔案名稱,要帶尾碼名。
上面方法需要引用的方法代碼如下:
private static Stream ExportDataSetToExcel(HSSFWorkbook book, ISheet sheet, IList<List<IExcelModel>> datas) { // create memory stream MemoryStream ms = new MemoryStream(); // process excel file AddDatasToExcelSheet(book, sheet, datas); book.Write(ms); ms.Flush(); ms.Position = 0; book = null; return ms; }
上面方法需要引用的方法代碼如下:
/// <summary> /// 將指定資料構造到 Excel 中 /// </summary> /// <param name="book"></param> /// <param name="sheet"></param> /// <param name="datas"></param> public static void AddDatasToExcelSheet(HSSFWorkbook book, ISheet sheet, IList<List<IExcelModel>> datas) { for (int i = 0; i < datas.Count(); i++) { AddDatasToExcelRow(book, sheet, i, datas[i]); } } /// <summary> /// 添加一個資料集合到將要匯出的 Excel 的指定行。 /// </summary> public static void AddDatasToExcelRow(HSSFWorkbook book, ISheet sheet, int rowIndex, List<IExcelModel> datas) { IRow row = sheet.CreateRow(rowIndex); ICellStyle cs = book.CreateCellStyle(); foreach (dynamic data in datas) { ICell cell = row.CreateCell(data.StartColIndex); cell.SetCellValue(data.Value); if (data.ColSpan > 1) { CellRangeAddress cra = new CellRangeAddress(rowIndex, rowIndex, data.StartColIndex, data.StartColIndex + data.ColSpan - 1); sheet.AddMergedRegion(cra); } if (data.Center) { //ICellStyle cs = book.CreateCellStyle();//放在這裡會報錯:The maximum number of cell styles was exceeded. You can define up to 4000 styles cs.VerticalAlignment = VerticalAlignment.Center; cs.Alignment = HorizontalAlignment.Center; cell.CellStyle = cs; } } }
代碼實戰:
IList<List<IExcelModel>> excelList = new List<List<IExcelModel>>(); //第一行 List<IExcelModel> firstRow = new List<IExcelModel>(); //第一個儲存格 IExcelModel tempFRCell = new ExcelModel() { StartColIndex = 0, ColSpan = 1, Center = true, Value = "" }; //把第一個儲存格添加到第一行中 firstRow.Add(tempFRCell); //把第一個行添加到excelList excelList.Add(firstRow); //最後的 HSSFWorkbook book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet(); //book.Add(sheet); ExcelConstructHelper.ExportDataSetToExcel("樣本.xls", book, sheet, excelList);
這樣就可以實現網頁上直接下載一個Excel檔案了。
未解決的問題:
1:儲存格的縱向合并
2:儲存格及行的樣式
如何將這兩個都融合到上面提到的規範中去???
----------------------------------------------歡迎批評與交流!!!--------------------------
ASP.NET中的Excel操作(NPOI方式)