以下是使用Apache POI库来实现合并单元格和计算合计行的示例代码:\n\njava\nimport org.apache.poi.ss.usermodel.*;\nimport org.apache.poi.xssf.usermodel.XSSFWorkbook;\n\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\n\npublic class ExcelMergeCellsExample {\n\n public static void main(String[] args) {\n // 创建数据集合\n List<AmcOfficeinfosUtil> dataList = new ArrayList<>();\n dataList.add(new AmcOfficeinfosUtil(&quot;数量1&quot;, &quot;名称1&quot;, &quot;规格1&quot;, &quot;部门1&quot;, &quot;申请人1&quot;));\n dataList.add(new AmcOfficeinfosUtil(&quot;数量2&quot;, &quot;名称2&quot;, &quot;规格2&quot;, &quot;部门2&quot;, &quot;申请人2&quot;));\n dataList.add(new AmcOfficeinfosUtil(&quot;数量1&quot;, &quot;名称1&quot;, &quot;规格1&quot;, &quot;部门1&quot;, &quot;申请人1&quot;));\n dataList.add(new AmcOfficeinfosUtil(&quot;数量3&quot;, &quot;名称3&quot;, &quot;规格3&quot;, &quot;部门3&quot;, &quot;申请人3&quot;));\n dataList.add(new AmcOfficeinfosUtil(&quot;数量2&quot;, &quot;名称2&quot;, &quot;规格2&quot;, &quot;部门2&quot;, &quot;申请人2&quot;));\n\n // 创建Excel文档\n Workbook workbook = new XSSFWorkbook();\n Sheet sheet = workbook.createSheet(&quot;Sheet1&quot;);\n\n // 设置标题行\n Row headerRow = sheet.createRow(0);\n headerRow.createCell(0).setCellValue(&quot;数量&quot;);\n headerRow.createCell(1).setCellValue(&quot;名称&quot;);\n headerRow.createCell(2).setCellValue(&quot;规格&quot;);\n headerRow.createCell(3).setCellValue(&quot;部门&quot;);\n headerRow.createCell(4).setCellValue(&quot;申请人&quot;);\n\n // 设置数据行\n int rowIndex = 1;\n for (AmcOfficeinfosUtil data : dataList) {\n Row dataRow = sheet.createRow(rowIndex++);\n dataRow.createCell(0).setCellValue(data.getQuantity());\n dataRow.createCell(1).setCellValue(data.getName());\n dataRow.createCell(2).setCellValue(data.getSpecification());\n dataRow.createCell(3).setCellValue(data.getDepartment());\n dataRow.createCell(4).setCellValue(data.getApplicant());\n }\n\n // 合并单元格并计算合计行\n mergeCellsAndCalculateTotal(workbook, sheet);\n\n // 保存Excel文件\n try (FileOutputStream outputStream = new FileOutputStream(&quot;output.xlsx&quot;)) {\n workbook.write(outputStream);\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n\n private static void mergeCellsAndCalculateTotal(Workbook workbook, Sheet sheet) {\n Map<String, Integer> nameQuantityMap = new HashMap<>();\n\n for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {\n Row row = sheet.getRow(rowIndex);\n Cell nameCell = row.getCell(1);\n Cell quantityCell = row.getCell(0);\n\n String name = nameCell.getStringCellValue();\n int quantity = (int) quantityCell.getNumericCellValue();\n\n // 合并相同名称的单元格\n if (nameQuantityMap.containsKey(name)) {\n int mergeStartRow = nameQuantityMap.get(name);\n int mergeEndRow = rowIndex;\n\n sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, mergeEndRow, 1, 1));\n } else {\n nameQuantityMap.put(name, rowIndex);\n }\n\n // 计算名称相同的数量合计\n if (nameQuantityMap.containsKey(name)) {\n int totalQuantity = nameQuantityMap.get(name);\n totalQuantity += quantity;\n nameQuantityMap.put(name, totalQuantity);\n }\n }\n\n // 添加合计行\n int totalRow = sheet.getLastRowNum() + 1;\n Row totalRowData = sheet.createRow(totalRow);\n totalRowData.createCell(0).setCellValue(&quot;合计&quot;);\n totalRowData.createCell(1).setCellValue(&quot;总数量&quot;);\n\n for (Map.Entry<String, Integer> entry : nameQuantityMap.entrySet()) {\n int rowIndex = entry.getValue();\n String name = entry.getKey();\n int totalQuantity = entry.getValue();\n\n Row row = sheet.getRow(rowIndex);\n Cell nameCell = row.getCell(1);\n\n if (nameCell.getStringCellValue().equals(name)) {\n totalRowData.createCell(2).setCellValue(totalQuantity);\n break;\n }\n }\n\n // 设置合计行样式\n CellStyle style = workbook.createCellStyle();\n Font font = workbook.createFont();\n font.setBold(true);\n style.setFont(font);\n totalRowData.getCell(0).setCellStyle(style);\n totalRowData.getCell(1).setCellStyle(style);\n totalRowData.getCell(2).setCellStyle(style);\n }\n}\n\n\n请注意,上述代码中的AmcOfficeinfosUtil是一个自定义类,您需要根据您的实际情况来替换它,并提供相应的getter方法来获取数据。此外,代码假定您使用的是.xlsx格式的Excel文件。如果您使用的是.xls格式,请将XSSFWorkbook更改为HSSFWorkbook

Java POI Excel表格合并单元格和计算合计行:根据数据集合List<AmcOfficeinfosUtil>操作

原文地址: https://www.cveoy.top/t/topic/puzy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录