POI导出excel(Java完整版代码)

1.pom.xml引入依赖

   <dependency>
       <groupId>org.apache.poi</groupId>
       <artifactId>poi</artifactId>
       <version>3.6</version>
   </dependency>

2.页面发送请求

window.location.href = “/exportInfoList?”+param;

3.后台Controller接收请求

//导出列表
    @RequestMapping(value = "exportInfoList")
    public void exportInfoList(String param, HttpServletResponse response) {
        OutputStream outputStream=null;
        try {
            HSSFWorkbook workbook=  service.exportExcel(param);
            outputStream = response.getOutputStream();
            response.reset();
            response.setContentType("application/msexcel");
            //response.setCharacterEncoding("utf-8");
            //设置浏览器响应头对应的Content-disposition 解决中文名字乱码
            String filename = "XX管理";
            response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes("gbk"), "iso8859-1")+".xls");
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

4.Service方法

  //XX管理导出excel
    public HSSFWorkbook exportExcel(String param){
        HSSFWorkbook workbook=new HSSFWorkbook();
       try {
           OutputStream outputStream = null;
           List<JpCustomerInfo> list = mapper.selectListByPage(param);
           HSSFSheet sheet = workbook.createSheet("XX管理");
           Row row = sheet.createRow(0);
           // excle标题
           String[] ar = new String[]{"姓名", "身份证号", "渠道来源", "手机号", "省份", "app是否登录",  "注册时间"};
           for (int i = 0; i < ar.length; i++) {
               Cell cell = row.createCell(i);
               cell.setCellValue(ar[i]);
               sheet.setColumnWidth(i,ar[i].getBytes().length*350);
           }
           //excel 内容
           for (int s = 0; s < list.size(); s++) {
               JpCustomerInfo info = list.get(s);
               row = sheet.createRow(s + 1);
               row.createCell(0).setCellValue(info.getName());
               row.createCell(1).setCellValue(info.getIdNumber());
               row.createCell(2).setCellValue(info.getChannelName());
               row.createCell(3).setCellValue(info.getPhone());
               row.createCell(4).setCellValue(info.getProvince()); //省份
               row.createCell(5).setCellValue(info.getAppIsLogin() == 0 ? "是" : "否"); //app是否登录
               Date date =info.getRegisterTime();
               if (date==null){
                   row.createCell(6).setCellValue(""); //注册时间
               }else{
                   String time=DateFormatUtils.format(date,"yyyy-MM-dd HH-mm-ss");
                   row.createCell(6).setCellValue(time); //注册时间
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
           return workbook;
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值