Java導出Excel文件

 1  /**
 2  * 導出 用get請求  3  * @param response  4  * @param
 5  * @throws IOException  6      */
 7     @RequestMapping(value = "/download")  8     public void download(HttpServletResponse response,HttpServletRequest request) throws IOException {  9 
10         int quality = UtilReq.parseInt(request.getParameter("quality"), -1); 11         String status = UtilReq.parseString(request.getParameter("status"),""); 12         String mobile = UtilReq.parseString(request.getParameter("mobile"),""); 13         long agentUid = UtilReq.parseLong(request.getParameter("agentUid"),0l); 14         HSSFWorkbook workbook = aritstService.download(quality,status,mobile,agentUid); 15 
16         //設置要導出的文件的名字
17         String fileName=""; 18         Date now = new Date(); 19 
20         fileName = "藝人數據" + UtilDate.formatDate2Str(now,"yyyy年MM月dd日HH時mm分ss秒") + ".xls"; 21 
22         fileName = new String(fileName.getBytes("gbk"), "iso-8859-1"); 23         response.setContentType("application/octet-stream"); 24         response.setHeader("Content-disposition", "attachment;filename=" + fileName); 25  response.flushBuffer(); 26  workbook.write(response.getOutputStream()); 27     }
 1 /**
 2  * 導出  3  * @param quality  4  * @param status  5  * @param mobile  6  * @param agentUid  7  * @return
 8      */
 9     public HSSFWorkbook download(int quality, String status, String mobile, long agentUid) { 10         //建立
11         HSSFWorkbook wb = new HSSFWorkbook(); 12         //headers表示excel表中第一行的表頭
13         String[] headers = {"姓名","邀請碼","所屬經紀人","經紀人手機號", "總收入金額", "接單數", "成單數","滿意度","優質藝人"}; 14         //建立一個只有頭信息的空表
15         HSSFSheet sheet = null; 16         sheet = wb.createSheet("藝人數據"); 17 
18         //headers表示excel表中第一行的表頭
19         HSSFRow row = sheet.createRow(0); 20 
21         //在excel表中添加表頭
22         for(int i=0;i<headers.length;i++){ 23             HSSFCell cell = row.createCell(i); 24             HSSFRichTextString text = new HSSFRichTextString(headers[i]); 25  cell.setCellValue(text); 26  } 27 
28         //查詢藝人列表,根據篩選條件
29         List<Integer> statusList = null; 30         if ("".equals(status)){ 31             status = "1,2"; 32  } 33         if (StringUtils.isNotEmpty(status)) { 34             statusList = new ArrayList<>(); 35             String[] statuss = status.split(","); 36             for (String statu : statuss) { 37  statusList.add(Integer.parseInt(statu)); 38  } 39  } 40         //須要導出的數據
41         List<Aritst> aritsts = aritstDao.selectArtistByAgentAndMobile(statusList, mobile, agentUid,quality, null,null,null); 42 
43         int rowNum = 1; 44         if (null != aritsts) { 45             //在表中存放查詢到的數據放入對應的列
46             for (Aritst aritst : aritsts) { 47                 HSSFRow row1 = sheet.createRow(rowNum); 48                 //"姓名","邀請碼","所屬經紀人","經紀人手機號", "總收入金額", "接單號", "成單數","滿意度","是否優質藝人"
49 
50                 row1.createCell(0).setCellValue(aritst.getNickname()); 51                 row1.createCell(1).setCellValue(aritst.getInviteCode()); 52                 row1.createCell(2).setCellValue(aritst.getAgent()); 53                 row1.createCell(3).setCellValue(aritst.getAgentMobile()); 54                 row1.createCell(4).setCellValue(aritst.getRevenue()); 55                 row1.createCell(5).setCellValue(aritst.getConnectionnum()); 56                 row1.createCell(6).setCellValue(aritst.getServernum()); 57                 row1.createCell(7).setCellValue(aritst.getSatisfiedRate()); 58                 row1.createCell(8).setCellValue(aritst.getQuality()==1?"是":"否"); 59                 rowNum++; 60  } 61  } 62         return wb; 63     }
相關文章
相關標籤/搜索