最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaWeb中導(dǎo)出excel文件的簡單方法

 更新時(shí)間:2016年10月14日 21:10:35   投稿:jingxian  
下面小編就為大家?guī)硪黄狫avaWeb中導(dǎo)出excel文件的簡單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

在平時(shí)做系統(tǒng)項(xiàng)目時(shí),經(jīng)常會需要做導(dǎo)出功能,不論是導(dǎo)出excel,還是導(dǎo)出cvs文件。我下面的demo是在springmvc的框架下實(shí)現(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)鍵詞", "價(jià)格","搜索熱度","推廣評分","購買熱度","曝光量","點(diǎn)擊量","點(diǎn)擊率","推廣時(shí)長","花費(fèi)","平均點(diǎn)擊花費(fèi)","匹配產(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 ?"暫停":"啟動(dòng)");
    } 

return wb;
}

這樣之后就可以直接點(diǎn)擊導(dǎo)出就有效果了。

以上就是小編為大家?guī)淼腏avaWeb中導(dǎo)出excel文件的簡單方法全部內(nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

最新評論

淅川县| 福鼎市| 墨脱县| 伊通| 洪洞县| 高阳县| 金门县| 怀仁县| 桐乡市| 文成县| 海南省| 侯马市| 东宁县| 普兰店市| 三台县| 广南县| 合阳县| 潼南县| 定安县| 临江市| 和静县| 隆昌县| 曲阜市| 青岛市| 梁河县| 登封市| 马边| 汝城县| 临西县| 黑水县| 克山县| 东台市| 南溪县| 宣化县| 黑龙江省| 太仓市| 湖南省| 黄大仙区| 龙川县| 昭通市| 西青区|