springboot 使用poi导出excel文件,及注意项

springboot 使用poi导出excel文件,及注意项

1.maven依赖

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

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>

2.excel 不同扩展名导出

// .xlsx 后缀  分别使用这几个类
org.apache.poi.xssf.usermodel.XSSFWorkbook
org.apache.poi.xssf.usermodel.XSSFSheet
org.apache.poi.xssf.usermodel.XSSFRow
org.apache.poi.xssf.usermodel.XSSFCellStyle
//.xls 使用这几个类
org.apache.poi.xssf.usermodel.HSSFWorkbook
org.apache.poi.xssf.usermodel.HSSFSheet
org.apache.poi.xssf.usermodel.HSSFRow
org.apache.poi.xssf.usermodel.HSSFCellStyle

注意:下载文件后,出现无法打开可能是使用类和后缀名对应有误!

3…xlsx 后缀为例 导出文件

public void download(HttpServletResponse response, List<ExportVo> list, String excleName) throws IOException {
    Map<String, String> map = new HashMap<>();
    //记录行数
    AtomicInteger num = new AtomicInteger();
    try (XSSFWorkbook workbook = new XSSFWorkbook()) {
        //创建一个Excel表单,参数为sheet的名字
        XSSFSheet sheet = workbook.createSheet("Sheet1");

        //设置数据
        for (ExportVo e : list) {
            if (!map.containsKey(e.getTeamId())) {
                //清空数据,避免干扰
                map.clear();
                //仅仅做记录
                map.put(e.getTeamId(), "add");
                //创建表头
                setTitle(workbook, sheet, num, e);
            }
            setData(sheet, num, e);
        }
        setResponse(excleName, response, workbook);
    }
}

/**
 * 封装响应
 *
 * @param excleName
 * @param response
 * @param workbook
 * @throws IOException
 */
private void setResponse(String excleName, HttpServletResponse response, XSSFWorkbook workbook) throws IOException {
    //清空response
    response.reset();
    //设置response的Header
    response.addHeader("Content-Disposition", "attachment;filename=" + excleName);
    OutputStream os = new BufferedOutputStream(response.getOutputStream());
    response.setContentType("application/vnd.ms-excel;charset=gb2312");
    //将excel写入到输出流中
    workbook.write(os);
    os.flush();
    os.close();
}

private void setData(XSSFSheet sheet, AtomicInteger num, ExportVo e) {
    XSSFRow row = sheet.createRow(num.getAndAdd(1));
    row.createCell(0).setCellValue(e.getName());
    row.createCell(1).setCellValue(e.getSchool());
    row.createCell(2).setCellValue(e.getMajor());
    row.createCell(3).setCellValue(e.getNumber());
    row.createCell(4).setCellValue(e.getStart());
    row.createCell(5).setCellValue(e.getTel());
    row.createCell(6).setCellValue(e.getMail());
}

private void setTitle(XSSFWorkbook workbook, XSSFSheet sheet, AtomicInteger num, ExportVo e) {

    //设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
    sheet.setColumnWidth(0, 10 * 256);
    sheet.setColumnWidth(1, 20 * 256);
    sheet.setColumnWidth(2, 20 * 256);
    sheet.setColumnWidth(3, 15 * 256);
    sheet.setColumnWidth(4, 10 * 256);
    sheet.setColumnWidth(5, 15 * 256);
    sheet.setColumnWidth(6, 23 * 256);

    //设置为居中加粗
    XSSFCellStyle style = workbook.createCellStyle();
    XSSFFont font = workbook.createFont();
    font.setBold(true);
    style.setFont(font);
    HSSFCellStyle
    //创建一行
    XSSFRow row = sheet.createRow(num.getAndAdd(1));
    //设置队伍属名
    XSSFCell cell;
    cell = row.createCell(0);
    cell.setCellValue("队伍名称");
    cell.setCellStyle(style);
    //设置队伍值
    cell = row.createCell(1);
    cell.setCellValue(e.getTeam());
    cell.setCellStyle(style);

    //创建一行
    XSSFRow row1 = sheet.createRow(num.getAndAdd(1));
    //设置队长属性名
    cell = row1.createCell(0);
    cell.setCellValue("队长");
    cell.setCellStyle(style);
    //设置队长名
    cell = row1.createCell(1);
    cell.setCellValue(e.getName());
    cell.setCellStyle(style);

    //创建一行 添加成员属性,
    XSSFRow row2 = sheet.createRow(num.getAndAdd(1));
    cell = row2.createCell(0);
    cell.setCellValue("姓名");
    cell.setCellStyle(style);

    cell = row2.createCell(1);
    cell.setCellValue("学校");
    cell.setCellStyle(style);

    cell = row2.createCell(2);
    cell.setCellValue("专业");
    cell.setCellStyle(style);

    cell = row2.createCell(3);
    cell.setCellValue("学号");
    cell.setCellStyle(style);

    cell = row2.createCell(4);
    cell.setCellValue("入学年份");
    cell.setCellStyle(style);
    cell = row2.createCell(5);
    cell.setCellValue("电话");
    cell.setCellStyle(style);
    cell = row2.createCell(6);
    cell.setCellValue("邮箱地址");
    cell.setCellStyle(style);
}

注意点:包含中文名时编码处理,否则会乱码等问题,

导出效果图:

队伍名称xx队
队长张三
姓名学校专业学号入学年份电话邮箱地址
张三xxxxxxxxxxxx
队伍名称ZZ队
队长李四
姓名学校专业学号入学年份电话邮箱地址
李四yyyyyyyyyyyy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值