您可以使用 Apache POI 库来操作 Excel 表格。首先,您需要创建一个工作簿和一个工作表。然后,您可以使用循环遍历数据集合,并根据相同的数量、名称、规格、部门和申请人合并单元格。最后,您可以在合计行中计算相同名称的数据的总和。\n\n下面是一个示例代码,演示了如何使用 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.List;\n\npublic class ExcelMergeCellsExample {\n\n public static void main(String[] args) {\n List<AmcOfficeinfosUtil> data = getData(); // 获取数据集合\n\n // 创建工作簿和工作表\n Workbook workbook = new XSSFWorkbook();\n Sheet sheet = workbook.createSheet("Sheet1");\n\n int rowIndex = 0;\n\n // 遍历数据集合\n for (AmcOfficeinfosUtil item : data) {\n Row row = sheet.createRow(rowIndex++);\n int cellIndex = 0;\n\n // 创建单元格并设置值\n Cell cell = row.createCell(cellIndex++);\n cell.setCellValue(item.getQuantity());\n\n cell = row.createCell(cellIndex++);\n cell.setCellValue(item.getName());\n\n cell = row.createCell(cellIndex++);\n cell.setCellValue(item.getSpecification());\n\n cell = row.createCell(cellIndex++);\n cell.setCellValue(item.getDepartment());\n\n cell = row.createCell(cellIndex);\n cell.setCellValue(item.getApplicant());\n }\n\n // 合并数量相同的单元格\n mergeCells(sheet, 0);\n\n // 合并名称相同的单元格\n mergeCells(sheet, 1);\n\n // 合并规格相同的单元格\n mergeCells(sheet, 2);\n\n // 合并部门相同的单元格\n mergeCells(sheet, 3);\n\n // 合并申请人相同的单元格\n mergeCells(sheet, 4);\n\n // 在最后添加合计行\n addTotalRow(sheet);\n\n // 保存工作簿到文件\n try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) {\n workbook.write(outputStream);\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n\n private static void mergeCells(Sheet sheet, int columnIndex) {\n int lastRowIndex = sheet.getLastRowNum();\n int startRowIndex = 0;\n\n while (startRowIndex <= lastRowIndex) {\n Row startRow = sheet.getRow(startRowIndex);\n Cell startCell = startRow.getCell(columnIndex);\n\n int endRowIndex = startRowIndex + 1;\n\n while (endRowIndex <= lastRowIndex) {\n Row endRow = sheet.getRow(endRowIndex);\n Cell endCell = endRow.getCell(columnIndex);\n\n if (!startCell.getStringCellValue().equals(endCell.getStringCellValue())) {\n break;\n }\n\n endRowIndex++;\n }\n\n if (endRowIndex > startRowIndex + 1) {\n sheet.addMergedRegion(new CellRangeAddress(startRowIndex, endRowIndex - 1, columnIndex, columnIndex));\n }\n\n startRowIndex = endRowIndex;\n }\n }\n\n private static void addTotalRow(Sheet sheet) {\n int lastRowIndex = sheet.getLastRowNum();\n\n Row totalRow = sheet.createRow(lastRowIndex + 1);\n Cell totalCell = totalRow.createCell(0);\n totalCell.setCellValue("合计");\n\n for (int i = 1; i < sheet.getRow(0).getLastCellNum(); i++) {\n CellRangeAddress cellRange = sheet.getMergedRegion(0, i);\n int firstRow = cellRange.getFirstRow();\n int lastRow = cellRange.getLastRow();\n\n double total = 0;\n\n for (int j = firstRow; j <= lastRow; j++) {\n Row row = sheet.getRow(j);\n Cell cell = row.getCell(i);\n total += cell.getNumericCellValue();\n }\n\n Cell totalValueCell = totalRow.createCell(i);\n totalValueCell.setCellValue(total);\n }\n }\n\n // 获取数据集合\n private static List<AmcOfficeinfosUtil> getData() {\n // 返回您的数据集合\n }\n}\n\n\n请注意,您需要根据您的实际数据集合和需求进行适当的调整。此示例仅用于演示如何使用 POI 库合并单元格和添加合计行。

Java POI Excel 合并单元格:根据数量、名称、规格、部门和申请人进行合并并计算总计

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

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