内容纲要
数据列表导出,并按指定列进行合并
- 组织参数代码
String export(rows){ def list = [] list.add(['熔炼炉号','材质','钢水总重kg','铸件单重','钢水单重','件数','图号','铸件名称', '电炉班组','造型班组','铸造号','化学成分和性能控制代号' ]) rows.each{ list.add([ it.furnace, it.texture, it.total_molten_steel_weight, it.rough_weight, it.molten_steel_weight, it.num, it.drawing, it.mater_name, it.yljz_dept_name, it.zxhx_dept_name, it.casting, it.standard_code ]) } //合并列 def megeIndex = null megeIndex = [0,1,2,11] }
- 计算同一列相同值,并对其它值求和
//合并同熔炼炉号的钢水重量 private void calcTotal(List<Record> castList) { def i = -1 //前一个炉号,不用null避免数据炉号有null def pre_furnace = "✾" //钢水总重 def total_molten_steel_weight = 0 castList.eachWithIndex { it, index -> def molten_steel_weight = it.molten_steel_weight if(molten_steel_weight==null){ molten_steel_weight=0 } if (it.furnace == pre_furnace && it.furnace!=null) { total_molten_steel_weight += molten_steel_weight } else { if (index > 0) { castList[index - 1].total_molten_steel_weight = total_molten_steel_weight } i++ pre_furnace = it.furnace total_molten_steel_weight = molten_steel_weight } if (index == castList.size() - 1) { castList[index].total_molten_steel_weight = total_molten_steel_weight } } if (castList != null && castList.size() > 0) { def reverseList = castList.reverse(); reverseList.eachWithIndex { it, index -> if (it.total_molten_steel_weight == null && index > 0) { it.total_molten_steel_weight = reverseList[index - 1].total_molten_steel_weight } } } }
- 导出excel代码
/** * 导出excel * @param list 数据列,第一行是表头 * @param path 文件存放路径 * @param mergeIndex 需要合并的列号 */ public static void ExportExcel(List<Object> list, String path, List<Integer> mergeIndex) { File dbfFile = new File(path); FileOutputStream out = null; Workbook wb = null; try { if (!dbfFile.exists() || dbfFile.isDirectory()) { dbfFile.createNewFile(); } out = new FileOutputStream(dbfFile); if (path.toLowerCase().endsWith(".xls")) { wb = new HSSFWorkbook(); } else { wb = new XSSFWorkbook(); } boolean mergeList = mergeIndex!=null&&mergeIndex.size()>0; Sheet sheet = wb.createSheet("Sheet1"); sheet.setDefaultColumnWidth(15); sheet.setDefaultRowHeight((short) (20 * 20)); CellStyle style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //存储合并的比对值:key 列,value[0] 比对值,value[1] 起始行 HashMap<Integer,Object[]> compareMap = new HashMap<>(); for (int r = 0; r < list.size(); r++) { List dataRow = (List) list.get(r); if (dataRow != null) { Row row = sheet.createRow(r); for (int c = 0; c < dataRow.size(); c++) { Object dataCell = dataRow.get(c); if (dataCell != null) { Cell cell = row.createCell(c); cell.setCellStyle(style); cell.setCellValue(dataCell.toString()); } //判断列是否要合并 if(mergeList&&mergeIndex.contains(c) && r>0){ Object[] compare = compareMap.get(c); if(compare!=null){//该列有比对值 if(!compare[0].equals(dataCell)){//当前值和比对值不同,则将前面列进行合并 //合并单元格范围 CellRangeAddress cra=new CellRangeAddress((int) compare[1],r-1,c,c); sheet.addMergedRegion(cra); if(dataCell==null){//当前值为空则移除比对值 compareMap.remove(c); }else {//替换比对值 compareMap.put(c,new Object[]{dataCell,r}); } } }else if(dataCell!=null){ //该列没有比对值,且当前值不为空,则将当前值存为比对值供后面比对 compareMap.put(c,new Object[]{dataCell,r}); } //如果当前行是最后一行,直接合并 if(r==list.size()-1 && compare!=null){ //合并单元格范围 CellRangeAddress cra=new CellRangeAddress((int) compare[1],r-1,c,c); sheet.addMergedRegion(cra); } } } } } wb.write(out); } catch (IOException e) { e.printStackTrace(); } finally { if(wb!=null){ try { wb.close(); } catch (IOException e) { e.printStackTrace(); } } if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }