Easypoi
引入依赖:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
工具类:
/**
* ***********************************************************************
*
* @since: 文件注释
* @name: MyExcelUtils
* @copyright: Copyright: 2020-2025
* @author: liurui28
* @vision V1.0.0
* @date: 2022/11/17
* HIK所有,受到法律的保护,任何公司或个人,未经授权不得擅自拷贝。
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify by user :{修改人}
* ***********************************************************************
**/
package com.hikvision.sspd.nedc.modules.business.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.NoSuchElementException;
public class MyExcelUtils {
/**
* 功能描述:复杂导出Excel,包括文件名以及表名,不创建表头
*
* @param list 导出的实体类
* @param title 表头名称
* @param sheetName sheet表名
* @param pojoClass 映射的实体类
* @param fileName
* @param response
* @return
*/
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));
}
/**
* 功能描述:默认导出方法
*
* @param list 导出的实体集合
* @param fileName 导出的文件名
* @param pojoClass pojo实体
* @param exportParams ExportParams封装实体
* @param response
* @return
*/
private static void defaultExport( List<?> list, Class<?> pojoClass, String fileName,
HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
/**
* 功能描述:Excel导出
*
* @param fileName 文件名称
* @param response
* @param workbook Excel对象
* @return
*/
private static void downLoadExcel( String fileName, HttpServletResponse response,
Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 功能描述:根据接收的Excel文件来导入Excel,并封装成实体类
*
* @param file 上传的文件
* @param titleRows 表标题的行数
* @param headerRows 表头行数
* @param pojoClass Excel实体类
* @return
*/
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) {
throw new RuntimeException("excel文件不能为空");
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return list;
}
}
常用注解:
1. @ExcelTarget
@ExcelTarget注解作用于最外层的对象,可以这么理解——这个类与一个excel对应(类属性对应excel的列);在使用easypoi的API导出excel生成Workbook对象时,作为参数传入,如下:
2. @Excel
@Excel 注解是作用到Filed上面,是对Excel一列的一个描述,这个注解是必须要的注解
/**
* 一级审批人,excel列名:一级审批人、顺序6、宽度20、分组"审批人信息"
*/
@Excel(name = "一级审批人", orderNum = "6", width = 20, groupName = "审批人信息")
private String firstAuditor;
3. @ExcelEntity
@ExcelEntity注解表示一个继续深入导出的实体,是作用一个类型为实体的属性上面
4. @ExcelCollection
@ExcelCollection注解表示一个集合,主要针对一对多的导出,作用在类型是List的属性上面;
/**
* 审核人信息
*/
@ExcelCollection(name = "审核人信息", orderNum = "6")
private List<StepAndAuditorExport> stepAndAuditorList;
导出controller
@ApiOperation("施工单位导出")
@PostMapping("/outputExcel")
public void outputExcel(@RequestBody @Validated TCompanyPageRequest tbTCompanyPageRequest, HttpServletResponse response) {
tbTCompanyPageRequest.setPageNo(1);
tbTCompanyPageRequest.setPageSize(1000000);
com.baomidou.mybatisplus.extension.plugins.pagination.Page<TCompany> pageResult = tCompanyRepository.paginQuery(tbTCompanyPageRequest);
//3. 分页结果组装
List<TCompany> dataList = pageResult.getRecords();
List<AddCompanyParam> resultList = new ArrayList<>();
String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
String fileName="施工单位列表-"+ yyyyMMddHHmmss +".xls";
MyExcelUtils.exportExcel(resultList,
"施工单位",
"施工单位列表",
AddCompanyParam.class,
fileName, response);
}
标签:excel,Excel,param,fileName,import,Easypoi,response,注解
From: https://www.cnblogs.com/liurui12138/p/17196364.html