|
@@ -1,6 +1,9 @@
|
|
|
package com.platform.office.poi.excel.imports;
|
|
|
|
|
|
+import com.platform.office.annotation.Excel;
|
|
|
+import com.platform.office.annotation.ExcelCollection;
|
|
|
import com.platform.office.annotation.ExcelTarget;
|
|
|
+import com.platform.office.poi.cache.DictHandle;
|
|
|
import com.platform.office.poi.excel.entity.ImportParams;
|
|
|
import com.platform.office.poi.excel.entity.params.ExcelCollectionParams;
|
|
|
import com.platform.office.poi.excel.entity.params.ExcelImportEntity;
|
|
@@ -29,6 +32,8 @@ import java.io.FileOutputStream;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.PushbackInputStream;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -199,6 +204,135 @@ public class ExcelImportServer extends ImportBaseService {
|
|
|
return collection;
|
|
|
}
|
|
|
|
|
|
+ public void getAllExcelField(DictHandle dictHandle, String targetId, Field[] fields, Map<String, ExcelImportEntity> excelParams, List<ExcelCollectionParams> excelCollection, Class<?> pojoClass, List<Method> getMethods) throws Exception {
|
|
|
+ ExcelImportEntity excelEntity = null;
|
|
|
+ for (int i = 0; i < fields.length; i++) {
|
|
|
+ Field field = fields[i];
|
|
|
+ if (PoiPublicUtil.isNotUserExcelUserThis(null, field, targetId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (PoiPublicUtil.isCollection(field.getType())) {
|
|
|
+ // 集合对象设置属性
|
|
|
+ ExcelCollectionParams collection = new ExcelCollectionParams();
|
|
|
+ collection.setName(field.getName());
|
|
|
+ Map<String, ExcelImportEntity> temp = new HashMap<String, ExcelImportEntity>();
|
|
|
+ ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
|
|
+ Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0];
|
|
|
+ collection.setType(clz);
|
|
|
+ getExcelFieldList(targetId, PoiPublicUtil.getClassFields(clz), clz, temp, null);
|
|
|
+ collection.setExcelParams(temp);
|
|
|
+ collection.setExcelName(field.getAnnotation(ExcelCollection.class).name());
|
|
|
+ additionalCollectionName(collection);
|
|
|
+ excelCollection.add(collection);
|
|
|
+ } else if (PoiPublicUtil.isJavaClass(field)) {
|
|
|
+ addEntityToMap(dictHandle,targetId, field, excelEntity, pojoClass, getMethods, excelParams);
|
|
|
+ } else {
|
|
|
+ List<Method> newMethods = new ArrayList<Method>();
|
|
|
+ if (getMethods != null) {
|
|
|
+ newMethods.addAll(getMethods);
|
|
|
+ }
|
|
|
+ newMethods.add(PoiPublicUtil.getMethod(field.getName(), pojoClass));
|
|
|
+ getAllExcelField(dictHandle,targetId, PoiPublicUtil.getClassFields(field.getType()), excelParams, excelCollection, field.getType(), newMethods);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addEntityToMap(DictHandle dictHandle,String targetId, Field field, ExcelImportEntity excelEntity, Class<?> pojoClass, List<Method> getMethods, Map<String, ExcelImportEntity> temp) throws Exception {
|
|
|
+ Excel excel = field.getAnnotation(Excel.class);
|
|
|
+ excelEntity = new ExcelImportEntity();
|
|
|
+ excelEntity.setType(excel.type());
|
|
|
+ excelEntity.setSaveUrl(excel.savePath());
|
|
|
+ excelEntity.setSaveType(excel.imageType());
|
|
|
+ excelEntity.setReplace(excel.replace());
|
|
|
+ excelEntity.setDatabaseFormat(excel.databaseFormat());
|
|
|
+ excelEntity.setVerify(getImportVerify(field));
|
|
|
+ excelEntity.setSuffix(excel.suffix());
|
|
|
+ getExcelField(targetId, field, excelEntity, excel, pojoClass);
|
|
|
+ // dictCode不为空
|
|
|
+ if(StringUtils.isNotBlank(excel.dicCode()) && dictHandle != null){
|
|
|
+ String[] replace = dictHandle.getReplace(excel.dicCode());
|
|
|
+ excelEntity.setReplace(replace);
|
|
|
+ }
|
|
|
+ if (getMethods != null) {
|
|
|
+ List<Method> newMethods = new ArrayList<Method>();
|
|
|
+ newMethods.addAll(getMethods);
|
|
|
+ newMethods.add(excelEntity.getMethod());
|
|
|
+ excelEntity.setMethods(newMethods);
|
|
|
+ }
|
|
|
+ temp.put(excelEntity.getName(), excelEntity);
|
|
|
+
|
|
|
+ }
|
|
|
+ private void additionalCollectionName(ExcelCollectionParams collection) {
|
|
|
+ Set<String> keys = new HashSet<String>();
|
|
|
+ keys.addAll(collection.getExcelParams().keySet());
|
|
|
+ for (String key : keys) {
|
|
|
+ collection.getExcelParams().put(collection.getExcelName() + "_" + key, collection.getExcelParams().get(key));
|
|
|
+ collection.getExcelParams().remove(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> List<T> importExcel(DictHandle dictHandle,Collection<T> result, Sheet sheet, Class<?> pojoClass, ImportParams params, Map<String, PictureData> pictures) throws Exception {
|
|
|
+ List collection = new ArrayList();
|
|
|
+ Map<String, ExcelImportEntity> excelParams = new HashMap<>();
|
|
|
+ List<ExcelCollectionParams> excelCollection = new ArrayList<>();
|
|
|
+ String targetId = null;
|
|
|
+ if (!Map.class.equals(pojoClass)) {
|
|
|
+ Field[] fields = PoiPublicUtil.getClassFields(pojoClass);
|
|
|
+ ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
|
|
|
+ if (etarget != null) {
|
|
|
+ targetId = etarget.value();
|
|
|
+ }
|
|
|
+ getAllExcelField(dictHandle,targetId, fields, excelParams, excelCollection, pojoClass, null);
|
|
|
+ }
|
|
|
+ Iterator<Row> rows = sheet.rowIterator();
|
|
|
+ for (int j = 0; j < params.getTitleRows(); j++) {
|
|
|
+ rows.next();
|
|
|
+ }
|
|
|
+ Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection);
|
|
|
+ Row row = null;
|
|
|
+ Object object = null;
|
|
|
+ String picId;
|
|
|
+ while (rows.hasNext() && (row == null || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) {
|
|
|
+ row = rows.next();
|
|
|
+ int tempNum = params.getKeyIndex();
|
|
|
+ System.out.println("---------------111------------"+tempNum);
|
|
|
+ System.out.println("----------------3333-----------"+row.getCell(tempNum));
|
|
|
+ // 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象
|
|
|
+ if ((row.getCell(params.getKeyIndex()) == null && StringUtils.isNotEmpty(getKeyValue(row.getCell(params.getKeyIndex())))) && object != null) {
|
|
|
+ for (ExcelCollectionParams param : excelCollection) {
|
|
|
+ addListContinue(object, param, row, titlemap, targetId, pictures, params);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ object = PoiPublicUtil.createObject(pojoClass, targetId);
|
|
|
+ try {
|
|
|
+ for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
|
|
|
+ Cell cell = row.getCell(i);
|
|
|
+ String titleString = (String) titlemap.get(i);
|
|
|
+ if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) {
|
|
|
+ if (excelParams.get(titleString) != null && excelParams.get(titleString).getType() == 2) {
|
|
|
+ picId = row.getRowNum() + "_" + i;
|
|
|
+ saveImage(object, picId, excelParams, titleString, pictures, params);
|
|
|
+ } else {
|
|
|
+ saveFieldValue(params, object, cell, excelParams, titleString, row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (ExcelCollectionParams param : excelCollection) {
|
|
|
+ addListContinue(object, param, row, titlemap, targetId, pictures, params);
|
|
|
+ }
|
|
|
+ collection.add(object);
|
|
|
+ } catch (ExcelImportException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
|
|
|
+ throw new ExcelImportException(e.getType(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return collection;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取表格字段列名对应信息
|
|
|
*
|
|
@@ -310,6 +444,56 @@ public class ExcelImportServer extends ImportBaseService {
|
|
|
return new ExcelImportResult(result, verfiyFail, book);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean
|
|
|
+ *
|
|
|
+ * @param inputstream
|
|
|
+ * @param pojoClass
|
|
|
+ * @param params
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public ExcelImportResult importExcelByIs(DictHandle dictHandle, InputStream inputstream, Class<?> pojoClass, ImportParams params) throws Exception {
|
|
|
+ if (LOGGER.isDebugEnabled()) {
|
|
|
+ LOGGER.debug("Excel import start ,class is {}", pojoClass);
|
|
|
+ }
|
|
|
+ List<T> result = new ArrayList<T>();
|
|
|
+ Workbook book = null;
|
|
|
+ boolean isXSSFWorkbook = true;
|
|
|
+ if (!(inputstream.markSupported())) {
|
|
|
+ inputstream = new PushbackInputStream(inputstream, 8);
|
|
|
+ }
|
|
|
+ if (POIFSFileSystem.hasPOIFSHeader(inputstream)) {
|
|
|
+ book = new HSSFWorkbook(inputstream);
|
|
|
+ isXSSFWorkbook = false;
|
|
|
+ } else if (POIXMLDocument.hasOOXMLHeader(inputstream)) {
|
|
|
+ book = new XSSFWorkbook(OPCPackage.open(inputstream));
|
|
|
+ }
|
|
|
+ createErrorCellStyle(book);
|
|
|
+ Map<String, PictureData> pictures;
|
|
|
+ for (int i = 0; i < params.getSheetNum(); i++) {
|
|
|
+ if (LOGGER.isDebugEnabled()) {
|
|
|
+ LOGGER.debug(" start to read excel by is ,startTime is {}", System.currentTimeMillis());
|
|
|
+ }
|
|
|
+ if (isXSSFWorkbook) {
|
|
|
+ pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), (XSSFWorkbook) book);
|
|
|
+ } else {
|
|
|
+ pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), (HSSFWorkbook) book);
|
|
|
+ }
|
|
|
+ if (LOGGER.isDebugEnabled()) {
|
|
|
+ LOGGER.debug(" end to read excel by is ,endTime is {}", System.currentTimeMillis());
|
|
|
+ }
|
|
|
+ result.addAll(importExcel(dictHandle,result, book.getSheetAt(i), pojoClass, params, pictures));
|
|
|
+ if (LOGGER.isDebugEnabled()) {
|
|
|
+ LOGGER.debug(" end to read excel list by pos ,endTime is {}", System.currentTimeMillis());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (params.isNeedSave()) {
|
|
|
+ saveThisExcel(params, pojoClass, isXSSFWorkbook, book);
|
|
|
+ }
|
|
|
+ return new ExcelImportResult(result, verfiyFail, book);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 保存字段值(获取值,校验值,追加错误信息)
|
|
|
*
|