JXL实现excel导出案例(分sheet页)

代码思路说明

原来项目中也是有导出的方法,自己觉得不太满意,就自己重新写了一个公用方法。这种类似的方法很多,这里只是自己的一个案例。
对于导出,应该有的参数是 要导出的字段、数据列表以、excel的表头及文件名。这里主要实现了分sheet页导出。
先看看代码吧: Action中确定了导出的参数及查询好了数据列表,直接调用excelExport方法,捕获相关异常即可,不需要多余的操作。

 
 
  1. /**
  2. * jxl导出excel方法
  3. * @author 巩浩 2016-7-7 下午2:17:47
  4. * @param fileName 文件名
  5. * @param heads 字段名称(例:{姓名,年龄})
  6. * @param headsStr 字段(例:{name,age})
  7. * @param dataList 数据列表
  8. */
  9. @SuppressWarnings("rawtypes")
  10. public static void excelExport(String fileName,String[] heads,String[] headsStr,List dataList) throws Exception {
  11. WritableWorkbook book=null ;
  12. OutputStream os = null;
  13. try{
  14. HttpServletResponse response = ServletActionContext.getResponse();
  15. os = response.getOutputStream();
  16. response.reset();
  17. response.setHeader("Content-disposition", "attachment; filename=\""
  18. + URLEncoder.encode(fileName,"UTF-8")+ "_"+System.currentTimeMillis() + ".xls\"");
  19. response.setContentType("application/msexcel; charset=utf-8");
  20. if (dataList != null && dataList.size() > 0) {
  21. book = createWorkBookWithStyle(os, dataList, heads, headsStr);
  22. } else {
  23. book = Workbook.createWorkbook(os);
  24. WritableSheet sheet = book.createSheet("日志信息", 0);
  25. // 指定单元格位置(如:第一列第一行(0, 0))以及单元格内容为(如:小明)
  26. for (int i = 0; i < heads.length; i++) {
  27. Label cell = new Label(i, 0, heads[i]);
  28. // 将定义好的单元格添加到工作表中
  29. sheet.addCell(cell);
  30. }
  31. }
  32. book.write();
  33. }catch(Exception e){
  34. e.printStackTrace();
  35. }finally{
  36. if(book!=null){
  37. book.close();
  38. }
  39. if(os!=null){
  40. os.close();
  41. }
  42. }
  43. }

createWorkBookWithStyle方法:这里只是给表头字段加红,这个方法只是用了简单的样式WritableFont,更多的大家可以自己去研究研究,讲道理导出文件没必要那么花哨。

 
 
  1. /**
  2. * 根据数据列表及参数输出工作薄
  3. * @author 巩浩 2016-7-7 下午2:32:38
  4. * @param os 输出流
  5. * @param dataList 数据列表
  6. * @param heads 字段名称
  7. * @param headsStr 字段
  8. * @throws IOException
  9. * @throws WriteException
  10. * @throws RowsExceededException
  11. * @throws NoSuchMethodException
  12. * @throws InvocationTargetException
  13. * @throws IllegalAccessException
  14. */
  15. @SuppressWarnings("rawtypes")
  16. public static WritableWorkbook createWorkBookWithStyle(OutputStream os, List dataList, String[] heads, String[] headsStr) throws IOException, RowsExceededException, WriteException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  17. //根据数据大小具体分n个sheet页,默认一页存储1000条数据
  18. int sizeLoop = dataList.size();//数据大小
  19. int size = dataList.size();
  20. if(sizeLoop < 1000){
  21. sizeLoop = 1000;
  22. }
  23. int sheetSize = 1000;
  24. int loopSize = sizeLoop/sheetSize;
  25. if(sizeLoop%sheetSize!=0){
  26. loopSize+=1;
  27. }
  28. //设置样式
  29. WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); // 定义格式 字体 下划线 斜体 粗体 颜色
  30. WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
  31. WritableCellFormat wcf_head = new WritableCellFormat(wf_head); // 单元格定义
  32. wcf_head.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
  33. WritableCellFormat wcf_table = new WritableCellFormat(wf_table);
  34. //创建一个工作薄
  35. WritableWorkbook ws = Workbook.createWorkbook(os);
  36. //分别往每个sheet页写数据
  37. for(int l = 0;l<loopSize;l++){
  38. WritableSheet sheet = ws.createSheet("第"+(l+1)+"页", l);
  39. for(int i=0;i<heads.length;i++){
  40. Label cell = new Label(i,0, heads[i],wcf_head);
  41. sheet.addCell(cell );
  42. }
  43. //循环读取数据列表
  44. int n = 1;
  45. for(int i=l*sheetSize;i<(l+1)*sheetSize && i<=size-1;i++){
  46. Object vrd = dataList.get(i);
  47. for(int j = 0;j<headsStr.length;j++){
  48. Object value = PropertyUtils.getProperty(vrd, headsStr[j]);
  49. if(ObjectUtil.isAllObjectsNotNull(value)){
  50. sheet.setColumnView(j, value.toString().length()+10);
  51. sheet.addCell(new Label(j,n,value.toString(),wcf_table));
  52. }
  53. }
  54. n++;
  55. }
  56. }
  57. return ws;
  58. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值