您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關java中export方法如何實現導出excel文件,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
//導出文件接口 public String export(){ return this.myExport(exportList); }
private String myExport(List<BusinessDept> list){ com.bronzesoft.power.tools.json.JSONObject info = new com.bronzesoft.power.tools.json.JSONObject(); try{ List<String> headList = new ArrayList<String>(Arrays.asList("年", "月", "部門","部門負責人","經營值","收入", "支出","填報工時","標準工時","經營參數")); Commonutil.export(list,headList,"部門經營總覽導出"); info = Commonutil.setInfo(info,"部門經營總覽導出"); }catch (Exception e){ LogUtil.error(this.getClass().getName()+".exportExcel()", e); } return info.toString(); }
首先,理解一下一個Excel的文件的組織形式,一個Excel文件對應于一個workbook(XSSFWorkbook),一個workbook可以有多個sheet(XSSFSheet)組成,一個sheet是由多個row(XSSFRow)組成,一個row是由多個cell(XSSFCell)組成。
public static <T> boolean export(List<T> list,List<String> headList,String fileName){ return myExport(list,headList,fileName,com.bronzesoft.rdm.platform.util.Constants.PATH + com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR ); } //將list導出為excel,文件名為fileName public static <T> boolean myExport(List<T> list,List<String> headList,String fileName,String path){ try{ if(!checkListAndHead(list,headList)){ LogUtil.info( "head的長度有問題,導出的文件不正確" ); } File file = new File(path + File.separator + fileName + ".xlsx"); if(!file.exists()) { file.createNewFile(); } //創建對應excel文件,存儲路徑path待確定 XSSFWorkbook workBook = new XSSFWorkbook(); //創建一個excel的sheet頁 XSSFSheet sheet = workBook.createSheet(fileName); XSSFRow row = null; XSSFCell cell = null; Map<String, CellStyle> styles = StyleUtil.createStyles(workBook); //表頭樣式 CellStyle headCenterIndex = styles.get("headCenter"); //列的樣式居中,背景顏色為白色 CellStyle center = styles.get("centerWhite"); row = sheet.createRow(0); //表頭數據 for (int i = 0; i < headList.size(); i++) { //創建列 cell = row.createCell(i); //設置列的value cell.setCellValue(headList.get(i)); //設置列的樣式 cell.setCellStyle(headCenterIndex); } //表格數據 //寫入表格數據 String codeName = ""; int rownum = 1; T obj = null; for(int i = 0; i < list.size(); i++){ obj = list.get(i); if(null != obj){ Field[] fields = obj.getClass().getDeclaredFields(); row = sheet.createRow(rownum); for(int j = 0; j< fields.length; j++){ fields[j].setAccessible(true); codeName = String.valueOf(fields[j].get(obj)); //創建第j列 cell = row.createCell(j); cell.setCellValue(Commonutil.getStringVal(codeName)); cell.setCellStyle(center); } } rownum++; } // //i代表列,設置列的寬度 // for (int i = 0; i < headList.size(); i++) { // if(i == 0){ // sheet.setColumnWidth(i, 2000); // }else if(i == 1 || i == 3){ // sheet.setColumnWidth(i, 6000); // }else{ // sheet.setColumnWidth(i, 4000); // } // } //將文件寫到臨時目錄 FileOutputStream out = new FileOutputStream(file); workBook.write(out); }catch (Exception e){ LogUtil.error(fileName + "export失敗" ); } return true; } //檢查head的size是否符合規范 public static <T> boolean checkListAndHead(List<T> list,List<String> headList){ if(list.size()>0){ T t = list.get(0); if(getColumnCount(t) != headList.size()){ LogUtil.info( "head的長度有問題" ); return false; } } return true; } //獲取一個對象成員變量的個數 public static <T> int getColumnCount(T t){ Field[] fields = t.getClass().getDeclaredFields(); int count = fields.length; return count; }
public static JSONObject setInfo(JSONObject info,String fileName) throws Exception { Storage s = Commonutil.getDefaultStorage(); info.put("port", String.valueOf(s.getPort())); info.put("dirAddress", Base64Util.encode(com.bronzesoft.rdm.platform.util.Constants.PATH)); info.put("address", Base64Util.encode(com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR + File.separator + fileName + ".xlsx")); info.put("name", Base64Util.encode(fileName)); info.put("extendName", "xlsx"); return info; }
function doExport(){ synAjax.tabCall("com.bronzesoft.rdm.SeDeptTotalTab", "export", null, function(data){ doDownload(data); }); } function doDownload(data){ console.log('data',data) r = eval('('+data+')'); $("#_file_dir").val(r.dirAddress); $("#_file_address").val(r.address); $("#_file_name").val(r.name); $("#_file_extname").val(r.extendName); if ($.browser.safari) { $("#_file_downform").attr("target", ""); } var servlet = powerPath + "download"; $("#_file_downform").attr("action", servlet).submit(); }
關于“java中export方法如何實現導出excel文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。