java导出excel资料
java导出excel文件
public String exportDispatchExcel() { String path=this.contextPvd.getRequest().getRealPath("/file/excel"); List<TblKepuoaRecordDispatch> list = (List<TblKepuoaRecordDispatch>) this.getOamessage().getAlluserList(); TblKepuoaRecordDispatch dispatch=new TblKepuoaRecordDispatch(); String file=""; try { file = createExcel(path,"发文列表",list,dispatch); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }//开始创建excel表 filedownPath=path+File.separator+file;//执行下载操作 return "downLoadDispatch"; } /** * 导出excel文件 * @param path 导出的路径 * @param filename 导出文件的名字 * @param list 要导出的对象list * @param study TblKepuoaStudy实体类 对象 * @return * @throws Exception */ public String createExcel(String path,String filename,List list,TblKepuoaRecordDispatch dispatch) throws Exception{ File file = new File(path+File.separator+filename+".xls" ); WritableWorkbook wbook = null; try { wbook = Workbook.createWorkbook(file); // 建立excel文件 String tmptitle = filename; // 标题 WritableSheet wsheet = wbook.createSheet(filename, 0); // sheet名称 // 设置excel标题 WritableFont wfont = new WritableFont(WritableFont.createFont("宋体"), 13, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); WritableCellFormat wcfFC = new WritableCellFormat(wfont); wsheet.addCell(new Label(4, 0, tmptitle, wcfFC)); wfont = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 10,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); wcfFC = new WritableCellFormat(wfont); wcfFC.setBorder(Border.NONE,BorderLineStyle.NONE); // 开始生成主体内容 wsheet.addCell(new Label(0, 1, "序号",wcfFC)); wsheet.addCell(new Label(1, 1, "文件名称",wcfFC)); wsheet.addCell(new Label(2, 1, "经办人",wcfFC)); wsheet.addCell(new Label(3, 1, "发文日期",wcfFC)); wsheet.addCell(new Label(4, 1, "收文机关",wcfFC)); wsheet.addCell(new Label(5, 1, "档案提供者",wcfFC)); int j = 2; for (int i = 0; i < list.size(); i++) { dispatch=(TblKepuoaRecordDispatch) list.get(i); String uptime=""; if(dispatch.getDispatchDate()!=null&&!"".equals(dispatch.getDispatchDate())){ uptime=sdf2.format(dispatch.getDispatchDate());//时间格式化 } wsheet.addCell(new Label(0, j, i+1+"",wcfFC)); wsheet.addCell(new Label(1, j,dispatch.getDispatchName(),wcfFC)); if(dispatch!=null && dispatch.getDispatchOpreator()!=null && !"".equals(dispatch.getDispatchOpreator())){ if(dispatch.getCode()!=null && !"".equals(dispatch.getCode())){ wsheet.addCell(new Label(2, j,dispatch.getDispatchOpreator()+"-"+dispatch.getCode(),wcfFC)); }else{ wsheet.addCell(new Label(2, j,dispatch.getDispatchOpreator(),wcfFC)); } } wsheet.addCell(new Label(3, j, uptime,wcfFC)); wsheet.addCell(new Label(4, j,dispatch.getDispatchOrgan(),wcfFC)); wsheet.addCell(new Label(5, j, dispatch.getRecordSupportPeople(),wcfFC)); j++; } } catch (IOException e) { // TODO Auto-generated catch block Loggers.info("DispatchAction 中 createExcel 方法:"+e.getMessage()); e.printStackTrace(); }finally{ if(wbook != null){ wbook.write(); // 写入文件 wbook.close(); } } // 主体内容生成结束 return filename+".xls"; }