1.导入依赖
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
倒完依赖就可以直接使用了
//1.首先创建一个excel 对象
XSSFWorkbook wb=new XSSFWorkbook();
//2.根据创建的操作对象创建操作页面 并给文件赋名字
XSSFSheet sheet = wb.createSheet( order.getCustomerName()+"表.xlsl");
//3.创建一行
XSSFRow row = sheet.createRow(0);
//4.根据创建的行创建列
//下标是几代表是第几个单元格内容
XSSFCell cell = row.createCell(0);
//给当前列赋值
cell.setCellValue("订单编号");
导出Excel文件
1.导出指定目录下(本地磁盘中)
//创建字符输出流
FileOutputStream outputStream=new FileOutputStream("F:\\tp\\test.xlsx");
//将内容写入进去
wb.write(outputStream);
//关闭流
wb.close;
outputStream.close;
2.前端点击下载按钮直接下载
//需要设置响应 防止中文乱码
response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(filename,"utf-8"));
//读取字符响应流
//os为字符响应流 response.getOutputStream()从controller传到service为outputStream
wb.write(os);
//关闭流
wb.close;
outputStream.close;
由于自己也不是很明白所以推荐看大佬分享的内容
https://blog.csdn.net/qq_42651904/article/details/88221392