Search code examples
javaexcelservletsjxls

jXML stream result via servlet without saving to disk


I use jXLS to create a report from template like that:

List report = new ArrayList();
report.add(new ReportItem(12,"item name 1"));
report.add(new ReportItem(421,"item name 2"));
report.add(new ReportItem(53,"item 3"));

Map beans = new HashMap();
beans.put("report", report);
XLSTransformer transformer = new XLSTransformer();

//transformer.groupCollection("report.rn");

transformer.transformXLS(templateFileName, beans, destFileName);

How can I stream the resulting file (destFileName) without saving it to disk?


Solution

  • I think you can use the following API to just create a workbook. It won't create any file.

    /**
     * Creates Workbook instance based on .xls template from a given InputStream and a number of beans
     *
     * @param is         xls InputStream with required
     * @param beanParams Beans in a map under keys used in .xls template to access to the beans
     * @return new {@link org.apache.poi.ss.usermodel.Workbook} generated by inserting beans into corresponding excel template
     * @throws net.sf.jxls.exception.ParsePropertyException if there were any problems in evaluating specified property value from a bean
     */
    public org.apache.poi.ss.usermodel.Workbook transformXLS(InputStream is, Map beanParams) throws ParsePropertyException, InvalidFormatException {
        org.apache.poi.ss.usermodel.Workbook hssfWorkbook = null;
        try {
            hssfWorkbook = WorkbookFactory.create(is);
            transformWorkbook(hssfWorkbook, beanParams);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return hssfWorkbook;
    }