Java POI组件实现导出

1. 导入依赖:

    <!-- POI相关依赖 -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
    </dependency>

2. 实体类

package com.ssm.bean;


import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class Users {

    private Long uid;
    private String uno;
    private String uname;
    private String password;
    private String sex;
    private Integer age;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;
    private String phone;
    private String address;
    private Long type;
    private String uhead;

    public Users() {
    }

   //实现Constructor、getter、setter、toString()
}

3. 业务层使用POI组件导出

//导出,不需要返回值,用ajax会出现问题,ids为前端传过来需要导出数据对于的id集合
    @RequestMapping("/export/{ids}")
    public void export(@PathVariable("ids") Integer[] ids,HttpServletRequest request , HttpServletResponse response){

        System.out.println("导出操作!");
        System.out.println(Arrays.toString(ids));
        //根据id查询到数据
        List<Users> users = userService.getBatchUserById(ids);
        for (Users user : users) {
            System.out.println(user);
        }

        //1.创建一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2.为这个工作簿创建一个sheet
        HSSFSheet sheet = workbook.createSheet("用户信息表");
        //创建表头
        HSSFRow titlerow = sheet.createRow(0);
        HSSFCell titlecell1 = titlerow.createCell(0);
        titlecell1.setCellValue("序号");
        HSSFCell titlecell2 = titlerow.createCell(1);
        titlecell2.setCellValue("编号");
        HSSFCell titlecell3 = titlerow.createCell(2);
        titlecell3.setCellValue("姓名");
        sheet.getHeader();
        for (int i = 0; i < users.size(); i++) {
            //从list中获取一个对象
            Users user = users.get(i);
            //创建一行
            HSSFRow row = sheet.createRow(i + 1);
            //以下分别为每一行设置多个单元格,并为单元格设置值
            HSSFCell idCell = row.createCell(0);
            idCell.setCellValue(user.getUid());
            HSSFCell unoCell = row.createCell(1);
            unoCell.setCellValue(user.getUno());
            HSSFCell unameCell = row.createCell(2);
            unameCell.setCellValue(user.getUname());
        }

        String fileName="用户信息表_"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".xls";

//        //向本地存储!
//        try {
//            OutputStream os = new FileOutputStream(new File("D:\\" + fileName));
//            workbook.write(os);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        //生成Excel并提供下载
        try {
            ServletOutputStream outputStream = response.getOutputStream();//浏览器输出字节流
            workbook.write(outputStream);

            String userAgent=request.getHeader("User-Agent");
            if(userAgent.contains("Safari")){
                response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8")) ;
            }else{
                //设置请求头以附件的形式打开
                response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1")) ;
            }
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

4. 效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值