如何使用seam在java中生成excel文件

时间:2012-02-16 09:54:32

标签: java seam

我正在使用seam 2.2.0我想动态生成excel并下载那个excel文件

请任何人都有代码 这是我的代码

public void getWriteExcelFile() {
    try {

        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("new sheet");
        HSSFCellStyle cellStyle = setHeaderStyle(hwb);

        HSSFRow rowhead1 = sheet.createRow((short) 0);
        HSSFCell cell = rowhead1.createCell((short) 4);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(new HSSFRichTextString(
                "Vizag Seaport Private Limited"));

        HSSFRow rowdata1 = sheet.createRow(3);
        rowdata1.createCell(2).setCellValue(
                "Computation Of Storage Charges");

        HSSFRow rowhead = sheet.createRow((short) 5);
        HSSFRow row = sheet.createRow((short) 5);
        HSSFRow rowhead2 = sheet.createRow((short) 6);
        HSSFRow row2 = sheet.createRow(6);

        HSSFRow rowhead3 = sheet.createRow((short) 7);
        HSSFRow row3 = sheet.createRow((short) 7);
        HSSFRow rowhead4 = sheet.createRow((short) 8);
        HSSFRow row4 = sheet.createRow((short) 8);

        HSSFCell cell1 = rowhead.createCell((short) 2);
        cell1.setCellStyle(cellStyle);
        cell1.setCellValue(new HSSFRichTextString("Party Name:"));
        row.createCell((short) 3).setCellValue(
                itStorageInvoice.getItImportCustomDetail()
                        .getIcPartyByBLParty().getPartyName());

        HSSFCell cell2 = rowhead.createCell((short) 7);
        cell2.setCellStyle(cellStyle);
        cell2.setCellValue(new HSSFRichTextString("Free Period:"));
        row.createCell((short) 8).setCellValue(
                itStorageInvoice.getFreeDays());
        System.out.println("-------------------------freedays-"
                + itStorageInvoice.getFreeDays());

        HSSFCell cell3 = rowhead2.createCell((short) 2);
        cell3.setCellStyle(cellStyle);
        cell3.setCellValue(new HSSFRichTextString("Cargo Stacker:"));

        row2.createCell((short) 3).setCellValue(
                itStorageInvoice.getItBlLdg().getItBlLdgDetails().get(0)
                        .getIcCommodity().getCommodityCode());
        System.out.println("--------------cargostacker---------"
                + itStorageInvoice.getItBlLdg().getItBlLdgDetails().get(0)
                        .getIcCommodity().getCommodityCode());

        HSSFCell cell4 = rowhead2.createCell((short) 7);
        cell4.setCellStyle(cellStyle);
        cell4.setCellValue(new HSSFRichTextString("Date Of Sailing:"));
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        row2.createCell(8).setCellValue(
                sdf.format(itStorageInvoice.getItVoyage().getLastRope()));
        System.out.println("--------date--------"
                + itStorageInvoice.getItVoyage().getLastRope());

        HSSFCell cell5 = rowhead3.createCell((short) 2);
        cell5.setCellStyle(cellStyle);
        cell5.setCellValue(new HSSFRichTextString("Vessel:"));
        row3.createCell((short) 3).setCellValue(
                itStorageInvoice.getItVoyage().getImVessel()
                        .getVesselName());

        HSSFCell cell6 = rowhead3.createCell((short) 7);
        cell6.setCellStyle(cellStyle);
        cell6.setCellValue(new HSSFRichTextString("BL # :"));
        row3.createCell((short) 8).setCellValue(
                itStorageInvoice.getItImportCustomDetail().getBlNumber());

        HSSFCell cell7 = rowhead4.createCell((short) 2);
        cell7.setCellStyle(cellStyle);
        cell7.setCellValue(new HSSFRichTextString("Tonnage"));
        row4.createCell((short) 3).setCellValue(
                itStorageInvoice.getItBlLdg().getItBlLdgDetails().get(0)
                        .getWeight().doubleValue()
                        + " " + "TONNE");

        /*
         * HSSFCell cell8= rowhead4.createCell((short) 7);
         * cell8.setCellStyle(cellStyle); cell8.setCellValue(new
         * HSSFRichTextString("Free Time Upto")); row4.createCell((short)
         * 8).setCellValue(itStorageInvoice.getItBlLdg().getFreePeriod());
         */

        HSSFRow rowhead5 = sheet.createRow((short) 10);
        CellStyle style = hwb.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT
                .getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);

        HSSFCell cell8 = rowhead5.createCell(2);
        cell8.setCellStyle(style);
        cell8.setCellValue("DATE");

        HSSFCell cell9 = rowhead5.createCell(3);
        cell9.setCellStyle(style);
        cell9.setCellValue("Bal.Appx.Qty");

        HSSFCell cell10 = rowhead5.createCell(4);
        cell10.setCellStyle(style);
        cell10.setCellValue("RATE");

        HSSFCell cell11 = rowhead5.createCell(5);
        cell11.setCellStyle(style);
        cell11.setCellValue("AMOUNT");

        FileOutputStream fileOut = new FileOutputStream(new File(this
                .getExcelFileName()));
        hwb.write(fileOut);
        fileOut.close();
        System.out.println("Your excel file has been generated!");

    } catch (Exception ex) {
        System.out.println(ex);
        ex.printStackTrace();

    }
}

我从前端打电话就像这样

但如果下载则不会下载,显示大小为0bytes

请提供任何帮助

2 个答案:

答案 0 :(得分:1)

当您将其写入FileOutputStream时,您只是将其写入文件;没有任何内容发送给用户。您需要将文件的内容写出到HttpResponse。看看这个:http://www.coolinterview.com/interview/26167/

答案 1 :(得分:1)

Seam内置了excel支持,你看过那个吗? excel生成是从你的xhtml完成的,并且布局几乎完全像数据表。 http://docs.jboss.org/seam/2.2.0.CR1/reference/en-US/html/excel.html