Java使用jxl.jar匯出Excel例項

xiaosong_2016發表於2018-05-23
public static boolean exportExcel(String sheetName,List<?> list,Map<String, String> mapFields,
      HttpServletResponse response,String path){
   boolean result = false;
   WritableWorkbook wook = null;//可寫的工作薄物件
       Object objClass = null;
       OutputStream out = null;
   try{
      if(path != null && !"".equals(path)){
         File file = new File(path);
         if(!file.exists()){
            file.mkdirs();
         }
         out = new FileOutputStream(file + "\\" + sheetName);
      }else{
         out = response.getOutputStream();
      }
      wook = Workbook.createWorkbook(out);
           //定義格式 字型 下劃線 斜體 粗體 顏色 
      //定義頭樣式
           WritableFont font = new WritableFont(WritableFont.ARIAL, 11,  
                   WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
           WritableCellFormat wcf_title = new WritableCellFormat(font);
           wcf_title.setAlignment(Alignment.CENTRE);
           wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);
           wcf_title.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
           wcf_title.setWrap(false);//不自動換行
           // 定義表格樣式
           WritableFont tableFont = new WritableFont(WritableFont.ARIAL,10,  
                   WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
           WritableCellFormat wcf_table = new WritableCellFormat(tableFont);
           wcf_table.setAlignment(Alignment.CENTRE);
           wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE);
           wcf_table.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
           wcf_table.setWrap(false);//不自動換行
           //建立工作表
           WritableSheet sheet = wook.createSheet(sheetName, 0);
           SheetSettings setting = sheet.getSettings();
           setting.setVerticalFreeze(1);//凍結視窗頭部 
           
           int columnIndex = 0;  //列索引
           List<String> methodNameList = new ArrayList<String>();
           if(mapFields != null){
               String key  = "";
               Map<String,Method> getMap = null;
               Method method = null;
               //開始匯出表格頭部
               for (Iterator<String> i = mapFields.keySet().iterator();i.hasNext();) {
                  key = i.next();
                  /*表格頭樣式匯出*/
                  sheet.setColumnView(columnIndex, 20);//根據內容自動設定列寬
                  sheet.addCell(new Label(columnIndex, 0, mapFields.get(key), wcf_title));
                  //記錄欄位的順序,以便於匯出的內容與欄位不出現偏移
            int size=key.split(" ").length;
            if(size>1){
               methodNameList.add(key.split(" ")[1]);
            }
            else{
               methodNameList.add(key);
            }
                   columnIndex++;
               }
               if(list != null && list.size() > 0){
                  //匯出表格內容
                   for (int i = 0,len = list.size(); i < len; i++) {
                       objClass = list.get(i);
                       //獲得物件所有的get方法
                       getMap = getAllMethod(objClass);
                       //按儲存的欄位順序匯出內容
                       for (int j = 0; j < methodNameList.size(); j++) {
                          //根據key獲取對應方法
                           method = getMap.get("GET" + methodNameList.get(j).toString().toUpperCase());
                           if(method!=null){
                               //從對應的get方法得到返回值
                               if(method.invoke(objClass) != null){
                                  String value = method.invoke(objClass).toString();
                                   //應用wcfc樣式建立單元格
                                   sheet.addCell(new Label(j, i+1, value, wcf_table));
                               }else{
                                  sheet.addCell(new Label(j, i+1, "", wcf_table));
                               }
                           }else{
                               //如果沒有對應的get方法,則預設將內容設為""
                               sheet.addCell(new Label(j, i+1, "", wcf_table));
                           }
                       }
                   }
               }else{
                  System.out.println("匯出表格無資料!");
               }
               result = true;
           }
           wook.write();
   }catch (Exception e) {
      result = false;
      System.out.println("失敗");
   } finally{
           try {
               if(wook!=null){
                   wook.close();
               }
               if(out!=null){
                   out.flush();
                   out.close();
               }
           } catch (Exception e2) {
               e2.printStackTrace();
           }
       }
   return result;
}

相關文章