程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JavaWeb中導出excel文件的簡略辦法

JavaWeb中導出excel文件的簡略辦法

編輯:關於JAVA

JavaWeb中導出excel文件的簡略辦法。本站提示廣大學習愛好者:(JavaWeb中導出excel文件的簡略辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaWeb中導出excel文件的簡略辦法正文


在日常平凡做體系項目時,常常會須要做導出功效,豈論是導出excel,照樣導出cvs文件。我上面的demo是在springmvc的框架下完成的。

1.JS中只須要用GET形式要求導出便可以了:

$('#word-export-btn').parent().on('click',function(){
		var promotionWord = JSON.stringify($('#mainForm').serializeObject());
		location.href="${ctx}/promotionWord/export?promotionWord="+promotionWord; 
});

2.在controller中要做的是將文件以數據流格局輸入:

  @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中須要將數據寫入到格局文件中:

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 = {"症結詞", "價錢","搜刮熱度","推行評分","購置熱度","暴光量","點擊量","點擊率","推行時長","消費","均勻點擊消費","婚配產物數","預估排名","狀況"};
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;
}

如許以後便可以直接點擊導出就有用果了。

以上就是小編為年夜家帶來的JavaWeb中導出excel文件的簡略辦法全體內容了,願望年夜家多多支撐~

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved