java 匯出excel windows和linux伺服器 通用
阿新 • • 發佈:2019-02-12
public boolean exportClassChargeExcel(List<ReceptionLog> costManageList) {
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = null;
try {
os = response.getOutputStream();
} catch (IOException e1) {
}
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String curDatetime = sdf.format(cal.getTime());
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=CategoryKeyWords" + curDatetime + ".xls");
response.setContentType("application/msexcel");
WritableWorkbook wwb = null;
WritableSheet ws = null;
try {
wwb = Workbook.createWorkbook(os);
ws = wwb.createSheet("訪問統計日誌", 0);
ws.getSettings().setDefaultColumnWidth(15); // 建立表頭
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.ORANGE);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label lId2HeadLabel = new Label(0, 0, "序號", wcfFC);
Label strKeyWordHeadLabel = new Label(1, 0, "操作時間", wcfFC);
Label strCategoryConfigHeadLabel = new Label(2, 0, "欄目", wcfFC);
Label dtModifyTimeHeadLabel = new Label(3, 0, "機頂盒卡", wcfFC);
ws.addCell(lId2HeadLabel);
ws.addCell(strKeyWordHeadLabel);
ws.addCell(strCategoryConfigHeadLabel);
ws.addCell(dtModifyTimeHeadLabel);
Label semesterNameLabel = null;
Label projectNameLabel = null;
Label childNameLabel = null;
Label startDateLabel = null;
for (int i = 0; i <costManageList.size(); i++) {
String className = ""+costManageList.get(i).getId(); // ID
String childNum= costManageList.get(i).getOpertingtime();
String sk=costManageList.get(i).getColumnname();
String tk=costManageList.get(i).getBoxcard();
semesterNameLabel = new Label(0, i+1, className);
projectNameLabel = new Label(1, i+1, ""+childNum);
childNameLabel = new Label(2, i+1, ""+sk);
startDateLabel = new Label(3, i+1, ""+tk);
ws.addCell(semesterNameLabel);
ws.addCell(projectNameLabel);
ws.addCell(childNameLabel);
ws.addCell(startDateLabel);
}
} catch (Exception e) {
return false;
} finally {
try {
wwb.write();
wwb.close();
os.close();
} catch (WriteException e) {
} catch (IOException e) {
}
}
return true;
}
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = null;
try {
os = response.getOutputStream();
} catch (IOException e1) {
}
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String curDatetime = sdf.format(cal.getTime());
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=CategoryKeyWords" + curDatetime + ".xls");
response.setContentType("application/msexcel");
WritableWorkbook wwb = null;
WritableSheet ws = null;
try {
wwb = Workbook.createWorkbook(os);
ws = wwb.createSheet("訪問統計日誌", 0);
ws.getSettings().setDefaultColumnWidth(15); // 建立表頭
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.ORANGE);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label lId2HeadLabel = new Label(0, 0, "序號", wcfFC);
Label strKeyWordHeadLabel = new Label(1, 0, "操作時間", wcfFC);
Label strCategoryConfigHeadLabel = new Label(2, 0, "欄目", wcfFC);
Label dtModifyTimeHeadLabel = new Label(3, 0, "機頂盒卡", wcfFC);
ws.addCell(lId2HeadLabel);
ws.addCell(strKeyWordHeadLabel);
ws.addCell(strCategoryConfigHeadLabel);
ws.addCell(dtModifyTimeHeadLabel);
Label semesterNameLabel = null;
Label projectNameLabel = null;
Label childNameLabel = null;
Label startDateLabel = null;
for (int i = 0; i <costManageList.size(); i++) {
String className = ""+costManageList.get(i).getId(); // ID
String childNum= costManageList.get(i).getOpertingtime();
String sk=costManageList.get(i).getColumnname();
String tk=costManageList.get(i).getBoxcard();
semesterNameLabel = new Label(0, i+1, className);
projectNameLabel = new Label(1, i+1, ""+childNum);
childNameLabel = new Label(2, i+1, ""+sk);
startDateLabel = new Label(3, i+1, ""+tk);
ws.addCell(semesterNameLabel);
ws.addCell(projectNameLabel);
ws.addCell(childNameLabel);
ws.addCell(startDateLabel);
}
} catch (Exception e) {
return false;
} finally {
try {
wwb.write();
wwb.close();
os.close();
} catch (WriteException e) {
} catch (IOException e) {
}
}
return true;
}