JXL导出Excel文件
/** * 导出为Excel * @param cdosCategoryKeyWords * @return */ public boolean exportExcel(CDO[] cdosCategoryKeyWords) { HttpServletResponse response=ServletActionContext.getResponse(); OutputStream os=null; try { os = response.getOutputStream(); } catch (IOException e1) { mallLog.error("获取response输出流出错"); } 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.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); WritableCellFormat wcfFC = new WritableCellFormat(wfc); Label lId2HeadLabel = new Label(0,0,"编号ID",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.setColumnView(2, 50); ws.addCell(strCategoryConfigHeadLabel); ws.addCell(dtModifyTimeHeadLabel); Label lId2Label = null; Label strKeyWordLabel = null; Label strCategoryConfigLabel = null; Label dtModifyTimeLabel = null; for(int i=1;i<=cdosCategoryKeyWords.length;i++) { String lId2 = cdosCategoryKeyWords[i-1].getStringValue("lId"); //ID String strKeyWord = cdosCategoryKeyWords[i-1].getStringValue("strKeyWord"); //关键词 String strCategoryConfig = cdosCategoryKeyWords[i-1].getStringValue("strCategoryConfig");//类目 String dtModifyTime = cdosCategoryKeyWords[i-1].getStringValue("dtModifyTime");//更新时间 lId2Label = new Label(0,i,lId2); strKeyWordLabel = new Label(1,i,strKeyWord); strCategoryConfigLabel = new Label(2,i,strCategoryConfig); dtModifyTimeLabel = new Label(3,i,dtModifyTime); ws.addCell(lId2Label); ws.addCell(strKeyWordLabel); ws.addCell(strCategoryConfigLabel); ws.addCell(dtModifyTimeLabel); } } catch (Exception e) { mallLog.error("输出Excel失败"); return false; } finally { try { wwb.write(); wwb.close(); os.close(); } catch (WriteException e) { mallLog.error("关闭WritableWorkbook出错"); } catch (IOException e) { mallLog.error("关闭WritableWorkbook出错"); } } return true; }