在 SpringBoot 中使用 POI 导出 Excel 文件
前言
一、POI 简介:
Apache POI 是 Apache 软件基金会的开放源码函式库,POI 提供 API 给 Java 程序对 Microsoft Office 格式档案读和写的功能。
HSSF 是 Horrible SpreadSheet Format(可怕的电子表格格式)的缩写,通过 HSSF,你可以用纯 Java 代码来读取、写入、修改 Excel 文件。
HSSF 为读取操作提供了两类 API:usermodel 和 eventusermodel,即“用户模型”和“事件-用户模型”。
二、POI 结构说明
包名称说明
-
HSSF 提供读写 Microsoft Excel XLS 格式档案的功能。
-
XSSF 提供读写 Microsoft Excel OOXML XLSX 格式档案的功能。
-
HWPF 提供读写 Microsoft Word DOC 格式档案的功能。
-
HSLF 提供读写 Microsoft PowerPoint 格式档案的功能。
-
HDGF 提供读 Microsoft Visio 格式档案的功能。
-
HPBF 提供读 Microsoft Publisher 格式档案的功能。
-
HSMF 提供读 Microsoft Outlook 格式档案的功能。
三、POI 常用类说明
类名 | 说明 |
---|---|
HSSFWorkbook | Excel的文档对象 |
HSSFSheet | Excel的表单 |
HSSFRow | Excel的行 |
HSSFCell | Excel的格子单元 |
HSSFFont | Excel字体 |
HSSFDataFormat | 格子单元的日期格式 |
HSSFHeader | Excel文档Sheet的页眉 |
HSSFFooter | Excel文档Sheet的页脚 |
HSSFCellStyle | 格子单元样式 |
HSSFDateUtil | 日期 |
HSSFPrintSetup | 打印 |
HSSFErrorConstants | 错误信息表 |
POI 的使用
添加依赖
在 maven 仓库中(https://mvnrepository.com/),可以搜索 POI ,并复制依赖到 SpringBoot 项目下的 pom.xml 文件中
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
基本用法
下面是一个导出员工信息表的示例:
后端代码
1. 写接口
@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {
@Autowired
EmployeeService employeeService;
......
@GetMapping("/export")
public ResponseEntity<byte[]> exportData(){
List<Employee> list = (List<Employee>) employeeService.getEmployeeByPage(null,null,null).getData();
return POIUtils.employee2Excel(list);
}
}
2. 写导出 Excel 的方法
public class POIUtils {
public static ResponseEntity<byte[]> employee2Excel(List<Employee> list) {
// 1. 创建一个 Excel 文档
HSSFWorkbook workbook = new HSSFWorkbook();
// 2. 创建文档摘要
workbook.createInformationProperties();
// 3. 获取并设置文档信息
DocumentSummaryInformation docInfo = workbook.getDocumentSummaryInformation();
docInfo.setCategory("员工信息");
docInfo.setCompany("浙江西瓜科技股份有限公司");
docInfo.setManager("爱吃西瓜的番茄酱");
docInfo.setDocumentVersion("0.1");
// 4. 获取并设置摘要信息
SummaryInformation summaryInformation = workbook.getSummaryInformation();
summaryInformation.setTitle("员工信息表");
summaryInformation.setAuthor("爱吃西瓜的番茄酱");
summaryInformation.setComments("本文档由 xxx 提供");
// 5. 创建表单
HSSFSheet sheet = workbook.createSheet("员工信息表");
// 5.1 创建并设置样式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 5.2 创建并设置日期格式
HSSFCellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
// 6. 创建第一行 标题行
HSSFRow r0 = sheet.createRow(0);
// 7. 创建第一列,并设置第一列的列名和样式
HSSFCell c0 = r0.createCell(0);
c0.setCellValue("编号");
c0.setCellStyle(headerStyle);
// 8. 类似,创建第2-24列
HSSFCell c1 = r0.createCell(1);
c1.setCellStyle(headerStyle);
c1.setCellValue("姓名");
HSSFCell c2 = r0.createCell(2);
c2.setCellStyle(headerStyle);
c2.setCellValue("性别");
HSSFCell c3 = r0.createCell(3);
c3.setCellStyle(headerStyle);
c3.setCellValue("工号");
HSSFCell c4 = r0.createCell(4);
c4.setCellStyle(headerStyle);
c4.setCellValue("出生日期");
HSSFCell c5 = r0.createCell(5);
c5.setCellStyle(headerStyle);
c5.setCellValue("身份证号码");
HSSFCell c6 = r0.createCell(6);
c6.setCellStyle(headerStyle);
c6.setCellValue("婚姻");
HSSFCell c7 = r0.createCell(7);
c7.setCellStyle(headerStyle);
c7.setCellValue("民族");
HSSFCell c8 = r0.createCell(8);
c8.setCellStyle(headerStyle);
c8.setCellValue("籍贯");
HSSFCell c9 = r0.createCell(9);
c9.setCellStyle(headerStyle);
c9.setCellValue("政治面貌");
HSSFCell c10 = r0.createCell(10);
c10.setCellStyle(headerStyle);
c10.setCellValue("邮箱");
HSSFCell c11 = r0.createCell(11);
c11.setCellStyle(headerStyle);
c11.setCellValue("电话号码");
HSSFCell c12 = r0.createCell(12);
c12.setCellStyle(headerStyle);
c12.setCellValue("住址");
HSSFCell c13 = r0.createCell(13);
c13.setCellStyle(headerStyle);
c13.setCellValue("所属部门");
HSSFCell c14 = r0.createCell(14);
c14.setCellStyle(headerStyle);
c14.setCellValue("职称");
HSSFCell c15 = r0.createCell(15);
c15.setCellStyle(headerStyle);
c15.setCellValue("职位");
HSSFCell c16 = r0.createCell(16);
c16.setCellStyle(headerStyle);
c16.setCellValue("聘用形式");
HSSFCell c17 = r0.createCell(17);
c17.setCellStyle(headerStyle);
c17.setCellValue("最高学历");
HSSFCell c18 = r0.createCell(18);
c18.setCellStyle(headerStyle);
c18.setCellValue("专业");
HSSFCell c19 = r0.createCell(19);
c19.setCellStyle(headerStyle);
c19.setCellValue("毕业院校");
HSSFCell c20 = r0.createCell(20);
c20.setCellStyle(headerStyle);
c20.setCellValue("入职日期");
HSSFCell c21 = r0.createCell(21);
c21.setCellStyle(headerStyle);
c21.setCellValue("转正日期");
HSSFCell c22 = r0.createCell(22);
c22.setCellStyle(headerStyle);
c22.setCellValue("合同起始日期");
HSSFCell c23 = r0.createCell(23);
c23.setCellStyle(headerStyle);
c23.setCellValue("合同截止日期");
HSSFCell c24 = r0.createCell(24);
c24.setCellStyle(headerStyle);
c24.setCellValue("合同期限(年)");
// 从第二行开始,对每个单元格赋值。
for (int i = 0; i < list.size(); i++) {
Employee emp = list.get(i);
HSSFRow row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(emp.getId());
row.createCell(1).setCellValue(emp.getName());
row.createCell(2).setCellValue(emp.getGender());
row.createCell(3).setCellValue(emp.getWorkID());
HSSFCell cell4 = row.createCell(4);
cell4.setCellStyle(dateCellStyle);
cell4.setCellValue(emp.getBirthday());
row.createCell(5).setCellValue(emp.getIdCard());
row.createCell(6).setCellValue(emp.getWedlock());
row.createCell(7).setCellValue(emp.getNation().getName());
row.createCell(8).setCellValue(emp.getNativePlace());
row.createCell(9).setCellValue(emp.getPoliticsstatus().getName());
row.createCell(10).setCellValue(emp.getEmail());
row.createCell(11).setCellValue(emp.getPhone());
row.createCell(12).setCellValue(emp.getAddress());
row.createCell(13).setCellValue(emp.getDepartment().getName());
row.createCell(14).setCellValue(emp.getJobLevel().getName());
row.createCell(15).setCellValue(emp.getPosition().getName());
row.createCell(16).setCellValue(emp.getEngageForm());
row.createCell(17).setCellValue(emp.getTiptopDegree());
row.createCell(18).setCellValue(emp.getSpecialty());
row.createCell(19).setCellValue(emp.getSchool());
HSSFCell cell20 = row.createCell(20);
cell20.setCellStyle(dateCellStyle);
cell20.setCellValue(emp.getBeginDate());
HSSFCell cell21 = row.createCell(21);
cell21.setCellStyle(dateCellStyle);
cell21.setCellValue(emp.getConversionTime());
HSSFCell cell22 = row.createCell(22);
cell22.setCellStyle(dateCellStyle);
cell22.setCellValue(emp.getBeginContract());
HSSFCell cell23 = row.createCell(23);
cell23.setCellStyle(dateCellStyle);
cell23.setCellValue(emp.getEndContract());
row.createCell(24).setCellValue(emp.getContractTerm());
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
HttpHeaders headers = new HttpHeaders();
try {
headers.setContentDispositionFormData("attachment", new String("员工表.xls".getBytes("UTF-8"), "ISO-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
workbook.write(stream);
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseEntity<byte[]>(stream.toByteArray(), headers, HttpStatus.CREATED);
}
}
前端代码
1. 定义点击事件
<el-button @click="exportData" size="small" type="success"><i class="fa fa-level-up" aria-hidden="true"></i> 导出数据</el-button>
2. 调用后端接口
methods: {
exportData: function(){
window.open("/employee/basic/export", "_parent");
},
...
}
导出结果如下图:
参考文章
https://blog.csdn.net/w893932747/article/details/89354979
每天学习一点点,每天进步一点点。