最近工作遇到了导出execl画表格,记录一下,省的下次再忘了,用的第三方的NPOI, 其中涉及到动态合并单元格,其中你想要动态合并的那列的数据,最好查询数据的时候排序一下,不然还得要处理跨行合并的问题,那样比较麻烦,所以我是先排序了一下,以下是代码
1 public IWorkbook GetDeptExcelQuerys(YearQueryParam param, User user) 2 { 3 4 //创建workbook 5 IWorkbook workbook; 6 workbook = new HSSFWorkbook(); 7 ISheet sheet = workbook.CreateSheet("Sheet1"); 8 //表头样式 9 IRow headerRow = sheet.CreateRow(0); 10 headerRow.HeightInPoints = 25; 11 headerRow.CreateCell(0).SetCellValue("xxxx金额记录表"); 12 ICellStyle headStyle = workbook.CreateCellStyle(); 13 headStyle.Alignment = HorizontalAlignment.Center; 14 IFont font = workbook.CreateFont(); 15 font.Boldweight = 25; 16 font.FontHeightInPoints = 16; 17 font.Color = HSSFColor.Black.Index; 18 headStyle.SetFont(font); 19 headerRow.GetCell(0).CellStyle = headStyle; 20 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 7)); 21 22 //border 23 //普通单元格样式 24 ICellStyle bodyStyle = workbook.CreateCellStyle(); 25 bodyStyle.Alignment = HorizontalAlignment.Center; 26 bodyStyle.VerticalAlignment = VerticalAlignment.Center; 27 IFont font1 = workbook.CreateFont(); 28 font1.Color = HSSFColor.Black.Index; 29 font1.Boldweight = 25; 30 font1.FontHeightInPoints = 12; 31 bodyStyle.FillForegroundColor = HSSFColor.White.Index; 32 bodyStyle.SetFont(font1); 33 //设置格式 34 IDataFormat format = workbook.CreateDataFormat(); 35 // format.GetFormat("¥#,##0") 货币格式 36 // bodyStyle.DataFormat = format.GetFormat("Numeric"); 37 //cell.SetCellType(CellType.Numeric); 38 39 // 表头 40 Dictionary<string, string> arrList = new Dictionary<string, string> { { "序号", "Numbers" }, { "名字", "DeptName" }, { "地区", "RegionName" }, { "年份", "Year" }, { "预算金额(元)", "BudgetMoney" }, { "调整金额(元)", 41 "ModifyMoney" }, { "实际预算金额(元)", "RBudgetMoney" }, { "医疗总费用(元)", "TotalAmount" } }; 42 IRow headrow = sheet.CreateRow(1); 43 var cellCount = 0; 44 foreach (var item in arrList) 45 { 46 ICell headcell = headrow.CreateCell(cellCount); 47 headcell.SetCellValue(item.Key.ToString()); 48 headcell.CellStyle = headStyle; 49 cellCount++; 50 } 51 var result = GetDeptExeclQuerys(param, user); 52 53 var name = string.Empty; 54 var type = string.Empty; 55 object vue; 56 //填充表内数据 57 for (int i = 0; i < result.Count; i++) 58 { 59 var j = 0; 60 IRow row = sheet.CreateRow(i + 2); 61 foreach (var item in arrList) 62 { 63 ICell cell = row.CreateCell(j); 64 foreach (System.Reflection.PropertyInfo p in result[i].GetType().GetProperties()) 65 { 66 name = p.Name; 67 vue = p.GetValue(result[i]); 68 type = p.PropertyType.Name; 69 if (item.Value == name) 70 { 71 if (type == "Int32" || type == "Decimal") 72 { 73 cell.SetCellValue(CellbillIsRMB(vue, false)); 74 } 75 else 76 { 77 cell.SetCellValue(vue.ToString()); 78 } 79 cell.CellStyle = bodyStyle; 80 } 81 } 82 j++; 83 } 84 85 } 86 87 //合并行 88 ArrayList arrTitles = new ArrayList { { "Numbers" }, { "DeptName" }, { "RegionName" }, { "Year" }, { "BudgetMoney" }, { "ModifyMoney" }, { "RBudgetMoney" }, { "TotalAmount" } }; 89 DataTable dt = ListToDt(result, arrTitles, false, ""); //因为我取的数据源是集合,于是转成datatable,合并单元格的时候需要用到 90 MergeCells(sheet, dt, 0, 0, 0); //合并单元格 91 MergeCells(sheet, dt, 1, 1, 1); 92 MergeCells(sheet, dt, 2, 2, 2); 93 //列宽 94 sheet.SetColumnWidth(0, 20 * 256); 95 sheet.SetColumnWidth(1, 20 * 256); 96 sheet.SetColumnWidth(2, 20 * 256); 97 sheet.SetColumnWidth(3, 20 * 256); 98 sheet.SetColumnWidth(4, 25 * 256); 99 sheet.SetColumnWidth(5, 25 * 256); 100 sheet.SetColumnWidth(6, 25 * 256); 101 sheet.SetColumnWidth(7, 25 * 256); 102 103 return workbook; 104 }
1 ///2 /// 合并单元格 3 /// 4 /// 当前sheet 5 /// 数据源 6 /// 要合并的第几列 7 /// 起始列号 8 /// 终止列号 9 10 public void MergeCells(ISheet sheet, DataTable dt, int cellIndex, int startIndex, int endIndex) 11 { 12 for (int i = 1; i < dt.Rows.Count + 2; i++) 13 { 14 string value = sheet.GetRow(i).GetCell(cellIndex).StringCellValue; 15 int end = i; 16 for (int j = i + 1; j < dt.Rows.Count + 2; j++) 17 { 18 string value1 = sheet.GetRow(j).GetCell(cellIndex).StringCellValue; 19 if (value.Trim() != value1.Trim()) 20 { 21 end = j - 1; 22 break; 23 } 24 else if (value.Trim() == value1.Trim() && j == (dt.Rows.Count + 1)) 25 { 26 end = j; 27 break; 28 } 29 } 30 sheet.AddMergedRegion(new CellRangeAddress(i, end, startIndex, endIndex)); 31 i = end; 32 } 33 }
效果图:
第一次写博客有点懵!!