如何在创建新的 Excel 文件时初始化单元格 (Apache POI)
我目前正在使用 Apache POI 创建空的 excel 文件(我稍后将阅读和编辑)。问题是,每当我尝试获取单元格时,我总是得到一个空值。我尝试初始化前几列和行(单元格不再为空),但使用这种方法,我无法插入新的行和列。如何能够初始化工作表的所有单元格而无需设置行数和列数?谢谢
编辑:嗨,这是我创建 Excel 文件的代码。我无法使用迭代器来初始化所有单元格,因为电子表格没有行和列。
FileOutputStream fileOut = new FileOutputStream(loc + formID +".xls");
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
for (Row row : sheet) {
for (Cell cell : row) {
CellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
cell.setCellValue("");
cell.setCellStyle(style);
}
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
I am currently using Apache POI to create empty excel files (that I will later read and edit). The problem is that whenever I try to get the cell, I always get a null value. I have tried initializing the first few columns and rows (the cells are no longer null) but with this approach, I cannot insert new rows and columns. How can I be able to initialize all cells of a sheet without having to set the number of rows and columns? Thanks
EDIT: Hi this is my code in creating excel files. I could not use the iterator to initialize all my cells since there are no rows and columns for the spreadsheet.
FileOutputStream fileOut = new FileOutputStream(loc + formID +".xls");
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
for (Row row : sheet) {
for (Cell cell : row) {
CellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
cell.setCellValue("");
cell.setCellStyle(style);
}
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现 MissingCellPolicy 可以帮助满足您的需求。当在一行上调用 getCell 时,您可以指定 MissingCellPolicy 以便在没有单元格的情况下发生某些事情,例如
org.apache.poi.ss.util.CellUtil 也可以提供帮助:
You might find that MissingCellPolicy can help with your needs. When calling getCell on a row, you can specify a MissingCellPolicy to have something happen if no cell was there, eg
org.apache.poi.ss.util.CellUtil can also help too: