java中的export方法實現(xiàn)導(dǎo)出excel文件
1.export函數(shù)
//導(dǎo)出文件接口 public String export(){ return this.myExport(exportList); }
2.導(dǎo)出列名
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("年", "月", "部門","部門負責人","經(jīng)營值","收入", "支出","填報工時","標準工時","經(jīng)營參數(shù)")); Commonutil.export(list,headList,"部門經(jīng)營總覽導(dǎo)出"); info = Commonutil.setInfo(info,"部門經(jīng)營總覽導(dǎo)出"); }catch (Exception e){ LogUtil.error(this.getClass().getName()+".exportExcel()", e); } return info.toString(); }
3.export實現(xiàn)方法
?首先,理解一下一個Excel的文件的組織形式,一個Excel文件對應(yīng)于一個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導(dǎo)出為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的長度有問題,導(dǎo)出的文件不正確" ); } File file = new File(path + File.separator + fileName + ".xlsx"); if(!file.exists()) { file.createNewFile(); } //創(chuàng)建對應(yīng)excel文件,存儲路徑path待確定 XSSFWorkbook workBook = new XSSFWorkbook(); //創(chuàng)建一個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); //表頭數(shù)據(jù) for (int i = 0; i < headList.size(); i++) { //創(chuàng)建列 cell = row.createCell(i); //設(shè)置列的value cell.setCellValue(headList.get(i)); //設(shè)置列的樣式 cell.setCellStyle(headCenterIndex); } //表格數(shù)據(jù) //寫入表格數(shù)據(jù) 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)); //創(chuàng)建第j列 cell = row.createCell(j); cell.setCellValue(Commonutil.getStringVal(codeName)); cell.setCellStyle(center); } } rownum++; } // //i代表列,設(shè)置列的寬度 // 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是否符合規(guī)范 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; } //獲取一個對象成員變量的個數(shù) public static <T> int getColumnCount(T t){ Field[] fields = t.getClass().getDeclaredFields(); int count = fields.length; return count; }
4.前端對接
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; }
5.前端代碼
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(); }
到此這篇關(guān)于java中的export方法實現(xiàn)導(dǎo)出excel文件的文章就介紹到這了,更多相關(guān)java導(dǎo)出excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring cloud gateway 全局過濾器的實現(xiàn)
全局過濾器作用于所有的路由,不需要單獨配置,我們可以用它來實現(xiàn)很多統(tǒng)一化處理的業(yè)務(wù)需求,這篇文章主要介紹了spring cloud gateway 全局過濾器的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03關(guān)于Spring?@Transactional事務(wù)傳播機制詳解
我們?nèi)粘9ぷ髦袠O少使用事務(wù)傳播級別,單純只是使用事務(wù)和rollbackfor拋出異常來解決事務(wù)問題,但其實我們很多時候使用的是不正確的,或者說會造成事務(wù)粒度過大,本文詳解一下事務(wù)傳播級別,也讓自己更好地處理事務(wù)問題,需要的朋友可以參考下2023-08-08IntelliJ IDEA之配置JDK的4種方式(小結(jié))
這篇文章主要介紹了IntelliJ IDEA之配置JDK的4種方式(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10