首页 > 解决方案 > 如何使用 inig poi 3.15 处理 java 报告中的自动大小问题?

问题描述

尝试使用此方法导出一个 excel 文件:

private Sheet activeSheet;
public void addText(int column, int rowNumber, String s) throws Exception {
    // TODO Auto-generated method stub
    Row row = activeSheet.getRow(rowNumber);
    if(row==null){
        row = activeSheet.createRow(rowNumber);
    }
    Cell cell = row.createCell(column);

    s = ToolUtils.nn(s);
    if(s.indexOf("\n")!=-1){
        int nbLines = StringUtils.countMatches(s, "\n") ;
        CellStyle cs = workbook.createCellStyle();
        cs.setWrapText(true);
        //cs.set
        cell.setCellStyle(cs);
        row.setHeightInPoints(((nbLines+1)*activeSheet.getDefaultRowHeightInPoints()));
        activeSheet.autoSizeColumn(column);

    }

    //cell.setCellValue(s);
    cell.setCellValue(s == null ? "" : s);
}

我有这个错误:

java.lang.IllegalStateException:无法自动调整列大小。确保在自动调整列大小之前跟踪列

我试图调用这个方法:

trackAllColumnsForAutoSizing()解决这个问题,但它不能作为工作表界面的已知方法工作。我要升级apache poi版本吗?我该怎么做才能修复将数据插入excel列的方法?

标签: java

解决方案


private SXSSFSheet activeSheet;
public void addText(int column, int rowNumber, String s) throws Exception {
    // TODO Auto-generated method stub

ActiveSheet=workbook.createSheet();
    Row row = activeSheet.getRow(rowNumber);
    if(row==null){
        row = activeSheet.createRow(rowNumber);
    }
    Cell cell = row.createCell(column);
cell.setCellValue(s == null ? "" : s);
    s = ToolUtils.nn(s);
    if(s.indexOf("\n")!=-1){
        int nbLines = StringUtils.countMatches(s, "\n") ;
        CellStyle cs = workbook.createCellStyle();
        cs.setWrapText(true);
        //cs.set
        cell.setCellStyle(cs);
        row.setHeightInPoints(((nbLines+1)*activeSheet.getDefaultRowHeightInPoints()));
   activeSheet.trackColumnsForAutoSizing();    activeSheet.autoSizeColumn(column);

    }



}

试试这个可能对你有帮助 可能有一些语法错误刚刚解决/调整你的 IDE 中的那些。


推荐阅读