繁体   English   中英

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

[英]how to generate the excel file in java using 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,则不会下载

请提供任何帮助

当您将其写入FileOutputStream时,您只是在将其写入文件。 没有任何内容发送给用户。 您需要将文件内容写出到HttpResponse。 检查一下: http : //www.coolinterview.com/interview/26167/

Seam内置了excel支持,您看过吗? excel生成是通过xhtml完成的,并且布局几乎完全像数据表一样。 http://docs.jboss.org/seam/2.2.0.CR1/reference/zh-CN/html/excel.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM