1 首先加入Excel的依赖,本例中我们用apache的poi:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
2.1 封装Excel导出工具类,导出xls格式
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* Excel工具类
*/
public class ExcelUtil {
/**
* Excel表格导出
* @param response HttpServletResponse对象
* @param excelData Excel表格的数据,封装为List<List<String>>
* @param sheetName sheet的名字
* @param fileName 导出Excel的文件名
* @param columnWidth Excel表格的宽度,建议为15
* @throws IOException 抛IO异常
*/
public static void exportExcel(HttpServletResponse response,
List<List<String>> excelData,
String sheetName,
String fileName,
int columnWidth) throws IOException {
//文件名的编码可能不对,需要在这里指定修改
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//生成一个表格,设置表格名称
HSSFSheet sheet = workbook.createSheet(sheetName);
//设置表格列宽度
sheet.setDefaultColumnWidth(columnWidth);
//写入List<List<String>>中的数据
int rowIndex = 0;
for(List<String> data : excelData){
//创建一个row行,然后自增1
HSSFRow row = sheet.createRow(rowIndex++);
//遍历添加本行数据
for (int i = 0; i < data.size(); i++) {
//创建一个单元格
HSSFCell cell = row.createCell(i);
//创建一个内容对象
HSSFRichTextString text = new HSSFRichTextString(data.get(i));
//将内容对象的文字内容写入到单元格中
cell.setCellValue(text);
}
}
//准备将Excel的输出流通过response输出到页面下载
//八进制输出流
response.setContentType("application/octet-stream; charset=utf-8");
//设置导出Excel的名称
response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
//刷新缓冲
response.flushBuffer();
//workbook将Excel写入到response的输出流中,供页面下载该Excel文件
OutputStream os = response.getOutputStream();
workbook.write(os);
//关闭workbook
workbook.close();
os.flush();
os.close();
}
}
2.2 导出xlsx格式
上面使用的HSSFWorkbook 导出的Excel是xls格式的,如果要导出xlsx格式,需要格外的包
<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>
封装工具类
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
* Excel工具类
*/
public class ExcelUtil {
/**
* Excel表格导出
* @param response HttpServletResponse对象
* @param excelData Excel表格的数据,封装为List<List<String>>
* @param sheetName sheet的名字
* @param fileName 导出Excel的文件名
* @param columnWidth Excel表格的宽度,建议为15
* @throws IOException 抛IO异常
*/
public static void exportExcel(HttpServletResponse response,
List<List<String>> excelData,
String sheetName,
String fileName,
int columnWidth) throws IOException {
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
//声明一个工作簿
Workbook workbook = new XSSFWorkbook();
//生成一个表格,设置表格名称
Sheet sheet = workbook.createSheet(sheetName);
//设置表格列宽度
sheet.setDefaultColumnWidth(columnWidth);
//写入List<List<String>>中的数据
int rowIndex = 0;
for(List<String> data : excelData){
//创建一个row行,然后自增1
Row row = sheet.createRow(rowIndex++);
//遍历添加本行数据
for (int i = 0; i < data.size(); i++) {
//创建一个单元格
Cell cell = row.createCell(i);
//创建一个内容对象
XSSFRichTextString text = new XSSFRichTextString(data.get(i));
//将内容对象的文字内容写入到单元格中
cell.setCellValue(text);
}
}
//准备将Excel的输出流通过response输出到页面下载
//八进制输出流
response.setContentType("application/octet-stream; charset=utf-8");
//设置导出Excel的名称
response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
//刷新缓冲
response.flushBuffer();
//workbook将Excel写入到response的输出流中,供页面下载该Excel文件
OutputStream os = response.getOutputStream();
workbook.write(os);
//关闭workbook
workbook.close();
os.flush();
os.close();
}
}
3 Controller调用
@GetMapping("/excelExport")
public void excelExport(HttpServletResponse response) throws IOException {
List<List<String>> excelData = new ArrayList<>();
List<String> head = new ArrayList<>();
head.add("第一列");
head.add("第二列");
head.add("第三列");
List<String> data1 = new ArrayList<>();
data1.add("123");
data1.add("234");
data1.add("345");
List<String> data2 = new ArrayList<>();
data2.add("abc");
data2.add("bcd");
data2.add("cde");
excelData.add(head);
excelData.add(data1);
excelData.add(data2);
String sheetName = "测试";
String fileName = "ExcelTest.xls";
ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15);
}
4 前端调用
excelExport() {
//接口地址
var downUrl = process.env.BASE_API + "/excelExport"
window.location.href = downUrl
}
参考博客:https://blog.csdn.net/weixin_39448458/article/details/83013677