提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
EasyExcel通用导出
一、准备工作
1.导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
<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>
二、代码示例如下
1.目录结构
2.详细代码
TestExportController
package com.example.easyexcel.controller;
import com.example.easyexcel.entity.ExportStudent;
import com.example.easyexcel.service.ExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* @author MUKE
*/
@RestController
public class TestExportController {
@Autowired
private ExportService exportService;
@PostMapping("/preExportJsdList")
public void preExportJsdList(HttpServletResponse response) {
List<ExportStudent> list = new ArrayList<>();
ExportStudent exportStudent = new ExportStudent();
exportStudent.setName("MUKE");
exportStudent.setAge(22);
list.add(exportStudent);
exportService.exportDataList(response, list, ExportStudent.class);
}
}
ExportStudent(导出模板类)
package com.example.easyexcel.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;
/**
* @author A80222
*/
@Data
public class ExportStudent {
@ExcelProperty( value = "姓名")
@ColumnWidth(5)
private String name;
@ExcelProperty( value = "年龄")
@ColumnWidth(5)
private Integer age;
}
ExportServiceImpl(导出实现类)
package com.example.easyexcel.service.impl;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.example.easyexcel.entity.ExportStudent;
import com.example.easyexcel.service.ExportService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Service
public class ExportServiceImpl implements ExportService {
@Override
public <E> void exportDataList(HttpServletResponse response, List<E> dataList, Class<E> clazz) {
try {
response.setHeader("Content-disposition", "attachment;filename=" + new String("学生信息表".getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream(), clazz);
writerBuilder
.autoCloseStream(true)
.excelType(ExcelTypeEnum.XLSX)
.needHead(true)
.sheet().sheetName("安装结算单数据")
.doWrite(dataList);
} catch (Exception e) {
}
}
}
ExportService(导出接口)
package com.example.easyexcel.service;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public interface ExportService {
//dataList 导出的数据, clazz导出数据的模板类
<E> void exportDataList(HttpServletResponse response, List<E> dataList, Class<E> clazz);
}