代码思路说明
原来项目中也是有导出的方法,自己觉得不太满意,就自己重新写了一个公用方法。这种类似的方法很多,这里只是自己的一个案例。
对于导出,应该有的参数是 要导出的字段、数据列表以、excel的表头及文件名。这里主要实现了分sheet页导出。
先看看代码吧: Action中确定了导出的参数及查询好了数据列表,直接调用excelExport方法,捕获相关异常即可,不需要多余的操作。
/*** jxl导出excel方法* @author 巩浩 2016-7-7 下午2:17:47* @param fileName 文件名* @param heads 字段名称(例:{姓名,年龄})* @param headsStr 字段(例:{name,age})* @param dataList 数据列表*/@SuppressWarnings("rawtypes")public static void excelExport(String fileName,String[] heads,String[] headsStr,List dataList) throws Exception {WritableWorkbook book=null ;OutputStream os = null;try{HttpServletResponse response = ServletActionContext.getResponse();os = response.getOutputStream();response.reset();response.setHeader("Content-disposition", "attachment; filename=\""+ URLEncoder.encode(fileName,"UTF-8")+ "_"+System.currentTimeMillis() + ".xls\"");response.setContentType("application/msexcel; charset=utf-8");if (dataList != null && dataList.size() > 0) {book = createWorkBookWithStyle(os, dataList, heads, headsStr);} else {book = Workbook.createWorkbook(os);WritableSheet sheet = book.createSheet("日志信息", 0);// 指定单元格位置(如:第一列第一行(0, 0))以及单元格内容为(如:小明)for (int i = 0; i < heads.length; i++) {Label cell = new Label(i, 0, heads[i]);// 将定义好的单元格添加到工作表中sheet.addCell(cell);}}book.write();}catch(Exception e){e.printStackTrace();}finally{if(book!=null){book.close();}if(os!=null){os.close();}}}
createWorkBookWithStyle方法:这里只是给表头字段加红,这个方法只是用了简单的样式WritableFont,更多的大家可以自己去研究研究,讲道理导出文件没必要那么花哨。
/*** 根据数据列表及参数输出工作薄* @author 巩浩 2016-7-7 下午2:32:38* @param os 输出流* @param dataList 数据列表* @param heads 字段名称* @param headsStr 字段* @throws IOException* @throws WriteException* @throws RowsExceededException* @throws NoSuchMethodException* @throws InvocationTargetException* @throws IllegalAccessException*/@SuppressWarnings("rawtypes")public static WritableWorkbook createWorkBookWithStyle(OutputStream os, List dataList, String[] heads, String[] headsStr) throws IOException, RowsExceededException, WriteException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {//根据数据大小具体分n个sheet页,默认一页存储1000条数据int sizeLoop = dataList.size();//数据大小int size = dataList.size();if(sizeLoop < 1000){sizeLoop = 1000;}int sheetSize = 1000;int loopSize = sizeLoop/sheetSize;if(sizeLoop%sheetSize!=0){loopSize+=1;}//设置样式WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); // 定义格式 字体 下划线 斜体 粗体 颜色WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色WritableCellFormat wcf_head = new WritableCellFormat(wf_head); // 单元格定义wcf_head.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式WritableCellFormat wcf_table = new WritableCellFormat(wf_table);//创建一个工作薄WritableWorkbook ws = Workbook.createWorkbook(os);//分别往每个sheet页写数据for(int l = 0;l<loopSize;l++){WritableSheet sheet = ws.createSheet("第"+(l+1)+"页", l);for(int i=0;i<heads.length;i++){Label cell = new Label(i,0, heads[i],wcf_head);sheet.addCell(cell );}//循环读取数据列表int n = 1;for(int i=l*sheetSize;i<(l+1)*sheetSize && i<=size-1;i++){Object vrd = dataList.get(i);for(int j = 0;j<headsStr.length;j++){Object value = PropertyUtils.getProperty(vrd, headsStr[j]);if(ObjectUtil.isAllObjectsNotNull(value)){sheet.setColumnView(j, value.toString().length()+10);sheet.addCell(new Label(j,n,value.toString(),wcf_table));}}n++;}}return ws;}