/// <summary> /// 导出文档 /// <para>resultDic:key-系统名称,value-对应表信息</para> /// </summary> public virtual void ExportDoc(Dictionary <string, List <ComparaResult> > resultDic, string folder) { // 导出Excel var excel = new HSSFWorkbook(); // 单元格样式 ICellStyle cellStyle = excel.CreateCellStyle(); cellStyle.VerticalAlignment = VerticalAlignment.Center; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; // 标题样式 ICellStyle titleStyle = excel.CreateCellStyle(); titleStyle.CloneStyleFrom(cellStyle); titleStyle.Alignment = HorizontalAlignment.Center; IFont font = excel.CreateFont(); font.Boldweight = (short)FontBoldWeight.Bold; titleStyle.SetFont(font); // 备注样式 // 样式尽可能定义好并复用,动态创建可能会导致导出的Excel样式错乱(和NPOI内部实现有关) ICellStyle remarkStyle = excel.CreateCellStyle(); remarkStyle.CloneStyleFrom(cellStyle); remarkStyle.FillPattern = FillPattern.SolidForeground; remarkStyle.FillForegroundColor = HSSFColor.Yellow.Index; // 设置 resultDic.ForEach(dic => { ISheet sheet = excel.CreateSheet(dic.Key); // 创建标题 IRow rowTitle = sheet.CreateRow(0); CreateCell(ref rowTitle, 0, titleStyle, "表中文名"); CreateCell(ref rowTitle, 1, titleStyle, "对比表名"); CreateCell(ref rowTitle, 2, titleStyle, "目标表名"); CreateCell(ref rowTitle, 3, titleStyle, "标志"); CreateCell(ref rowTitle, 4, titleStyle, "是否清空"); CreateCell(ref rowTitle, 5, titleStyle, "备注"); // 创建内容 dic.Value.ForEach((result, i) => { var e = (ComparaInfo)result; IRow row = sheet.CreateRow(i + 1); CreateCell(ref row, 0, cellStyle, e.TableNameChn); CreateCell(ref row, 1, cellStyle, e.ComparaTableName); CreateCell(ref row, 2, cellStyle, e.TargetTableName); CreateCell(ref row, 3, cellStyle, e.Flag); CreateCell(ref row, 4, cellStyle, e.IsClear); CreateCell(ref row, 5, result.IsAssert ? remarkStyle : cellStyle, e.Remark); }); // 列宽自适应 for (int i = 0; i < rowTitle.Cells.Count; i++) { sheet.AutoSizeColumn(i); } }); // 写入文件 string path = folder + "清空升级.xls"; IOExt.CreateFile(path); File.SetAttributes(path, FileAttributes.Normal); using (var fs = new FileStream(path, FileMode.Create)) { excel.Write(fs); } }