Как создать файл Excel в Java с помощью шва - PullRequest
0 голосов
/ 16 февраля 2012

Я использую шов 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();

    }
}

я звонюиз внешнего интерфейса, как это

, но не загружается, если загружается, показывает размер как 0 байтов

, пожалуйста, предоставьте любую помощь

Ответы [ 2 ]

1 голос
/ 17 февраля 2012

Шов имеет встроенную поддержку Excel, вы смотрели на это? Генерация Excel производится из вашего xhtml и выложена почти точно так же, как и для данных. http://docs.jboss.org/seam/2.2.0.CR1/reference/en-US/html/excel.html

1 голос
/ 16 февраля 2012

Когда вы записываете его в FileOutputStream, вы просто записываете его в файл; ничего не отправлено пользователю. Вам необходимо выписать содержимое файла в HttpResponse. Проверьте это: http://www.coolinterview.com/interview/26167/

...