jsf資料轉存成EXCEL
public void toExportXls() {
if (mainDataLists.size() > 0) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("invoiceNo");
row.createCell(1).setCellValue("waybillNo");
row.createCell(2).setCellValue("Seq");
row.createCell(3).setCellValue("containerNo");
row.createCell(4).setCellValue("materialName");
row.createCell(5).setCellValue("type");
row.createCell(6).setCellValue("weight");
row.createCell(7).setCellValue("containerNoBarcode");
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("Free 3 of 9 Extended");//字型名稱
font.setFontHeight((short)800); //字體大小
style.setFont(font);
int i = 1;
for (InPackingListInputBean n : mainDataLists) {
row = sheet.createRow(i);
row.createCell(0).setCellValue(n.getInvoiceNo());
row.createCell(1).setCellValue(n.getWaybillNo());
row.createCell(2).setCellValue(n.getSeq());
row.createCell(3).setCellValue(n.getContainerNo());
row.createCell(4).setCellValue(n.getMaterialName());
row.createCell(5).setCellValue(n.getType());
row.createCell(6).setCellValue(n.getWeight());
HSSFCell cell7 = row.createCell(7);
cell7.setCellStyle(style);//設定儲存格字型
cell7.setCellValue(n.getContainerNo());
i++;
}
row.setRowStyle(style);
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getResponse();
res.setContentType("application/vnd.ms-excel");
res.setHeader("Content-disposition", "attachment;filename=" + mainDataLists.get(0).getInvoiceNo().toString().replace("/", "").replace("-", "") + ".xls");
ServletOutputStream out = res.getOutputStream();
wb.write(out);
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
留言
張貼留言