JavaWeb中導(dǎo)出excel文件的簡單方法
在平時做系統(tǒng)項目時,經(jīng)常會需要做導(dǎo)出功能,不論是導(dǎo)出excel,還是導(dǎo)出cvs文件。我下面的demo是在springmvc的框架下實現(xiàn)的。
1.JS中只需要用GET模式請求導(dǎo)出就可以了:
$('#word-export-btn').parent().on('click',function(){ var promotionWord = JSON.stringify($('#mainForm').serializeObject()); location.href="${ctx}/promotionWord/export?promotionWord="+promotionWord; });
2.在controller中要做的是將文件以數(shù)據(jù)流格式輸出:
@RequestMapping("/export") public void export(HttpSession session, String promotionWord, HttpServletRequest request, HttpServletResponse response) throws IOException { User sessionUser = (User) session.getAttribute("user"); JSONObject jsonObj = JSONObject.parseObject(promotionWord); HSSFWorkbook wb = promotionWordService.export(sessionUser.getId(), jsonObj); response.setContentType("application/vnd.ms-excel"); Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String fileName = "word-" + sdf.format(cal.getTime()) + ".xls"; response.setHeader("Content-disposition", "attachment;filename=" + fileName); OutputStream ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStream.flush(); ouputStream.close(); }
3.在service中需要將數(shù)據(jù)寫入到格式文件中:
public HSSFWorkbook export(String userId, JSONObject jsonObj) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("word"); HSSFRow row = sheet.createRow(0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); List<PromotionWord> pWordList; Map<String, Object> map = new HashMap<>(); map.put("userId", userId); map.put("checkExistRule", jsonObj.getString("checkExistRule")); map.put("status", jsonObj.getString("status")); map.put("qsStar", jsonObj.getString("qsStar")); map.put("impressionCount", jsonObj.getString("impressionCount")); map.put("selectGroupId", jsonObj.getString("selectGroupId")); map.put("isCheck", jsonObj.getString("isCheck")); map.put("word", jsonObj.getString("word")); Long impression = jsonObj.getLong("impressionCount"); Long click = jsonObj.getLong("clickCount"); if(impression != null){ PromotionWord word = new PromotionWord(); word.setCreatedBy(userId); word.setImpressionCount7(impression); pWordList = getTwentyPercentlists(word); if(pWordList != null && pWordList.size() > 0){ map.put("impressionCount", pWordList.get(pWordList.size()-1).getImpressionCount()); }else{ map.put("impressionCount", 1); } }else if(click != null){ PromotionWord word = new PromotionWord(); word.setCreatedBy(userId); word.setClickCount7(click); pWordList = getTwentyPercentlists(word); if(pWordList != null && pWordList.size() > 0){ map.put("clickCount", pWordList.get(pWordList.size()-1).getClickCount()); }else{ map.put("clickCount", 1); } } List<PromotionWord> list = commonDao.queryList(PROMOTION_WORD_DAO + ".queryExportDataByUser", map); String[] excelHeader = {"關(guān)鍵詞", "價格","搜索熱度","推廣評分","購買熱度","曝光量","點擊量","點擊率","推廣時長","花費","平均點擊花費","匹配產(chǎn)品數(shù)","預(yù)估排名","狀態(tài)"}; for (int i = 0; i < excelHeader.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellValue(excelHeader[i]); cell.setCellStyle(style); if(i == 0){ sheet.setColumnWidth(0, 30*256); }else{ sheet.setColumnWidth(i, 10*256); } } if(list != null && list.size() > 0) for (int i = 0; i < list.size(); i++) { row = sheet.createRow(i + 1); PromotionWord word = list.get(i); row.createCell(0).setCellValue(word.getWord()); row.createCell(1).setCellValue(word.getPrice()+""); row.createCell(2).setCellValue(word.getSearchCount()); row.createCell(3).setCellValue(word.getQsStar()); row.createCell(4).setCellValue(word.getBuyCount()); row.createCell(5).setCellValue(word.getImpressionCount7()); row.createCell(6).setCellValue(word.getClickCount7()); if(word.getClickCount7() == 0L){ row.createCell(7).setCellValue("0.00%"); }else{ DecimalFormat df = new DecimalFormat("0.00%"); row.createCell(7).setCellValue(df.format((Double.valueOf(word.getClickCount7())/Double.valueOf(word.getImpressionCount7())))); } row.createCell(8).setCellValue(word.getOnlineTime7()); row.createCell(9).setCellValue(word.getCost7()+""); row.createCell(10).setCellValue(word.getAvgCost7()+""); row.createCell(11).setCellValue(word.getMatchCount()); String rank = ""; if(word.getMatchCount() != null && word.getMatchCount() != 0){ if(word.getProspectRank() == null || word.getProspectRank() == 0L){ rank = "其他位置"; }else{ rank = "第"+word.getProspectRank()+"位"; } }else{ rank = "---"; } row.createCell(12).setCellValue(rank); row.createCell(13).setCellValue(word.getStatus() == 1 ?"暫停":"啟動"); } return wb; }
這樣之后就可以直接點擊導(dǎo)出就有效果了。
以上就是小編為大家?guī)淼腏avaWeb中導(dǎo)出excel文件的簡單方法全部內(nèi)容了,希望大家多多支持腳本之家~
- java常用工具類之Excel操作類及依賴包下載
- java實現(xiàn)excel導(dǎo)入數(shù)據(jù)的工具類
- 在java poi導(dǎo)入Excel通用工具類示例詳解
- java實現(xiàn)的導(dǎo)出Excel工具類實例
- Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實例
- JavaWeb導(dǎo)出Excel文件并彈出下載框
- JavaWeb動態(tài)導(dǎo)出Excel可彈出下載
- java導(dǎo)出數(shù)據(jù)庫的全部表到excel
- Java根據(jù)模板導(dǎo)出Excel報表并復(fù)制模板生成多個Sheet頁
- Java使用excel工具類導(dǎo)出對象功能示例
相關(guān)文章
SpringBoot環(huán)境Druid數(shù)據(jù)源使用及特點
Druid 是目前比較流行的高性能的,分布式列存儲的OLAP框架(具體來說是MOLAP)。本文給大家分享SpringBoot環(huán)境Druid數(shù)據(jù)源使用及特點介紹,感興趣的朋友跟隨小編一起看看吧2021-07-07關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題
這篇文章主要介紹了關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題及解決方法,在這里 springboot中已經(jīng)內(nèi)嵌了上傳圖片的依賴包,因此不需要再添加額外依賴,具體實現(xiàn)代碼跟隨小編一起看看吧2021-04-04Java獲取Jar、War包路徑并生成可編輯修改的本地配置文件
這篇文章主要給大家介紹了關(guān)于Java如何獲取Jar、War包路徑并生成可編輯修改的本地配置文件,文中通過代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01