Bug 23958

Summary: Cannot get a the template feature to work when writing to an excel file.
Product: POI Reporter: mjmoore
Component: POI OverallAssignee: POI Developers List <dev>
Status: RESOLVED WORKSFORME    
Severity: major    
Priority: P3    
Version: 2.0-pre3   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description mjmoore 2003-10-20 22:05:34 UTC
I am trying to create a footer with formulas to set the printable area and 
footer text when printing the an excel report.  When I implemented using a 
template to satisfy this requirement, the excel sheet started being created as 
blank.  There is data being put into the object, but when I got to write that 
data to the excel sheet, it comes out as blank.  Here is some sample code.  
Please let me know if I am doing something wrong here.

public void generate(LevelOfProcessing aLevel, String excelFileTemplate, String 
excelFileName)
            throws ReportGenerationException {

        //clear out instance variables so this method
        //can be called multiple times for the same payroll file
        initialize();

        this.level = aLevel;
        //this.excelFileTemplate = excelFileTemplate;

        createSpreadsheet();
        createHeader();
        
        writeExcelFile(excelFileName);

    }

 private void createSpreadsheet() {
        if (this.excelFileTemplate != null) {
            if (logging.isInfoEnabled()) {
                logging.info("Reading from excel template file " + 
this.excelFileTemplate);
            }
             
         try {

	     InputStream templateStream = 
ClasspathResourceLocator.getResourceStream(this.excelFileTemplate);
             this.workbook = new ExcelWorkbook(templateStream);
             this.workbook = new ExcelWorkbook();
             this.sheet = this.workbook.getSheet(REPORT_NAME);
             templateStream.close();
	    } catch (FileNotFoundException fnfe) {
                if (logging.isDebugEnabled()) {
                    logging.debug("Unable to read from file " + 
this.excelFileTemplate +
                            ".  Creating a new workbook.");
                }
                this.workbook = new ExcelWorkbook();
                this.sheet = this.workbook.createSheet(REPORT_NAME);

            } catch (IOException ioe) {
                if (logging.isDebugEnabled()) {
                    logging.debug("Unable to read from file " + 
this.excelFileTemplate +
                            ". Creating a new workbook.");
                }
                this.workbook = new ExcelWorkbook();
                this.sheet = this.workbook.createSheet(REPORT_NAME);

            } catch (ResourceNotFoundException rnfe) {
                if (logging.isDebugEnabled()) {
                    logging.debug("Unable to find file " + 
this.excelFileTemplate +
                            ". Creating a new workbook.");
                }
                this.workbook = new ExcelWorkbook();
                this.sheet = this.workbook.createSheet(REPORT_NAME);

            }
        } else {
            if (logging.isInfoEnabled()) {
                logging.info("Excel template file not specified.  Creating a 
new workbook.");
            }

            this.workbook = new ExcelWorkbook();
            this.sheet = this.workbook.createSheet(REPORT_NAME);

        }

        this.sheet.setColumnWidth((short) 0, (short) (15 * 256));
        this.sheet.setColumnWidth((short) 1, (short) (12 * 256));
        this.sheet.setColumnWidth((short) 2, (short) (10 * 256));
        this.sheet.setColumnWidth((short) 3, (short) (50 * 256));
        this.sheet.setColumnWidth((short) 4, (short) (13 * 256));
        this.sheet.setColumnWidth((short) 5, (short) (35 * 256));
        this.sheet.setColumnWidth((short) 6, (short) (35 * 256));
        this.sheet.setFitToPage(true);
    }
 private void createHeader() {

        row = this.sheet.createRow(HEADER_ROW);
        ExcelCell cell = row.getOrCreateCell(HEADER_COLUMN);
        cell.setCellTypeString();
        cell.setCellValue(STRONG);
        cell.setCellStyle(getTitleStyle());

        row = this.sheet.createRow((short) (HEADER_ROW + 1));
        cell = row.getOrCreateCell(HEADER_COLUMN);
        cell.setCellTypeString();
        cell.setCellValue(REPORT_NAME);
        cell.setCellStyle(getTitleStyle());

    }

private void writeExcelFile(String excelFileName)
            throws ReportGenerationException {

        Long payrollKey = payrollData.getPayrollFile().getId().getLongValue
("payroll_file_key");
        Long levelKey = this.level.getId().getLongValue("level_key");

        try {
            if (logging.isInfoEnabled()) {
                logging.info("Writing edit report for payroll file key " + 
payrollKey.toString() +
                        " and level key " + levelKey.toString() +
                        " to file " + excelFileName);
            }
            FileOutputStream out = new FileOutputStream(excelFileName);

            this.workbook.write(out);
            out.close();

        } catch (FileNotFoundException fnfe) {
            if (logging.isDebugEnabled()) {
                logging.debug("Unable to write to file " + excelFileName +
                        " for payroll file key " + payrollKey.toString() +
                        " and level key " + levelKey.toString(),
                        fnfe);
            }
            throw new ReportGenerationException(fnfe, "Unable to write edit 
report to " + excelFileName);

        } catch (IOException ioe) {
            if (logging.isDebugEnabled()) {
                logging.debug("Unable to write to file " + excelFileName +
                        " for payroll file key " + payrollKey.toString() +
                        " and level key " + levelKey.toString(),
                        ioe);

            }
            throw new ReportGenerationException(ioe, "Unable to write edit 
report to " + excelFileName);
        }
    }
Comment 1 Yegor Kozlov 2008-12-30 09:57:10 UTC
I can't reproduce the problem from its description. The sample code isn’t of much help because it uses custom wrapper around HSSF and it makes impossible to compile and run.

Try the latest POI-3.5-beta4 or download daily builds from http://encore.torchbox.com/poi-svn-build/

If the problem is still there, re-open the bug and attach a unit test to reproduce it.

Yegor
Comment 2 David Fisher 2009-02-18 09:29:30 UTC
We've done headers and footers and they seem to work. Yegor tried as much as possible with the sample code.