gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:58:13 24 4
gpt4 key购买 nike

我使用的是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();

}
}

我是这样从前端调用的

但没有下载,下载后显示大小为0字节

请提供任何帮助

最佳答案

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

关于java - 如何使用java在java中使用seam生成excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9309013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com