修改参数模版导入

This commit is contained in:
gaosong
2023-11-18 12:24:19 +08:00
parent b1c0a575c1
commit d65da69c1d
4 changed files with 138 additions and 14 deletions
+22 -3
View File
@@ -124,7 +124,7 @@
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
@@ -143,12 +143,31 @@
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.junrar</groupId>
@@ -6,6 +6,7 @@ import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.aliyuncs.utils.IOUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -25,6 +26,7 @@ import com.jero.modules.cert.template.mapper.ParamsInfoEOMapper;
import com.jero.modules.cert.template.service.ICertCategoryParamsInfoEOService;
import com.jero.modules.cert.template.service.IParamsInfoEOService;
import com.jero.modules.cert.template.utils.CommonExcelExportStyler;
import com.jero.modules.cert.template.utils.RightSheetExcelImportUtil;
import com.jero.modules.cert.template.vo.*;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
@@ -34,6 +36,7 @@ import com.jero.modules.system.entity.SysCategory;
import com.jero.modules.system.entity.SysDictItem;
import com.jero.modules.system.service.ISysDictItemService;
import com.jero.modules.system.service.impl.SysCategoryServiceImpl;
import com.monitorjbl.xlsx.StreamingReader;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -904,8 +907,11 @@ public class ParamsInfoEOServiceImpl extends ServiceImpl<ParamsInfoEOMapper, Par
// 导入参数设置,默认即可
ImportParams params = new ImportParams();
try {
// 取数据
XSSFWorkbook workbook=new XSSFWorkbook(new FileInputStream(excelfile));
Workbook workbook = StreamingReader.builder()
.rowCacheSize(10 * 10)
.bufferSize(1024 * 4)
.open(new FileInputStream(excelfile));
List<ParamsInfoCnImport> paramsInfoEOList = new ArrayList<>();
Map<String,List<CertCategoryParamsInfoCnImport>> certCategoryParamsInfoEOListMap = new HashMap<>();
@@ -936,16 +942,16 @@ public class ParamsInfoEOServiceImpl extends ServiceImpl<ParamsInfoEOMapper, Par
"Description", "*Control Component", "Validate Component", "Component Value", "Default Value", "File Template"};
params.setImportFields(fields);
ExcelImportResult<ParamsInfoEnImport> resultEn = ExcelImportUtil.importExcelMore(excelfile,ParamsInfoEnImport.class, params);
List<ParamsInfoEnImport> paramsInfoEOListEn = resultEn.getList();
// importExcelBySax 方法有bug,仅能导入第一个sheet页,所以仅针对numSheet为0的时候使用
List<ParamsInfoEnImport> paramsInfoEOListEn = RightSheetExcelImportUtil.importExcelBySax(new FileInputStream(excelfile), ParamsInfoCnImport.class, params);
copyParamsInfoList(paramsInfoEOListEn, paramsInfoEOList);
} else {
String fields[] ={ "*NIO编号", "*是否必填", "*参数名称","*参数批次","*责任领域",
"参数说明", "*控件类型", "控件校验", "控件备选值", "默认值", "附件模板"};
params.setImportFields(fields);
ExcelImportResult<ParamsInfoCnImport> result = ExcelImportUtil.importExcelMore(excelfile, ParamsInfoCnImport.class, params);
paramsInfoEOList = result.getList();
// importExcelBySax 方法有bug,仅能导入第一个sheet页,所以仅针对numSheet为0的时候使用
paramsInfoEOList = RightSheetExcelImportUtil.importExcelBySax(new FileInputStream(excelfile), ParamsInfoCnImport.class, params);
}
} else { // 认证类别参数表
@@ -955,15 +961,13 @@ public class ParamsInfoEOServiceImpl extends ServiceImpl<ParamsInfoEOMapper, Par
String fields[] ={ "*NIO Number", "*Number", "*Para Name","Description"};
params.setImportFields(fields);
ExcelImportResult<CertCategoryParamsInfoEnImport> resultEn = ExcelImportUtil.importExcelMore(excelfile, CertCategoryParamsInfoEnImport.class, params);
List<CertCategoryParamsInfoEnImport> listEn = resultEn.getList();
List<CertCategoryParamsInfoEnImport> listEn = RightSheetExcelImportUtil.importExcelBySax(new FileInputStream(excelfile), CertCategoryParamsInfoEnImport.class, params);
copycertCategoryParamsInfoList(listEn, list);
} else {
String fields[] ={ "*NIO编号", "*编号", "*参数名称","参数说明"};
params.setImportFields(fields);
ExcelImportResult<CertCategoryParamsInfoCnImport> result = ExcelImportUtil.importExcelMore(excelfile, CertCategoryParamsInfoCnImport.class, params);
list = result.getList();
list = RightSheetExcelImportUtil.importExcelBySax(new FileInputStream(excelfile), CertCategoryParamsInfoCnImport.class, params);
}
if (CollectionUtil.isNotEmpty(list)) {
list.remove(0); // 去掉填写说明
@@ -0,0 +1,20 @@
package com.jero.modules.cert.template.utils;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.imports.sax.SaxReadExcel;
import cn.afterturn.easypoi.excel.imports.sax.parse.ISaxRowRead;
import cn.afterturn.easypoi.handler.inter.IExcelReadRowHandler;
import java.io.InputStream;
import java.util.List;
/**
* 覆写为正确的sheet读取的工具类
*/
public class RightSheetExcelImportUtil {
public static <T> List<T> importExcelBySax(InputStream inputstream, Class<?> pojoClass, ImportParams params) {
return (new RightSheetSaxReadExcel()).readExcel(inputstream, pojoClass, params, (ISaxRowRead)null, (IExcelReadRowHandler)null);
}
}
@@ -0,0 +1,81 @@
package com.jero.modules.cert.template.utils;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.imports.sax.SaxReadExcel;
import cn.afterturn.easypoi.excel.imports.sax.SheetHandler;
import cn.afterturn.easypoi.excel.imports.sax.parse.ISaxRowRead;
import cn.afterturn.easypoi.excel.imports.sax.parse.SaxRowRead;
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import cn.afterturn.easypoi.handler.inter.IExcelReadRowHandler;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
/**
* 修复SaxReadExcel Sheet不正确的问题
*/
public class RightSheetSaxReadExcel extends SaxReadExcel {
private static final Logger LOGGER = LoggerFactory.getLogger(RightSheetSaxReadExcel.class);
@Override
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params, ISaxRowRead rowRead, IExcelReadRowHandler hanlder) {
try {
OPCPackage opcPackage = OPCPackage.open(inputstream);
return this.readExcel(opcPackage, pojoClass, params, rowRead, hanlder);
} catch (Exception var7) {
LOGGER.error(var7.getMessage(), var7);
throw new ExcelImportException(var7.getMessage());
}
}
private <T> List<T> readExcel(OPCPackage opcPackage, Class<?> pojoClass, ImportParams params, ISaxRowRead rowRead, IExcelReadRowHandler hanlder) {
try {
XSSFReader xssfReader = new XSSFReader(opcPackage);
SharedStringsTable sst = xssfReader.getSharedStringsTable();
if (rowRead == null) {
rowRead = new SaxRowRead(pojoClass, params, hanlder);
}
XMLReader parser = this.fetchSheetParser(sst, (ISaxRowRead)rowRead);
Iterator<InputStream> sheets = xssfReader.getSheetsData();
int sheetIndex = 0;
while (sheets.hasNext() && sheetIndex < params.getSheetNum() + params.getStartSheetIndex()) {
if (sheetIndex < params.getStartSheetIndex()) {
sheets.next();
} else {
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
sheetIndex++;
}
return ((ISaxRowRead)rowRead).getList();
} catch (Exception var13) {
LOGGER.error(var13.getMessage(), var13);
throw new ExcelImportException("SAX导入数据失败");
}
}
private XMLReader fetchSheetParser(SharedStringsTable sst, ISaxRowRead rowRead) throws SAXException {
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
ContentHandler handler = new SheetHandler(sst, rowRead);
parser.setContentHandler(handler);
return parser;
}
}