将XSSFWorkbook转换为SXSSFWorkbook APACHE POI时出现问题

时间:2018-11-03 16:40:06

标签: java excel apache-poi xssf hssfworkbook

我正在尝试将xssfWorkbook文件转换为SXSSF,因为我正在生成一个大报告,但是问题是我无法转换,我目前正在尝试的是此示例。

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

}

如您所见,我正在生成一个带有模板文件的报告,因为开始时会有一些数据,当我尝试从一开始就转换为SXSSF时,就无法完成,因为如果我理解正确, SXSSF必须在空白处写入,因为这是模板,因此无法在其中写入。

因此,我尝试在第一行之后进行转换,但不打印任何内容,它会生成带有信息的第一行,但此刻在写入数据时却什么也没写,因此,如何使用类似模板的ot转换SXSSF我的这个例子吗?

谢谢您的时间,我只是找不到答案。

0 个答案:

没有答案