/// <summary> /// 按照行集合对象,创建Execl文件流 /// </summary> /// <param name="list">行集合对象</param> /// <returns>Execl文件流</returns> public static MemoryStream RenderToExcel(List <E_Row> list) { MemoryStream memoryStream = new MemoryStream(); //创建工作薄 var workbook = new HSSFWorkbook(); //创建表 var sheet = workbook.CreateSheet(); //填充表数据 foreach (E_Row current in list) { IRow row = sheet.CreateRow(current.rowindex); foreach (E_Cell eCell in current.cells) { //判断是否存在序号合并的单元格 if (eCell.colspan > 1 || eCell.rowspan > 1) { int rowIndex = current.rowindex; //第一行 int num2 = current.rowindex + eCell.rowspan - 1; //最后一行 int cellIndex = eCell.cellindex; //第一个单元格 int num3 = eCell.cellindex + eCell.colspan - 1; //最后一个单元格 CellRangeAddress cellRangeAddress = new CellRangeAddress(rowIndex, num2, cellIndex, num3); sheet.AddMergedRegion(cellRangeAddress); } row.CreateCell(eCell.cellindex).SetCellValue(eCell.content); } } HtmlTableExport.AutoSizeColumns(sheet); workbook.Write(memoryStream); memoryStream.Flush(); memoryStream.Position = 0L; return(memoryStream); }
/// <summary> /// 生成Execl /// </summary> /// <param name="HtmlTable"></param> /// <param name="fileName"></param> public static bool RenderToExcel(string HtmlTable, string fileName) { List <E_Row> list = GetRowList(HtmlTable); using (MemoryStream memoryStream = RenderToExcel(list)) { HtmlTableExport.SaveToFile(memoryStream, fileName); } return(true); }