【问题标题】:Using SXSSF(APACHE POI) and adding comment does not generate proper excel file使用 SXSSF(APACHE POI) 并添加注释不会生成正确的 excel 文件
【发布时间】:2015-03-25 23:32:44
【问题描述】:

我在使用 SXSSF poi api 生成有效的 xlsx 文件时遇到问题。 如果我使用以下代码会生成正确的 excel 文件: 工作簿 wb = new XSSFWorkbook(); 但如果我使用失败: SXSSFWorkbook wb = new SXSSFWorkbook(100);

错误:已删除记录:来自 /xl/cmets1.xml 部分的评论(评论)

请帮助我了解代码有什么问题。我正在寻找使用 SXSSF api 生成包含单元格 cmets 的 excel 文件。

实际代码:

    public static void main(String[] args) throws Exception {
   // Workbook wb = new XSSFWorkbook();
    SXSSFWorkbook wb = new SXSSFWorkbook(100);
    Sheet sh = wb.createSheet();       
    for(int rownum = 0; rownum < 1000; rownum++){
        Row row = sh.createRow(rownum);
        for(int cellnum = 0; cellnum < 10; cellnum++){
            Cell cell = row.createCell(cellnum);
            String address = new CellReference(cell).formatAsString();
            cell.setCellValue(address);
            setCellComment(cell,address);                
        }
    }

    FileOutputStream out = new FileOutputStream("comments.xlsx");
    wb.write(out);
    out.close();
}

 protected static void setCellComment(Cell cell, String message) {
        Drawing drawing = cell.getSheet().createDrawingPatriarch();
        CreationHelper factory = cell.getSheet().getWorkbook()
                .getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(cell.getRowIndex());
        anchor.setRow2(cell.getRowIndex() + 1);
        anchor.setDx1(100);
        anchor.setDx2(100);
        anchor.setDy1(100);
        anchor.setDy2(100);

        // Create the comment and set the text+author
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString(message);
        comment.setString(str);
        comment.setAuthor("Apache POI");
        // Assign the comment to the cell
        cell.setCellComment(comment);
    }

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:

    我遇到了同样的问题,并通过在Comment 上设置行和列属性来解决它。

    如下修改示例中的setCellComment() 方法应该可以解决问题。

    protected static void setCellComment(Cell cell, String message) {
    
        ...
    
        // Create the comment and set the text+author
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString(message);
        comment.setString(str);
        comment.setAuthor("Apache POI");
    
        // Set the row and column here
        comment.setRow(cell.getRowIndex());
        comment.setColumn(cell.getColumnIndex());
    
        // Assign the comment to the cell
        cell.setCellComment(comment);
    } 
    

    我通过查看XSSFCell 类中setCellComment() 方法的来源发现了这个解决方案。 SXSSFCell中对应的方法没有设置行列。

    【讨论】:

    • 这可能是 SXSSF 中的一个错误,您有机会在 POI bugzilla 问题跟踪器中报告它吗?
    • 我刚刚检查了 POI Bugzilla,似乎已经有针对此问题的错误报告:issues.apache.org/bugzilla/show_bug.cgi?id=56424
    • 那个 bug 看起来需要一个 junit 单元测试和一个补丁,你有/大部分都有。我强烈建议你为它做出贡献,这样可以修复错误:)
    • 这是我的确切问题,这个解决方案是完美的解决方案。拯救了我的一天。不过对我来说它的 EOD ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多