add
This commit is contained in:
27
qiaoba-commons/qiaoba-common-poi/pom.xml
Normal file
27
qiaoba-commons/qiaoba-common-poi/pom.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>qiaoba-commons</artifactId>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>qiaoba-common-poi</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- easypoi -->
|
||||
<dependency>
|
||||
<groupId>cn.afterturn</groupId>
|
||||
<artifactId>easypoi-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- Java Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,39 @@
|
||||
package com.qiaoba.common.poi.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Excel导出设置对象
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2021/10/25 0025 上午 9:15
|
||||
*/
|
||||
@Data
|
||||
public class ExcelSetting implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String EXCEL_FIELD_SHOW_FLAG = "1";
|
||||
|
||||
private Long id;
|
||||
|
||||
private String className;
|
||||
|
||||
private String fieldId;
|
||||
|
||||
private String fieldName;
|
||||
|
||||
private String width;
|
||||
|
||||
private Integer sort;
|
||||
|
||||
private String show;
|
||||
|
||||
private String dateFormat;
|
||||
|
||||
private String replace;
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
package com.qiaoba.common.poi.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 cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
|
||||
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.poi.model.ExcelSetting;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Excel 工具类
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2021/10/25 0025 上午 9:15
|
||||
*/
|
||||
public class ExcelUtil {
|
||||
|
||||
|
||||
/**
|
||||
* 默认导出
|
||||
*
|
||||
* @param list 数据
|
||||
* @param pojoClass java类
|
||||
* @param fileName 文件名
|
||||
* @param response response
|
||||
*/
|
||||
public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response) {
|
||||
defaultExport(list, pojoClass, fileName, new ExportParams(fileName, fileName, ExcelType.XSSF), response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义导出
|
||||
*
|
||||
* @param list 数据
|
||||
* @param entities 字段设置
|
||||
* @param fileName 文件名
|
||||
* @param response response
|
||||
*/
|
||||
public static void exportExcel(List<?> list, List<ExcelExportEntity> entities, String fileName, HttpServletResponse response) {
|
||||
exportBySettings(list, entities, fileName, new ExportParams(fileName, fileName, ExcelType.XSSF), response);
|
||||
}
|
||||
|
||||
private static void exportBySettings(List<?> list, List<ExcelExportEntity> entities, String fileName, ExportParams exportParams, HttpServletResponse response) {
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entities, list);
|
||||
downLoadExcel(fileName, response, workbook);
|
||||
}
|
||||
|
||||
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) {
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
|
||||
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 + ".xlsx" , "UTF-8"));
|
||||
workbook.write(response.getOutputStream());
|
||||
IOUtils.closeQuietly(workbook);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
|
||||
return importExcel(file.getInputStream(), 1, 1, pojoClass);
|
||||
}
|
||||
|
||||
private static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
|
||||
if (inputStream == null) {
|
||||
return null;
|
||||
}
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(titleRows);
|
||||
params.setHeadRows(headerRows);
|
||||
params.setSaveUrl("/excel/");
|
||||
try {
|
||||
return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
|
||||
} catch (NoSuchElementException e) {
|
||||
throw new IOException("excel文件不能为空");
|
||||
} catch (Exception e) {
|
||||
throw new IOException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static List<ExcelExportEntity> createExcelEntry(List<ExcelSetting> settings) {
|
||||
List<ExcelExportEntity> entities = new ArrayList<>();
|
||||
for (ExcelSetting setting : settings) {
|
||||
// 去除隐藏
|
||||
if (!ExcelSetting.EXCEL_FIELD_SHOW_FLAG.equals(setting.getShow())) {
|
||||
continue;
|
||||
}
|
||||
ExcelExportEntity entity = new ExcelExportEntity(setting.getFieldName(), setting.getFieldId());
|
||||
entity.setWidth(Double.valueOf(setting.getWidth()));
|
||||
String replace = setting.getReplace();
|
||||
if (StrUtil.isNotBlank(replace) && replace.contains(",")) {
|
||||
entity.setReplace(replace.split(","));
|
||||
}
|
||||
entity.setFormat(setting.getDateFormat());
|
||||
entity.setOrderNum(setting.getSort());
|
||||
entities.add(entity);
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user