欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java对已有excel进行信息追加

程序员文章站 2024-03-09 10:00:53
...
private void process(String path, String sheetName, int col, String value) throws Exception {
        File file = new File(path);
        if (!file.exists()) {
            //创建新文件
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook.createSheet(sheetName);
            workbook.write(fileOutputStream);
        }
        FileInputStream inputStream = new FileInputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = workbook.getSheet(sheetName);
        if (sheet == null) {
            workbook.createSheet(sheetName);
        }
        int lastRowNum = sheet.getPhysicalNumberOfRows();
        XSSFRow row = sheet.createRow(lastRowNum);
        row.createCell(col).setCellValue(value);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        workbook.write(fileOutputStream);
    }