java poi execl表格根据数据集合list数量名称数据相同的单元格进行合并规格数据相同的单元格进行合并部门数据相同的单元格进行合并申请人数据相同的单元格进行合并 并且在最后有合计行并计算名称相同数据合并后的数量
您可以使用Apache POI库来操作Excel表格,并按照您的要求进行合并和计算。下面是一个示例代码:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelMergeExample {
public static void main(String[] args) throws IOException {
// 创建工作簿和表格
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 模拟数据集合
List<Data> dataList = getDataList();
// 创建行和列索引变量
int rowIndex = 0;
int columnIndex = 0;
// 创建合并单元格的范围
CellRangeAddressList rangeList = new CellRangeAddressList();
// 创建数量、名称、规格、部门、申请人对应的合并单元格范围的Map
Map<String, CellRangeAddress> quantityMap = new HashMap<>();
Map<String, CellRangeAddress> nameMap = new HashMap<>();
Map<String, CellRangeAddress> specificationMap = new HashMap<>();
Map<String, CellRangeAddress> departmentMap = new HashMap<>();
Map<String, CellRangeAddress> applicantMap = new HashMap<>();
// 遍历数据集合
for (Data data : dataList) {
// 创建新行
Row row = sheet.createRow(rowIndex);
// 创建单元格并设置值
Cell cell1 = row.createCell(columnIndex);
cell1.setCellValue(data.getQuantity());
Cell cell2 = row.createCell(columnIndex + 1);
cell2.setCellValue(data.getName());
Cell cell3 = row.createCell(columnIndex + 2);
cell3.setCellValue(data.getSpecification());
Cell cell4 = row.createCell(columnIndex + 3);
cell4.setCellValue(data.getDepartment());
Cell cell5 = row.createCell(columnIndex + 4);
cell5.setCellValue(data.getApplicant());
// 添加合并单元格的范围
String quantityKey = data.getQuantity();
if (!quantityMap.containsKey(quantityKey)) {
quantityMap.put(quantityKey, new CellRangeAddress(rowIndex, rowIndex, columnIndex, columnIndex));
} else {
CellRangeAddress rangeAddress = quantityMap.get(quantityKey);
rangeAddress.setLastRow(rangeAddress.getLastRow() + 1);
}
rangeList.addCellRangeAddress(quantityMap.get(quantityKey));
String nameKey = data.getName();
if (!nameMap.containsKey(nameKey)) {
nameMap.put(nameKey, new CellRangeAddress(rowIndex, rowIndex, columnIndex + 1, columnIndex + 1));
} else {
CellRangeAddress rangeAddress = nameMap.get(nameKey);
rangeAddress.setLastRow(rangeAddress.getLastRow() + 1);
}
rangeList.addCellRangeAddress(nameMap.get(nameKey));
String specificationKey = data.getSpecification();
if (!specificationMap.containsKey(specificationKey)) {
specificationMap.put(specificationKey, new CellRangeAddress(rowIndex, rowIndex, columnIndex + 2, columnIndex + 2));
} else {
CellRangeAddress rangeAddress = specificationMap.get(specificationKey);
rangeAddress.setLastRow(rangeAddress.getLastRow() + 1);
}
rangeList.addCellRangeAddress(specificationMap.get(specificationKey));
String departmentKey = data.getDepartment();
if (!departmentMap.containsKey(departmentKey)) {
departmentMap.put(departmentKey, new CellRangeAddress(rowIndex, rowIndex, columnIndex + 3, columnIndex + 3));
} else {
CellRangeAddress rangeAddress = departmentMap.get(departmentKey);
rangeAddress.setLastRow(rangeAddress.getLastRow() + 1);
}
rangeList.addCellRangeAddress(departmentMap.get(departmentKey));
String applicantKey = data.getApplicant();
if (!applicantMap.containsKey(applicantKey)) {
applicantMap.put(applicantKey, new CellRangeAddress(rowIndex, rowIndex, columnIndex + 4, columnIndex + 4));
} else {
CellRangeAddress rangeAddress = applicantMap.get(applicantKey);
rangeAddress.setLastRow(rangeAddress.getLastRow() + 1);
}
rangeList.addCellRangeAddress(applicantMap.get(applicantKey));
rowIndex++;
}
// 执行合并单元格
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:E1"));
sheet.setAutoFilter(CellRangeAddress.valueOf("A" + (rowIndex + 1) + ":E" + (rowIndex + 1)));
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:E" + (rowIndex + 1)));
sheet.setAutoFilter(rangeList);
// 创建合计行
Row totalRow = sheet.createRow(rowIndex + 1);
// 创建合计标题单元格
Cell totalCell1 = totalRow.createCell(columnIndex);
totalCell1.setCellValue("合计");
// 计算名称相同数据合并后的数量
for (String nameKey : nameMap.keySet()) {
CellRangeAddress rangeAddress = nameMap.get(nameKey);
int firstRow = rangeAddress.getFirstRow();
int lastRow = rangeAddress.getLastRow();
int totalQuantity = 0;
for (int i = firstRow; i <= lastRow; i++) {
Row row = sheet.getRow(i);
Cell quantityCell = row.getCell(columnIndex);
totalQuantity += quantityCell.getNumericCellValue();
}
Cell totalQuantityCell = totalRow.createCell(columnIndex + 1);
totalQuantityCell.setCellValue(totalQuantity);
}
// 保存Excel文件
FileOutputStream fileOutputStream = new FileOutputStream("output.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Excel文件已生成。");
}
private static List<Data> getDataList() {
// 编写自己的获取数据集合的逻辑
// 这里只是一个示例,您需要根据您的数据源编写代码
return null;
}
private static class Data {
private String quantity;
private String name;
private String specification;
private String department;
private String applicant;
// 省略构造方法和getter/setter方法
}
}
请注意,此示例代码中的getDataList()方法应根据您的数据源编写。此外,您需要根据自己的需求进一步调整代码
原文地址: https://www.cveoy.top/t/topic/hK91 著作权归作者所有。请勿转载和采集!