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. }
發佈了42 篇原創文章 · 獲贊 37 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章