java 导出excel windows和linux服务器 通用

public boolean exportClassChargeExcel(List 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 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;
}

你可能感兴趣的:(java)