3

I'm triying to convert a xssfWorkbook file to a SXSSF because I'm generating a big report, but the problem is that I cannot convert, what I'm currently trying is this example.

public void export(Workbook hssfWorkbook, Map<String, Object> model) throws Exception {
    XSSFWorkbook workbook = (XSSFWorkbook)hssfWorkbook;
    Sheet sheet = workbook.getSheet(this.excelSheetName);
    try {
    this.initCommonCellStyles(workbook);
    List<ItemsToBillReport> itemsObject= (List<ItemsToBillReport>)model.get("the model name");

    CellStyle boldCellStyle = workbook.createCellStyle();
    this.setCellStyleBoldFont(workbook, boldCellStyle);
    new HSSFColor.GREY_40_PERCENT();
    boldCellStyle.setFillForegroundColor(GREY_40_PERCENT.index);
    boldCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

    Row bdmRow = sheet.getRow(0);
    Cell bdmCell = bdmRow.getCell(0);
    Bdm bdm = (Bdm)model.get("anoder model");
    if(bdm != null && bdm.getLastName()!=null && bdm.getFirstName()!=null){
        bdmCell.setCellValue("BDM : "+bdm.getLastName()+", "+bdm.getFirstName());
    }else{
        bdmCell.setCellValue("BDM : -");
    }
    bdmCell.setCellStyle(boldCellStyle);

    Row billDateRow = sheet.getRow(1);
    Cell billDateCell = billDateRow.getCell(0);
    String dateFrom = (String)model.get("date from model");
    String dateTo = (String)model.get("dateTo");
    billDateCell.setCellValue("title "+ dateFrom +" TO "+dateTo);
    billDateCell.setCellStyle(boldCellStyle);

    Row dateTimeRow = sheet.getRow(2);
    Cell dateTimeCell = dateTimeRow.getCell(0);
    Format formatter;
    final Date date = new Date();
    formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    String dateString = formatter.format(date);
    dateTimeCell.setCellValue("AS OF : "+dateString);
    dateTimeCell.setCellStyle(boldCellStyle);

    boolean includeFullData = (Boolean)model.get(thisExporter.INCLUDE_FULL_DATA);
    boolean isBriefView = (Boolean)model.get(thisExporter.ATTRIBUTE_ITB_BRIEF_VIEW);

    SXSSFWorkbook wb = new SXSSFWorkbook(workbook,1000);
    sheet = wb.createSheet(this.excelSheetName);
    final int totalColNum = this.printItemsToBillListDetails(sheet, itemsObject, includeFullData, isBriefView);
    //this.autosizeColumns(sheet, totalColNum);
    }catch(Exception e){
        Exception h = e;
    }

}

As you can see, I'm generating the report with a template file because there is some data that goes at the begining, when I try to convert to SXSSF at the beggining, it cannot be done because if I understood correctly, SXSSF has to write in an empty space, since this is a template it can't write it there.

So I tried to convert after the first rows but is not printing anything, it generates the first rows with the info but at the moment to write the data it writes nothing, so, how can I convert ot SXSSF using a template like this example of mine?

Thank you for your time, I just can't find an answer.

Ivan Cuellar
  • 31
  • 1
  • 2
  • 2
    The problem with your question is that the provided code is not complete. It contains as much unknown parts that nobody will be able reproducing what you are trying to do. Exact the reason why we need [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of code in questions. – Axel Richter Nov 04 '18 at 06:07
  • 1
    But from your description and the code parts I suspect you have not really understand what `SXSSFWorkbook wb = new SXSSFWorkbook(workbook,1000);` is doing. It creates a `SXSSFWorkbook` from the given `XSSFWorkbook workbook`. This `SXSSFWorkbook` then contains all parts of the given `XSSFWorkbook workbook` already. The already present parts cannot be accessed in `SXSSFWorkbook` but they are there. And new parts (sheets, rows, cells) can be added in `SXSSFWorkbook`. – Axel Richter Nov 04 '18 at 06:09
  • So the sheet named `this.excelSheetName` would must be already there in your `SXSSFWorkbook wb` if your code above the creation runs without exceptions. But after that you are creating that sheet new using `sheet = wb.createSheet(this.excelSheetName);`. No later than now an exception would must be thrown because a workbook cannot contain two sheets having the same name. – Axel Richter Nov 04 '18 at 06:12
  • But I suspect you simply don't get informed about exceptions because your `catch` part simply does nothing with the exceptions it got. So maybe even the first parts of your code have thrown exceptions but you knows nothing about that. So at first you should printing the exceptions stack trace somewhere using `e.printStackTrace()` in your `catch` block. – Axel Richter Nov 04 '18 at 06:12
  • Yea, my catch is not right, I get what you told me but then, how can I convert to SXSSF using a template? – Ivan Cuellar Nov 04 '18 at 17:18
  • Because the public void export(Workbook hssfWorkbook, Map model) throws Exception { has a template that i'm using – Ivan Cuellar Nov 04 '18 at 17:19
  • Please do providing a minimal, complete, and verifiable example of code in your question as I told in my first comment. And if exceptions are thrown, then please show us the complete stack trace. Else we cannot help. – Axel Richter Nov 04 '18 at 17:32
  • Sorry if I'm not showing all of the code, but the rest of the code is just the creation of the cells and the setCellValue, that is just all the work of the .printItemsToBillListDetails( – Ivan Cuellar Nov 04 '18 at 19:22
  • 1
    And actually the code works, the problem is that is not printing the info in the excel file, I'm not having exception when running the code, I just need to know how to convert from XSSF to SXSSF, because the way i'm doing it is not writing anything in the file – Ivan Cuellar Nov 04 '18 at 19:23

0 Answers0