1、pom.xml中导入依赖包
springboot项目在pom.xml中导入依赖,maven进行更新,加载jar包。
<!-- EasyPOI 导入导出依赖包 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
2、实体类中,在需要导出的成员变量上方加@Excel注解
package com.domain;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;
/**
*
* @TableName person
*/
@TableName(value ="person")
@Data
public class Person implements Serializable {
/**
* 主键
*/
@TableId(type = IdType.AUTO)
@Excel(name="主键")
private Long id;
/**
* 名字
*/
@Excel(name="姓名")
private String name;
/**
* 性别
*/
@Excel(name="性别")
private String sex;
/**
* 地址
*/
private String adress;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
3、添加配置类(工具类)
public class ExcelUtil {
/**
* 导出: 常用导出模板
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
/**
* 导出:创建表头的模板
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* 使用Map作为参数导出 不推荐使用
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
defaultExport(list, fileName, response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 导入: 常用导入模板
* @param titleRows 标题行数
* @param headerRows 表头行数
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
e.printStackTrace();
throw new RuntimeException("excel文件不能为空");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return list;
}
/**
* excel 导入 默认 1行标题 1行表头
*/
public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
return importExcel(file, 1, 1, pojoClass);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw new RuntimeException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return list;
}
}
4、控制层写方法并调用工具类中的方法
@RestController
@RequestMapping("person")
public class PersonController {
@Autowired
PersonMapper personMapper;
@RequestMapping("excel")
public boolean excel(HttpServletResponse response){
List<Person> list = personMapper.selectList(null);
ExcelUtil.exportExcel(list,"标题","标签",Person.class,"Person.xls",response);
return true;
}
HttpServletResponse response 是Java的核心接口,前后台的交互模式是请求回应式的。前端向后台发请求,会被后台封装成一个请求对象。后台把数据响应给前端,就会有一个响应对象 response
5、使用网页测试
弹窗—>保存文件—>Excel
6、导出Excel文件