我想知道如何使用Apache POI在.xlsx文件中添加新列。但是我没有找到相关资料。有没有办法解决这个问题?或者是否有其他库可以解决这个问题?提前感谢。
我想知道如何使用Apache POI在.xlsx文件中添加新列。但是我没有找到相关资料。有没有办法解决这个问题?或者是否有其他库可以解决这个问题?提前感谢。
FileInputStream excelFile = new FileInputStream(new File(fileDirectory+file));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
// Add additional column for results
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Cell cell = currentRow.createCell(currentRow.getLastCellNum(), CellType.STRING);
if(currentRow.getRowNum() == 0)
cell.setCellValue("NEW-COLUMN");
}
使用apache POI没有明确的方法来实现这一点。如果您知道所需的行数和列数,可以先创建所需数量的行,然后在行中创建相应的单元格。如有需要,可以参考以下代码。
for(row=0;row<maxRowLimit;row++){
myRow = sheet.getRow(row);
if (myRow == null) {
myRow = sheet.createRow(row);
myCell=myRow.getCell(columnNumber);
if (myCell == null)
myRow.createCell(columnNumber);
}
}