首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用seam在java中生成excel文件

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

Stack Overflow用户
提问于 2012-02-16 17:54:32
回答 2查看 1.2K关注 0票数 0

我正在使用Seam2.2.0,我想动态生成excel并下载该excel文件。

有人能拿到密码吗?这是我的密码

代码语言:javascript
复制
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();

    }
}

我是这样从前台打来的

但如果它被下载,则不会被下载,它的大小显示为0字节

请提供任何帮助

EN

回答 2

Stack Overflow用户

发布于 2012-02-16 21:17:41

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

票数 1
EN

Stack Overflow用户

发布于 2012-02-17 05:03:45

Seam内置了对excel的支持,你看过了吗?excel的生成是从您的xhtml完成的,其布局几乎与datatable完全相同。http://docs.jboss.org/seam/2.2.0.CR1/reference/en-US/html/excel.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9309013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档