Apache POI是用Java编写的免费开源的跨平台的Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能,其中使用最多的就是使用POI操作Excel文件。
使用前需要导入先导入MAVEN坐标
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
POI结构:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能 --xls
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能--xlsx
HWPF - 提供读写Microsoft Word DOC格式档案的功能
HSLF - 提供读写Microsoft PowerPoint格式档案的功能
HDGF - 提供读Microsoft Visio格式档案的功能
HPBF - 提供读Microsoft Publisher格式档案的功能
HSMF - 提供读Microsoft Outlook格式档案的功能
//这里通过我之前做的一个小项目中的数据库表作为一个例子
通过下面这段代码导出上面数据库中表格式为EXCEL--xlsx
@RestController
@Slf4j
@RequestMapping("/")
public class ExportExcelController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) throws IOException {
//这里通过我只做的一个小项目中的表格作为一个例子
//首先通过service方法查询数据库中表的数据
List<Employee> employeeList = employeeService.list();
//创建工作簿对象
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//创建表格,指定工作簿名称为订单明细
XSSFSheet sheet = xssfWorkbook.createSheet("订单明细");
//XSSFRow row = sheet.createRow(0);//括号中的数值就是行的索引数值,本文采用遍历的方式创建工作簿这一行代码不需要
//将数据库表的字段名放入一个数组中
String[] employee = {"姓名","用户名","密码","电话","性别","状态","下单时间","更新时间"};
for (int i = 0; i < employee.length; i++) {
//通过遍历employee数组给工作簿定义表头
row.createCell(i).setCellValue(employee[i]);
//第一次遍历时工作簿最左上角的一个框中set数据为姓名
}
for (int i = 0; i < employeeList.size(); i++) {
XSSFRow row1 = sheet.createRow(i+1);
row1.createCell(0).setCellValue(employeeList.get(i).getName());
row1.createCell(1).setCellValue(employeeList.get(i).getUsername());
row1.createCell(2).setCellValue(employeeList.get(i).getPassword());
row1.createCell(3).setCellValue(employeeList.get(i).getPhone());
row1.createCell(4).setCellValue(employeeList.get(i).getSex());
row1.createCell(5).setCellValue(employeeList.get(i).getStatus());
row1.createCell(6).setCellValue(employeeList.get(i).getCreateTime().toString());
row1.createCell(7).setCellValue(employeeList.get(i).getUpdateTime().toString());
}
//设置表格的名称,通过时间设置名称
String fileName = "员工信息表"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+".xlsx";
//创建文件输出流
FileOutputStream fileOutputStream = new FileOutputStream("D:\\"+fileName);
xssfWorkbook.write(fileOutputStream);
// ServletOutputStream outputStream = response.getOutputStream();
//下面的是Excel导出中HttpServletResponse消息头参数设置,具体意思大家可以百度一下
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment;filename=\"" +fileName+ ".xlsx\"");
xssfWorkbook.write(response.getOutputStream());
xssfWorkbook.close();
}
}
代码中的第一个for循环相当于设置表头
第二个for循环就是将数据表的数据set到表格中
前端我只做了一个简单的页面跳转
点击按钮导出表格
希望本文对大家有用哈。