如何在创建新的 Excel 文件时初始化单元格 (Apache POI)

发布于 2024-10-27 16:08:58 字数 809 浏览 0 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2024-11-03 16:08:58

您可能会发现 MissingCellPolicy 可以帮助满足您的需求。当在一行上调用 getCell 时,您可以指定 MissingCellPolicy 以便在没有单元格的情况下发生某些事情,例如

Row r = sheet.getRow(2);
Cell c = r.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK);
c.setCellValue("This will always work, c will never be null");

org.apache.poi.ss.util.CellUtil 也可以提供帮助:

// Get the row, creating it if needed
Row r = CellUtil.getRow(4, sheet);
// Get the cell, creating it if needed
Cell c = CellUtil.getCell(2, r);

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

Row r = sheet.getRow(2);
Cell c = r.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK);
c.setCellValue("This will always work, c will never be null");

org.apache.poi.ss.util.CellUtil can also help too:

// Get the row, creating it if needed
Row r = CellUtil.getRow(4, sheet);
// Get the cell, creating it if needed
Cell c = CellUtil.getCell(2, r);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文