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队 | ||||||
---|---|---|---|---|---|---|---|
队长 | 张三 | ||||||
姓名 | 学校 | 专业 | 学号 | 入学年份 | 电话 | 邮箱地址 | |
张三 | xx | xx | xx | xx | xx | xx | |
队伍名称 | ZZ队 | ||||||
队长 | 李四 | ||||||
姓名 | 学校 | 专业 | 学号 | 入学年份 | 电话 | 邮箱地址 | |
李四 | yy | yy | yy | yy | yy | yy | |
… | |||||||