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     }

 

posted on 2019-06-05 14:14  你猜怎么着  阅读(229)  评论(0编辑  收藏  举报

导航