Java实现修改excel表格的内容并另存为新表格

    public void generateCombineTable() {
        String sourceFilePath = "C:\\Users\\DELL\\Desktop\\新建文件夹\\测试表.xlsx";
        String targetFilePath = "C:\\Users\\DELL\\Desktop\\新建文件夹\\测试表-new.xlsx";

        try {
            FileInputStream inputStream = new FileInputStream(sourceFilePath);
            Workbook workbook = null;
            if ("xlsx".equals(getExtension(sourceFilePath))) {
                workbook = new XSSFWorkbook(inputStream);
            } else if ("xls".equals(getExtension(sourceFilePath))) {
                workbook = new HSSFWorkbook(inputStream);
            }
            if (workbook != null) {
                int numberOfSheets = workbook.getNumberOfSheets();
                for (int j = 0; j < numberOfSheets; j++) {
                    // 获取第1个工作表
                    if (j != 0) continue;
                    Sheet sheet = workbook.getSheetAt(j);
                    int lastRowNum = sheet.getLastRowNum(); // 获取最后一行的索引
                    int lastColumnNum = ExcelUtil.getMaxColumnCount(sheet); // 获取最后一列的索引

                    // 迭代行
                    outerLoop:
                    for (int rowIndex = 0; rowIndex <= lastRowNum; rowIndex++) {
                        Row row = sheet.getRow(rowIndex);
                        if (row == null) {
                            continue;
                        }

                        //迭代列
                        for (int colIndex = 0; colIndex < lastColumnNum; colIndex++) {
                            Cell cell = row.getCell(colIndex);
                            if (cell == null) {
                                continue;
                            }

                            String contents = ExcelUtil.getCellValue(cell);
                            if ("旧内容".equals(contents)) {
                            	//在这里替换内容
                                cell.setCellValue("新内容");
                            }
                        }
                    }
                }

                // 将修改后的Workbook写入到新的文件
                File targetFile = new File(targetFilePath);
                File parentDir = targetFile.getParentFile();
                // 如果父目录不存在,则创建
                if (parentDir != null && !parentDir.exists()) {
                    parentDir.mkdirs();
                }
                try (FileOutputStream outputStream = new FileOutputStream(targetFilePath)) {
                    workbook.write(outputStream);
                }

                // 关闭工作簿
                workbook.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

ExcelUtil

import org.apache.poi.ss.usermodel.*;

/**
 * Excel相关处理
 */
public class ExcelUtil {
    /**
     * 根据单元格的类型获取其值。
     *
     * @param cell 要获取值的单元格
     * @return 单元格的值
     */
    public static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
                return "";
            default:
                return "";
        }
    }

    /**
     * 获取工作表中的最大列数
     */
    public static int getMaxColumnCount(Sheet sheet) {
        int lastRowNum = sheet.getLastRowNum(); // 获取最后一行的索引,这通常是工作表中的最大行数
        int lastColumnNum = 0; // 获取最后一列的索引(基于0),这通常是行中的最大列数
        for (int r = 0; r <= lastRowNum; r++) {
            Row row = sheet.getRow(r);
            if (row != null) {
                int tempLastColumnNum = row.getLastCellNum();
                if (tempLastColumnNum > lastColumnNum) {
                    lastColumnNum = tempLastColumnNum;
                }
            }
        }
        return lastColumnNum;
    }

    /**
     * 设置单元格居中显示的样式
     */
    public static CellStyle setCellCenterStyle(Workbook workbook) {
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 设置单元格边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        return cellStyle;
    }

    public static CellStyle setCellBorderStyle(Workbook workbook) {
        CellStyle cellStyle = workbook.createCellStyle();
        // 设置单元格边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        return cellStyle;
    }
}

你可能感兴趣的:(java,excel)