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);
}