java poi execl表格根据数据集合list数量名称数据相同的单元格进行合并规格数据相同的单元格进行合并部门数据相同的单元格进行合并申请人数据相同的单元格进行合并 并且在最后有合计行并计算相同数据合并后的数量
您可以使用Apache POI库来操作Excel文件。下面是一个简单的示例,展示如何根据数据集合list中的数据合并相同的单元格:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelMergeCellsExample {
public static void main(String[] args) throws IOException {
// 创建一个新的Excel工作簿
Workbook workbook = new XSSFWorkbook();
// 创建一个工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 模拟数据集合list
List<Data> dataList = getDataList();
// 记录每个单元格的起始行和起始列
Map<String, CellAddress> cellAddresses = new HashMap<>();
int rowNum = 0;
// 写入表头
Row headerRow = sheet.createRow(rowNum++);
headerRow.createCell(0).setCellValue("数量");
headerRow.createCell(1).setCellValue("名称");
headerRow.createCell(2).setCellValue("规格");
headerRow.createCell(3).setCellValue("部门");
headerRow.createCell(4).setCellValue("申请人");
// 写入数据
for (Data data : dataList) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(data.getQuantity());
row.createCell(1).setCellValue(data.getName());
row.createCell(2).setCellValue(data.getSpecification());
row.createCell(3).setCellValue(data.getDepartment());
row.createCell(4).setCellValue(data.getApplicant());
}
// 合并相同数量的单元格
mergeCells(sheet, 1, rowNum, 0);
// 合并相同名称的单元格
mergeCells(sheet, 1, rowNum, 1);
// 合并相同规格的单元格
mergeCells(sheet, 1, rowNum, 2);
// 合并相同部门的单元格
mergeCells(sheet, 1, rowNum, 3);
// 合并相同申请人的单元格
mergeCells(sheet, 1, rowNum, 4);
// 创建合计行
Row totalRow = sheet.createRow(rowNum++);
totalRow.createCell(0).setCellValue("合计");
// 计算相同数据合并后的数量并写入合计行
for (int i = 1; i < rowNum; i++) {
Row row = sheet.getRow(i);
Cell quantityCell = row.getCell(0);
Cell totalCell = totalRow.createCell(0);
totalCell.setCellType(CellType.FORMULA);
totalCell.setCellFormula(totalCell.getCellFormula() + "+" + quantityCell.getAddress().formatAsString());
}
// 调整列宽以适应内容
for (int i = 0; i < 5; i++) {
sheet.autoSizeColumn(i);
}
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream("output.xlsx");
workbook.write(fileOut);
fileOut.close();
// 关闭工作簿
workbook.close();
}
private static void mergeCells(Sheet sheet, int startRow, int endRow, int columnIndex) {
CellAddress prevCellAddress = null;
for (int i = startRow; i < endRow; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(columnIndex);
CellAddress currentCellAddress = cell.getAddress();
if (prevCellAddress != null && !currentCellAddress.equals(prevCellAddress)) {
sheet.addMergedRegion(new CellRangeAddress(prevCellAddress.getRow(), currentCellAddress.getRow() - 1, columnIndex, columnIndex));
}
prevCellAddress = currentCellAddress;
}
if (prevCellAddress != null) {
sheet.addMergedRegion(new CellRangeAddress(prevCellAddress.getRow(), endRow - 1, columnIndex, columnIndex));
}
}
private static List<Data> getDataList() {
List<Data> dataList = new ArrayList<>();
// 添加示例数据
dataList.add(new Data(10, "商品1", "规格1", "部门1", "申请人1"));
dataList.add(new Data(20, "商品1", "规格2", "部门1", "申请人1"));
dataList.add(new Data(30, "商品2", "规格1", "部门2", "申请人2"));
dataList.add(new Data(40, "商品2", "规格1", "部门2", "申请人2"));
dataList.add(new Data(50, "商品2", "规格2", "部门2", "申请人3"));
return dataList;
}
private static class Data {
private int quantity;
private String name;
private String specification;
private String department;
private String applicant;
public Data(int quantity, String name, String specification, String department, String applicant) {
this.quantity = quantity;
this.name = name;
this.specification = specification;
this.department = department;
this.applicant = applicant;
}
public int getQuantity() {
return quantity;
}
public String getName() {
return name;
}
public String getSpecification() {
return specification;
}
public String getDepartment() {
return department;
}
public String getApplicant() {
return applicant;
}
}
}
此示例演示了如何使用Apache POI库在Excel表格中根据数据集合list进行单元格合并,并在最后添加合计行来计算相同数据合并后的数量。您可以根据您的实际需求进行修改和调整
原文地址: https://www.cveoy.top/t/topic/hK9C 著作权归作者所有。请勿转载和采集!