POM文件引入
cn.afterturn
easypoi-base
4.3.0
cn.afterturn
easypoi-web
4.3.0
cn.afterturn
easypoi-annotation
4.3.0
com.alibaba
easyexcel
2.1.3
创建导入导出VO
package com.wl.ltx.dealer.sql.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* 导出对象
* @since 2022-06-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="ExportVo对象", description="ExportVo对象")
public class ExportVo implements Serializable {
private static final long serialVersionUID=1L;
@ApiModelProperty(value = "ID")
private String id;
@ApiModelProperty(value = "银行联号")
@Excel(name = "银行联号",width = 20)
private String affiliated;
@ApiModelProperty(value = "公私标识")
@Excel(name = "公私标识",replace = { "私户_1", "公户_2"},width = 20)
private String pubPriSign;
@ApiModelProperty("有效起始日期")
@Excel(name = "有效起始日期",format = "yyyy-MM-dd",width = 20)
private Date bkValidFrom;
@ApiModelProperty("有效结束日期")
@Excel(name = "有效结束日期",format = "yyyy-MM-dd",width = 20)
private Date bkValidTo;
}
@ApiOperation(value = "导入", notes = "导入", httpMethod = "POST")
@RequestMapping(value = "/import", method = {RequestMethod.POST})
@Transactional(rollbackFor = Exception.class)
public ResponseEntity import(@RequestParam(value = "file",required = false) MultipartFile file){
ImportParams importParams = new ImportParams();
// 数据处理
importParams.setHeadRows(1);
importParams.setTitleRows(1);
try {
List list = ExcelImportUtil.importExcel(file.getInputStream(), ExportVo.class, importParams);
if(list.size()==0){
return RestResponse.createFailRes("上传0条请按照模板格式上传");
}else {
//自行处理导入的数据做操作.....
}
return RestResponse.createSuccessRes("导入成功");
} catch (Exception e) {
e.printStackTrace();
return RestResponse.createFailRes(e.getMessage());
}
}
导出
@ApiOperation(value = "导出", notes = "导出")
@RequestMapping(value = "/export", method = {RequestMethod.POST})
public void export(@RequestBody ExportVo param, HttpServletResponse response) {
try {
List exportDealerBankVoList = infoService.queryExport(param);//查询
EaspoiExcelUtils.exportExcel(exportDealerBankVoList,"导出","导出",ExportVo.class,"导出", response);
} catch (Exception e) {
e.printStackTrace();
LoggerFactory.getLogger(this.getClass()).error(e.getMessage());
}
}
附一个工具类
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 cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*ClassName: EaspoiExcelUtils
*Package: com.wl.cloud.common.utils
*Description:
*/
public class EaspoiExcelUtils {
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);
}
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
多shell 工具类
/**
* @description:
*/
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WorkBookUtils {
/**
* 创建workbook,
* 通过maplist填充Excel内容
* 返回workbook
*
* 进一步使用可以写入流,e.g.
* FileOutputStream fos = new FileOutputStream(file);
* workbook.write(fos);
* */
public static Workbook mutiSheet(List> mapListList){
Workbook workbook = null;
workbook = ExcelExportUtil.exportExcel(mapListList,ExcelType.HSSF);
return workbook;
}
public static Map createOneSheet(ExportParams exportParams,Class> clazz,List> data){
Map map = new HashMap<>();
map.put("title",exportParams);//new ExportParams("title"+i, "sheetName"+i, ExcelType.XSSF)
map.put("entity", clazz);
map.put("data",data);
return map;
}
/*
* 创建一个表格并填充内容
* 返回map供工作簿使用
* */
public static Map createOneSheet(String sheetName,String title,Class> clazz,List> data){
ExportParams exportParams = new ExportParams(title,sheetName, ExcelType.HSSF);
return createOneSheet(exportParams,clazz,data);
}
/**
* 创建workbook,
* 通过maplist填充Excel内容
* 返回workbook
*
* 进一步使用可以写入流,e.g.
* FileOutputStream fos = new FileOutputStream(file);
* workbook.write(fos);
* */
public static Workbook mutiSheetXSSF(List> mapListList){
Workbook workbook = null;
workbook = ExcelExportUtil.exportExcel(mapListList,ExcelType.XSSF);
return workbook;
}
/*
* 创建一个表格并填充内容
* 返回map供工作簿使用
* */
public static Map createOneSheetXSSF(String sheetName,String title,Class> clazz,List> data){
ExportParams exportParams = new ExportParams(title,sheetName, ExcelType.XSSF);
return createOneSheet(exportParams,clazz,data);
}
}
多shell 导出
@ApiOperation(value = "多shell导出", notes = "多shell导出")
@RequestMapping(value = "/exportShell", method = {RequestMethod.POST})
public void exportShell(@RequestBody ExportVo param, HttpServletResponse response) {
try {
List> lists = new ArrayList<>();
List vo1List = infoService.query1Export(param);
List vo2List = infoService.query2Export(param);
lists.add(WorkBookUtils.createOneSheet("shell1导出", "shell1导出", ExportShell1Vo.class, vo1List ));
lists.add(WorkBookUtils.createOneSheet("shell2导出", "shell2导出", ExportShell2Vo.class, vo2List));
Workbook workbook = WorkBookUtils.mutiSheet(lists);
if (ObjectUtils.isNotEmpty(workbook)){
EaspoiExcelUtils.downLoadExcel("导出", response, workbook);
}
} catch (Exception e) {
e.printStackTrace();
LoggerFactory.getLogger(this.getClass()).error(e.getMessage());
}
}