springboot导出excel
时间: 2023-11-05 13:02:28 浏览: 163
Spring Boot提供了多种方式来导出Excel,其中常见的有以下几种:
1. 使用POI实现Excel导出
POI是一款用于操作Microsoft Office格式文件的Java API。通过POI可以创建、读取和修改Excel、Word和PowerPoint等格式文件。在Spring Boot中使用POI可以很方便地实现Excel导出功能。
步骤如下:
1)添加依赖:
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
2)创建Excel文件:
可以使用以下代码创建Excel文件:
```
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
```
3)将Excel文件写入输出流:
可以使用以下代码将Excel文件写入输出流:
```
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=test.xlsx");
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
```
完整代码如下:
```
@RequestMapping("/export")
public void export(HttpServletResponse response) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=test.xlsx");
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}
```
2. 使用EasyExcel实现Excel导出
EasyExcel是一款基于POI封装的Java库,能够快速、高效地读写Excel文件。EasyExcel提供了简单易用的API,可以很方便地实现Excel导入导出功能。
步骤如下:
1)添加依赖:
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
```
2)创建Excel文件:
可以使用以下代码创建Excel文件:
```
List<String> data = Arrays.asList("Hello,World!");
String fileName = "test.xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
writeSheet.setHead(Arrays.asList("Content"));
excelWriter.write(data, writeSheet);
excelWriter.finish();
```
3)将Excel文件写入输出流:
可以使用以下代码将Excel文件写入输出流:
```
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=test.xlsx");
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
out.flush();
out.close();
```
完整代码如下:
```
@RequestMapping("/export")
public void export(HttpServletResponse response) throws IOException {
List<String> data = Arrays.asList("Hello,World!");
String fileName = "test.xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
writeSheet.setHead(Arrays.asList("Content"));
excelWriter.write(data, writeSheet);
excelWriter.finish();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=test.xlsx");
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
out.flush();
out.close();
}
```
以上就是Spring Boot导出Excel的两种常见方式。可以根据实际需求选择适合的方式来实现Excel导出功能。
阅读全文