Browse Source

导入制定列,导入,解决复杂表头的导入

4.1.3.A
qrb.jueyue@gmail.com 7 years ago
parent
commit
9de2cb5d5c
6 changed files with 74 additions and 28 deletions
  1. +6
    -0
      easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java
  2. +1
    -1
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelExportUtil.java
  3. +4
    -4
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelImportUtil.java
  4. +12
    -1
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java
  5. +45
    -21
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportService.java
  6. +6
    -1
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java

+ 6
- 0
easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java View File

@@ -150,4 +150,10 @@ public @interface Excel {
* @return
*/
public String isImportField() default "false";
/**
* 固定的某一列,解决不好解析的问题
* @return
*/
public int fixedIndex() default -1;
}

+ 1
- 1
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelExportUtil.java View File

@@ -100,7 +100,7 @@ public class ExcelExportUtil {
* Excel对象数据List
*/
public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,
Collection<? extends Map<?, ?>> dataSet) {
Collection<?> dataSet) {
Workbook workbook = getWorkbook(entity.getType(),dataSet.size());;
new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet);
return workbook;


+ 4
- 4
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelImportUtil.java View File

@@ -59,7 +59,7 @@ public class ExcelImportUtil {
FileInputStream in = null;
try {
in = new FileInputStream(file);
return new ExcelImportService().importExcelByIs(in, pojoClass, params).getList();
return new ExcelImportService().importExcelByIs(in, pojoClass, params, false).getList();
} catch (ExcelImportException e) {
throw new ExcelImportException(e.getType(), e);
} catch (Exception e) {
@@ -81,7 +81,7 @@ public class ExcelImportUtil {
*/
public static <T> List<T> importExcel(InputStream inputstream, Class<?> pojoClass,
ImportParams params) throws Exception {
return new ExcelImportService().importExcelByIs(inputstream, pojoClass, params).getList();
return new ExcelImportService().importExcelByIs(inputstream, pojoClass, params, false).getList();
}
/**
@@ -97,7 +97,7 @@ public class ExcelImportUtil {
public static <T> ExcelImportResult<T> importExcelMore(InputStream inputstream,
Class<?> pojoClass,
ImportParams params) throws Exception {
return new ExcelImportService().importExcelByIs(inputstream, pojoClass, params);
return new ExcelImportService().importExcelByIs(inputstream, pojoClass, params, true);
}
/**
@@ -113,7 +113,7 @@ public class ExcelImportUtil {
FileInputStream in = null;
try {
in = new FileInputStream(file);
return new ExcelImportService().importExcelByIs(in, pojoClass, params);
return new ExcelImportService().importExcelByIs(in, pojoClass, params, true);
} catch (ExcelImportException e) {
throw new ExcelImportException(e.getType(), e);
} catch (Exception e) {


+ 12
- 1
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java View File

@@ -57,7 +57,11 @@ public class ExcelBaseEntity {
/**
* 这个是不是超链接,如果是需要实现接口返回对象
*/
private boolean hyperlink;
private boolean hyperlink;
/**
* 固定的列
*/
private Integer fixedIndex;
private List<Method> methods;
@@ -134,4 +138,11 @@ public class ExcelBaseEntity {
this.hyperlink = hyperlink;
}
public int getFixedIndex() {
return fixedIndex;
}
public void setFixedIndex(int fixedIndex) {
this.fixedIndex = fixedIndex;
}
}

+ 45
- 21
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportService.java View File

@@ -17,6 +17,10 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
@@ -37,14 +41,9 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import cn.afterturn.easypoi.excel.entity.ImportParams;
@@ -173,7 +172,7 @@ public class ExcelImportService extends ImportBaseService {
for (int j = 0; j < params.getTitleRows(); j++) {
rows.next();
}
Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection);
Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection,excelParams);
checkIsValidTemplate(titlemap, excelParams, params, excelCollection);
Row row = null;
Object object = null;
@@ -206,13 +205,14 @@ public class ExcelImportService extends ImportBaseService {
} else {
object = PoiPublicUtil.createObject(pojoClass, targetId);
try {
for (int i = row.getFirstCellNum(), le = titlemap.size(); i < le; i++) {
Cell cell = row.getCell(i);
String titleString = (String) titlemap.get(i);
Set<Integer> keys = titlemap.keySet();
for (Integer cn:keys) {
Cell cell = row.getCell(cn);
String titleString = (String) titlemap.get(cn);
if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) {
if (excelParams.get(titleString) != null
&& excelParams.get(titleString).getType() == 2) {
picId = row.getRowNum() + "_" + i;
picId = row.getRowNum() + "_" + cn;
saveImage(object, picId, excelParams, titleString, pictures,
params);
} else {
@@ -220,6 +220,9 @@ public class ExcelImportService extends ImportBaseService {
}
}
}
//for (int i = row.getFirstCellNum(), le = titlemap.size(); i < le; i++) {

//}

for (ExcelCollectionParams param : excelCollection) {
addListContinue(object, param, row, titlemap, targetId, pictures, params);
@@ -294,7 +297,8 @@ public class ExcelImportService extends ImportBaseService {
* 获取表格字段列名对应信息
*/
private Map<Integer, String> getTitleMap(Iterator<Row> rows, ImportParams params,
List<ExcelCollectionParams> excelCollection) {
List<ExcelCollectionParams> excelCollection,
Map<String, ExcelImportEntity> excelParams) {
Map<Integer, String> titlemap = new LinkedHashMap<Integer, String>();
Iterator<Cell> cellTitle;
String collectionName = null;
@@ -331,6 +335,15 @@ public class ExcelImportService extends ImportBaseService {
}
}
}

// 处理指定列的情况
Set<String> keys = excelParams.keySet();
for (String key : keys){
if (key.startsWith("FIXED_")){
String [] arr = key.split("_");
titlemap.put(Integer.parseInt(arr[1]),key);
}
}
return titlemap;
}

@@ -351,7 +364,7 @@ public class ExcelImportService extends ImportBaseService {
* Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean
*/
public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass,
ImportParams params) throws Exception {
ImportParams params, boolean needMore) throws Exception {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Excel import start ,class is {}", pojoClass);
}
@@ -365,8 +378,15 @@ public class ExcelImportService extends ImportBaseService {
baos.flush();

InputStream userIs = new ByteArrayInputStream(baos.toByteArray());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Excel clone success");
}
Workbook book = WorkbookFactory.create(userIs);

boolean isXSSFWorkbook = !(book instanceof HSSFWorkbook);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Workbook create success");
}
ExcelImportResult importResult = new ExcelImportResult();
createErrorCellStyle(book);
Map<String, PictureData> pictures;
@@ -387,7 +407,7 @@ public class ExcelImportService extends ImportBaseService {
}
result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" end to read excel list by pos ,endTime is {}", System.currentTimeMillis());
LOGGER.debug(" end to read excel list by sheet ,endTime is {}", System.currentTimeMillis());
}
if (params.isReadSingleCell()) {
readSingleCell(importResult, book.getSheetAt(i), params);
@@ -399,13 +419,17 @@ public class ExcelImportService extends ImportBaseService {
if (params.isNeedSave()) {
saveThisExcel(params, pojoClass, isXSSFWorkbook, book);
}
InputStream successIs = new ByteArrayInputStream(baos.toByteArray());
Workbook successBook = WorkbookFactory.create(successIs);
importResult.setWorkbook(removeSuperfluousRows(successBook, failRow, params));
importResult.setFailWorkbook(removeSuperfluousRows(book, successRow, params));
importResult.setList(result);
importResult.setFailList(failCollection);
importResult.setVerfiyFail(verfiyFail);
if (needMore) {
InputStream successIs = new ByteArrayInputStream(baos.toByteArray());
Workbook successBook = WorkbookFactory.create(successIs);
importResult.setWorkbook(removeSuperfluousRows(successBook, failRow, params));
importResult.setFailWorkbook(removeSuperfluousRows(book, successRow, params));
importResult.setFailList(failCollection);
importResult.setVerfiyFail(verfiyFail);
successIs.close();
}
baos.close();
return importResult;
}



+ 6
- 1
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java View File

@@ -76,6 +76,7 @@ public class ImportBaseService {
excelEntity.setSuffix(excel.suffix());
excelEntity.setImportField(Boolean
.valueOf(PoiPublicUtil.getValueByTargetId(excel.isImportField(), targetId, "false")));
excelEntity.setFixedIndex(excel.fixedIndex());
getExcelField(targetId, field, excelEntity, excel, pojoClass, excelEntityAnn);
if (getMethods != null) {
List<Method> newMethods = new ArrayList<Method>();
@@ -83,7 +84,11 @@ public class ImportBaseService {
newMethods.add(excelEntity.getMethod());
excelEntity.setMethods(newMethods);
}
temp.put(excelEntity.getName(), excelEntity);
if(excelEntity.getFixedIndex() != -1){
temp.put("FIXED_"+excelEntity.getFixedIndex() , excelEntity);
} else {
temp.put(excelEntity.getName(), excelEntity);
}
}


Loading…
Cancel
Save