2

trying to export on one excel file using this method:

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);
}

I have got this error:

java.lang.IllegalStateException: Could not auto-size column. Make sure the column was tracked prior to auto-sizing the column

I have try to call this methode:

trackAllColumnsForAutoSizing() to fix the issue but it does not work as know method for the sheet interface. Shall I upgrade the apache poi version? what shall I do do fix the method that insert data into the excel columns?

Alex Sergeenko
  • 642
  • 5
  • 22
user3070123
  • 159
  • 2
  • 20
  • just as other remarque when I try to call the methode trackAllColumnsFor AutoSizing() : alwyes I have got this : The method trackAllColumnsForAutoSizing() is undefined for the type SXSSFSheet !! – user3070123 Jul 19 '18 at 13:42
  • https://stackoverflow.com/questions/14497082/autosizecolumns-on-sxssfworkbook Refer this may this help. – sidd shadab Jul 19 '18 at 13:46
  • unfortunatly not , I have updated on my methode to make sure the cells are not empty .. but it does not solve the problem – user3070123 Jul 19 '18 at 13:50

3 Answers3

3
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);

    }



}

Try this once may this helps you There may be some syntax error just resolved/adjust those in your IDE.

sidd shadab
  • 123
  • 1
  • 11
1
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.trackColumnsForAutoSizing(column);
        activeSheet.autoSizeColumn(column);


    }

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

} 
user3070123
  • 159
  • 2
  • 20
0

may not be exactly related, but you can try this:

        sheet.trackAllColumnsForAutoSizing();
    for (int i = 0; i < 11; i++) {
        sheet.autoSizeColumn(i);
    }

("i" should be less than your column numbers)