소스 검색

导入加了readRows 的 参数

4.1.3.A
jueyue 8 년 전
부모
커밋
6065e8f77d
8개의 변경된 파일504개의 추가작업 그리고 448개의 파일을 삭제
  1. +1
    -0
      .gitignore
  2. +12
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java
  3. +2
    -1
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/TemplateSumHanlder.java
  4. +430
    -441
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java
  5. +4
    -3
      easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiCellUtil.java
  6. +33
    -0
      easypoi-web/pom.xml
  7. +10
    -2
      easypoi-web/src/main/java/org/jeecgframework/poi/excel/view/PoiBaseView.java
  8. +12
    -1
      pom.xml

+ 1
- 0
.gitignore 파일 보기

@@ -8,3 +8,4 @@ deploy.bat
/pom_bak.xml
/deploy_sing.bat
/build/
/bin/

+ 12
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java 파일 보기

@@ -71,6 +71,10 @@ public class ImportParams extends ExcelBaseParams {
* 最后的无效行数
*/
private int lastOfInvalidRow = 0;
/**
* 手动控制读取的行数
*/
private int readRows = 0;
/**
* 导入时校验数据模板,是不是正确的Excel
*/
@@ -172,4 +176,12 @@ public class ImportParams extends ExcelBaseParams {
this.importFields = importFields;
}
public int getReadRows() {
return readRows;
}
public void setReadRows(int readRows) {
this.readRows = readRows;
}
}

+ 2
- 1
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/TemplateSumHanlder.java 파일 보기

@@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.jeecgframework.poi.util.PoiCellUtil;
import static org.jeecgframework.poi.util.PoiElUtil.*;
@@ -40,7 +41,7 @@ public class TemplateSumHanlder {
continue;
}
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
if (row.getCell(i) != null && row.getCell(i).getStringCellValue().contains(SUM)) {
if (row.getCell(i) != null && PoiCellUtil.getCellValue(row.getCell(i)).contains(SUM)) {
addSumCellToList(row.getCell(i));
}
}


+ 430
- 441
easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java 파일 보기

@@ -55,6 +55,7 @@ import org.jeecgframework.poi.excel.imports.base.ImportBaseService;
import org.jeecgframework.poi.exception.excel.ExcelImportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum;
import org.jeecgframework.poi.handler.inter.IExcelModel;
import org.jeecgframework.poi.util.PoiCellUtil;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.jeecgframework.poi.util.PoiReflectorUtil;
import org.jeecgframework.poi.util.PoiValidationUtil;
@@ -70,464 +71,452 @@ import org.slf4j.LoggerFactory;
@SuppressWarnings({ "rawtypes", "unchecked", "hiding" })
public class ExcelImportServer extends ImportBaseService {
private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class);
private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class);
private CellValueServer cellValueServer;
private CellValueServer cellValueServer;
private boolean verfiyFail = false;
/**
* 异常数据styler
*/
private CellStyle errorCellStyle;
private boolean verfiyFail = false;
/**
* 异常数据styler
*/
private CellStyle errorCellStyle;
public ExcelImportServer() {
this.cellValueServer = new CellValueServer();
}
public ExcelImportServer() {
this.cellValueServer = new CellValueServer();
}
/***
* 向List里面继续添加元素
*
* @param object
* @param param
* @param row
* @param titlemap
* @param targetId
* @param pictures
* @param params
*/
private void addListContinue(Object object, ExcelCollectionParams param, Row row,
Map<Integer, String> titlemap, String targetId,
Map<String, PictureData> pictures,
ImportParams params) throws Exception {
Collection collection = (Collection) PoiReflectorUtil.fromCache(object.getClass())
.getValue(object, param.getName());
Object entity = PoiPublicUtil.createObject(param.getType(), targetId);
String picId;
boolean isUsed = false;// 是否需要加上这个对象
for (int i = row.getFirstCellNum(); i < param.getExcelParams().size(); i++) {
Cell cell = row.getCell(i);
String titleString = (String) titlemap.get(i);
if (param.getExcelParams().containsKey(titleString)) {
if (param.getExcelParams().get(titleString).getType() == 2) {
picId = row.getRowNum() + "_" + i;
saveImage(object, picId, param.getExcelParams(), titleString, pictures, params);
} else {
saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row);
}
isUsed = true;
}
}
if (isUsed) {
collection.add(entity);
}
}
/***
* 向List里面继续添加元素
*
* @param object
* @param param
* @param row
* @param titlemap
* @param targetId
* @param pictures
* @param params
*/
private void addListContinue(Object object, ExcelCollectionParams param, Row row,
Map<Integer, String> titlemap, String targetId,
Map<String, PictureData> pictures,
ImportParams params) throws Exception {
Collection collection = (Collection) PoiReflectorUtil.fromCache(object.getClass())
.getValue(object, param.getName());
Object entity = PoiPublicUtil.createObject(param.getType(), targetId);
String picId;
boolean isUsed = false;// 是否需要加上这个对象
for (int i = row.getFirstCellNum(); i < param.getExcelParams().size(); i++) {
Cell cell = row.getCell(i);
String titleString = (String) titlemap.get(i);
if (param.getExcelParams().containsKey(titleString)) {
if (param.getExcelParams().get(titleString).getType() == 2) {
picId = row.getRowNum() + "_" + i;
saveImage(object, picId, param.getExcelParams(), titleString, pictures, params);
} else {
saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row);
}
isUsed = true;
}
}
if (isUsed) {
collection.add(entity);
}
}
/**
* 获取key的值,针对不同类型获取不同的值
*
* @author JueYue
* 2013-11-21
* @param cell
* @return
*/
private String getKeyValue(Cell cell) {
Object obj = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
obj = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
obj = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
obj = cell.getCellFormula();
break;
default:
cell.setCellType(Cell.CELL_TYPE_STRING);
obj = cell.getStringCellValue();
}
return obj == null ? null : obj.toString().trim();
}
/**
* 获取key的值,针对不同类型获取不同的值
*
* @author JueYue
* 2013-11-21
* @param cell
* @return
*/
private String getKeyValue(Cell cell) {
Object obj = PoiCellUtil.getCellValue(cell);
return obj == null ? null : obj.toString().trim();
}
/**
* 获取保存的真实路径
*
* @param excelImportEntity
* @param object
* @return
* @throws Exception
*/
private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception {
String url = "";
if (excelImportEntity.getSaveUrl().equals("upload")) {
if (excelImportEntity.getMethods() != null
&& excelImportEntity.getMethods().size() > 0) {
object = getFieldBySomeMethod(excelImportEntity.getMethods(), object);
}
url = object.getClass().getName()
.split("\\.")[object.getClass().getName().split("\\.").length - 1];
return excelImportEntity.getSaveUrl() + "/" + url;
}
return excelImportEntity.getSaveUrl();
}
/**
* 获取保存的真实路径
*
* @param excelImportEntity
* @param object
* @return
* @throws Exception
*/
private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception {
String url = "";
if (excelImportEntity.getSaveUrl().equals("upload")) {
if (excelImportEntity.getMethods() != null
&& excelImportEntity.getMethods().size() > 0) {
object = getFieldBySomeMethod(excelImportEntity.getMethods(), object);
}
url = object.getClass().getName()
.split("\\.")[object.getClass().getName().split("\\.").length - 1];
return excelImportEntity.getSaveUrl() + "/" + url;
}
return excelImportEntity.getSaveUrl();
}
private <T> List<T> importExcel(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<String, ExcelImportEntity>();
List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>();
String targetId = null;
if (!Map.class.equals(pojoClass)) {
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
if (etarget != null) {
targetId = etarget.value();
}
getAllExcelField(targetId, fileds, 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);
checkIsValidTemplate(titlemap, excelParams, params, excelCollection);
Row row = null;
Object object = null;
String picId;
while (rows.hasNext()
&& (row == null
|| sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) {
row = rows.next();
// 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象
// keyIndex 如果为空就不处理,仍然处理这一行
if (params.getKeyIndex() != null
&& (row.getCell(params.getKeyIndex()) == null
|| StringUtils.isEmpty(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 = titlemap.size(); 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);
}
}
}
private <T> List<T> importExcel(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<String, ExcelImportEntity>();
List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>();
String targetId = null;
if (!Map.class.equals(pojoClass)) {
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
if (etarget != null) {
targetId = etarget.value();
}
getAllExcelField(targetId, fileds, 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);
checkIsValidTemplate(titlemap, excelParams, params, excelCollection);
Row row = null;
Object object = null;
String picId;
int readRow = 0;
while (rows.hasNext()
&& (row == null
|| sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) {
if (params.getReadRows() > 0 && readRow > params.getReadRows()) {
break;
}
row = rows.next();
// 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象
// keyIndex 如果为空就不处理,仍然处理这一行
if (params.getKeyIndex() != null
&& (row.getCell(params.getKeyIndex()) == null
|| StringUtils.isEmpty(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 = titlemap.size(); 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);
}
if (verifyingDataValidity(object, row, params, pojoClass)) {
collection.add(object);
}
} catch (ExcelImportException e) {
if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
throw new ExcelImportException(e.getType(), e);
}
}
}
}
return collection;
}
for (ExcelCollectionParams param : excelCollection) {
addListContinue(object, param, row, titlemap, targetId, pictures, params);
}
if (verifyingDataValidity(object, row, params, pojoClass)) {
collection.add(object);
}
} catch (ExcelImportException e) {
if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
throw new ExcelImportException(e.getType(), e);
}
}
}
readRow++;
}
return collection;
}
/**
* 校验数据合法性
* @param object
* @param row
* @param params
* @param pojoClass
* @return
*/
private boolean verifyingDataValidity(Object object, Row row, ImportParams params,
Class<?> pojoClass) {
boolean isAdd = true;
Cell cell = null;
if (params.isNeedVerfiy()) {
String errorMsg = PoiValidationUtil.validation(object);
if (StringUtils.isNotEmpty(errorMsg)) {
cell = row.createCell(row.getLastCellNum());
cell.setCellValue(errorMsg);
if (object instanceof IExcelModel) {
IExcelModel model = (IExcelModel) object;
model.setErrorMsg(errorMsg);
} else {
isAdd = false;
}
verfiyFail = true;
}
}
if (params.getVerifyHanlder() != null) {
ExcelVerifyHanlderResult result = params.getVerifyHanlder().verifyHandler(object);
if (!result.isSuccess()) {
if (cell == null)
cell = row.createCell(row.getLastCellNum());
cell.setCellValue((StringUtils.isNoneBlank(cell.getStringCellValue())
? cell.getStringCellValue() + "," : "") + result.getMsg());
if (object instanceof IExcelModel) {
IExcelModel model = (IExcelModel) object;
model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg())
? model.getErrorMsg() + "," : "") + result.getMsg());
} else {
isAdd = false;
}
verfiyFail = true;
}
}
if (cell != null)
cell.setCellStyle(errorCellStyle);
return isAdd;
}
/**
* 校验数据合法性
* @param object
* @param row
* @param params
* @param pojoClass
* @return
*/
private boolean verifyingDataValidity(Object object, Row row, ImportParams params,
Class<?> pojoClass) {
boolean isAdd = true;
Cell cell = null;
if (params.isNeedVerfiy()) {
String errorMsg = PoiValidationUtil.validation(object);
if (StringUtils.isNotEmpty(errorMsg)) {
cell = row.createCell(row.getLastCellNum());
cell.setCellValue(errorMsg);
if (object instanceof IExcelModel) {
IExcelModel model = (IExcelModel) object;
model.setErrorMsg(errorMsg);
} else {
isAdd = false;
}
verfiyFail = true;
}
}
if (params.getVerifyHanlder() != null) {
ExcelVerifyHanlderResult result = params.getVerifyHanlder().verifyHandler(object);
if (!result.isSuccess()) {
if (cell == null)
cell = row.createCell(row.getLastCellNum());
cell.setCellValue((StringUtils.isNoneBlank(cell.getStringCellValue())
? cell.getStringCellValue() + "," : "") + result.getMsg());
if (object instanceof IExcelModel) {
IExcelModel model = (IExcelModel) object;
model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg())
? model.getErrorMsg() + "," : "") + result.getMsg());
} else {
isAdd = false;
}
verfiyFail = true;
}
}
if (cell != null)
cell.setCellStyle(errorCellStyle);
return isAdd;
}
/**
* 获取表格字段列名对应信息
* @param rows
* @param params
* @param excelCollection
* @return
*/
private Map<Integer, String> getTitleMap(Iterator<Row> rows, ImportParams params,
List<ExcelCollectionParams> excelCollection) {
Map<Integer, String> titlemap = new HashMap<Integer, String>();
Iterator<Cell> cellTitle;
String collectionName = null;
ExcelCollectionParams collectionParams = null;
Row row = null;
for (int j = 0; j < params.getHeadRows(); j++) {
row = rows.next();
if (row == null) {
continue;
}
cellTitle = row.cellIterator();
while (cellTitle.hasNext()) {
Cell cell = cellTitle.next();
String value = getKeyValue(cell);
value = value.replace("\n", "");
int i = cell.getColumnIndex();
//用以支持重名导入
if (StringUtils.isNotEmpty(value)) {
if (titlemap.containsKey(i)) {
collectionName = titlemap.get(i);
collectionParams = getCollectionParams(excelCollection, collectionName);
titlemap.put(i, collectionName + "_" + value);
} else if (StringUtils.isNotEmpty(collectionName) && collectionParams != null
&& collectionParams.getExcelParams()
.containsKey(collectionName + "_" + value)) {
titlemap.put(i, collectionName + "_" + value);
} else {
collectionName = null;
collectionParams = null;
}
if (StringUtils.isEmpty(collectionName)) {
titlemap.put(i, value);
}
}
}
}
return titlemap;
}
/**
* 获取表格字段列名对应信息
* @param rows
* @param params
* @param excelCollection
* @return
*/
private Map<Integer, String> getTitleMap(Iterator<Row> rows, ImportParams params,
List<ExcelCollectionParams> excelCollection) {
Map<Integer, String> titlemap = new HashMap<Integer, String>();
Iterator<Cell> cellTitle;
String collectionName = null;
ExcelCollectionParams collectionParams = null;
Row row = null;
for (int j = 0; j < params.getHeadRows(); j++) {
row = rows.next();
if (row == null) {
continue;
}
cellTitle = row.cellIterator();
while (cellTitle.hasNext()) {
Cell cell = cellTitle.next();
String value = getKeyValue(cell);
value = value.replace("\n", "");
int i = cell.getColumnIndex();
//用以支持重名导入
if (StringUtils.isNotEmpty(value)) {
if (titlemap.containsKey(i)) {
collectionName = titlemap.get(i);
collectionParams = getCollectionParams(excelCollection, collectionName);
titlemap.put(i, collectionName + "_" + value);
} else if (StringUtils.isNotEmpty(collectionName) && collectionParams != null
&& collectionParams.getExcelParams()
.containsKey(collectionName + "_" + value)) {
titlemap.put(i, collectionName + "_" + value);
} else {
collectionName = null;
collectionParams = null;
}
if (StringUtils.isEmpty(collectionName)) {
titlemap.put(i, value);
}
}
}
}
return titlemap;
}
/**
* 获取这个名称对应的集合信息
* @param excelCollection
* @param collectionName
* @return
*/
private ExcelCollectionParams getCollectionParams(List<ExcelCollectionParams> excelCollection,
String collectionName) {
for (ExcelCollectionParams excelCollectionParams : excelCollection) {
if (collectionName.equals(excelCollectionParams.getExcelName())) {
return excelCollectionParams;
}
}
return null;
}
/**
* 获取这个名称对应的集合信息
* @param excelCollection
* @param collectionName
* @return
*/
private ExcelCollectionParams getCollectionParams(List<ExcelCollectionParams> excelCollection,
String collectionName) {
for (ExcelCollectionParams excelCollectionParams : excelCollection) {
if (collectionName.equals(excelCollectionParams.getExcelName())) {
return excelCollectionParams;
}
}
return null;
}
/**
* Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean
*
* @param inputstream
* @param pojoClass
* @param params
* @return
* @throws Exception
*/
public ExcelImportResult importExcelByIs(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 = params.getStartSheetIndex(); i < params.getStartSheetIndex()
+ params.getSheetNum(); i++) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime());
}
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 {}", new Date().getTime());
}
result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime());
}
}
if (params.isNeedSave()) {
saveThisExcel(params, pojoClass, isXSSFWorkbook, book);
}
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(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 = params.getStartSheetIndex(); i < params.getStartSheetIndex()
+ params.getSheetNum(); i++) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime());
}
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 {}", new Date().getTime());
}
result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime());
}
}
if (params.isNeedSave()) {
saveThisExcel(params, pojoClass, isXSSFWorkbook, book);
}
return new ExcelImportResult(result, verfiyFail, book);
}
/**
* 检查是不是合法的模板
* @param titlemap
* @param excelParams
* @param params
* @param excelCollection
*/
private void checkIsValidTemplate(Map<Integer, String> titlemap,
Map<String, ExcelImportEntity> excelParams,
ImportParams params,
List<ExcelCollectionParams> excelCollection) {
/**
* 检查是不是合法的模板
* @param titlemap
* @param excelParams
* @param params
* @param excelCollection
*/
private void checkIsValidTemplate(Map<Integer, String> titlemap,
Map<String, ExcelImportEntity> excelParams,
ImportParams params,
List<ExcelCollectionParams> excelCollection) {
if (params.getImportFields() != null) {
for (int i = 0, le = params.getImportFields().length; i < le; i++) {
if (!titlemap.containsValue(params.getImportFields()[i])) {
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
} else {
Collection<ExcelImportEntity> collection = excelParams.values();
for (ExcelImportEntity excelImportEntity : collection) {
if (excelImportEntity.isImportField()
&& !titlemap.containsValue(excelImportEntity.getName())) {
LOGGER.error(excelImportEntity.getName() + "必须有,但是没找到");
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
if (params.getImportFields() != null) {
for (int i = 0, le = params.getImportFields().length; i < le; i++) {
if (!titlemap.containsValue(params.getImportFields()[i])) {
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
} else {
Collection<ExcelImportEntity> collection = excelParams.values();
for (ExcelImportEntity excelImportEntity : collection) {
if (excelImportEntity.isImportField()
&& !titlemap.containsValue(excelImportEntity.getName())) {
LOGGER.error(excelImportEntity.getName() + "必须有,但是没找到");
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
for (int i = 0, le = excelCollection.size(); i < le; i++) {
ExcelCollectionParams collectionparams = excelCollection.get(i);
collection = collectionparams.getExcelParams().values();
for (ExcelImportEntity excelImportEntity : collection) {
if (excelImportEntity.isImportField() && !titlemap.containsValue(
collectionparams.getExcelName() + "_" + excelImportEntity.getName())) {
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
}
}
}
for (int i = 0, le = excelCollection.size(); i < le; i++) {
ExcelCollectionParams collectionparams = excelCollection.get(i);
collection = collectionparams.getExcelParams().values();
for (ExcelImportEntity excelImportEntity : collection) {
if (excelImportEntity.isImportField() && !titlemap.containsValue(
collectionparams.getExcelName() + "_" + excelImportEntity.getName())) {
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
}
}
}
/**
* 保存字段值(获取值,校验值,追加错误信息)
*
* @param params
* @param object
* @param cell
* @param excelParams
* @param titleString
* @param row
* @throws Exception
*/
private void saveFieldValue(ImportParams params, Object object, Cell cell,
Map<String, ExcelImportEntity> excelParams, String titleString,
Row row) throws Exception {
Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams,
titleString);
if (object instanceof Map) {
if (params.getDataHanlder() != null) {
params.getDataHanlder().setMapValue((Map) object, titleString, value);
} else {
((Map) object).put(titleString, value);
}
} else {
setValues(excelParams.get(titleString), object, value);
}
}
/**
* 保存字段值(获取值,校验值,追加错误信息)
*
* @param params
* @param object
* @param cell
* @param excelParams
* @param titleString
* @param row
* @throws Exception
*/
private void saveFieldValue(ImportParams params, Object object, Cell cell,
Map<String, ExcelImportEntity> excelParams, String titleString,
Row row) throws Exception {
Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams,
titleString);
if (object instanceof Map) {
if (params.getDataHanlder() != null) {
params.getDataHanlder().setMapValue((Map) object, titleString, value);
} else {
((Map) object).put(titleString, value);
}
} else {
setValues(excelParams.get(titleString), object, value);
}
}
/**
*
* @param object
* @param picId
* @param excelParams
* @param titleString
* @param pictures
* @param params
* @throws Exception
*/
private void saveImage(Object object, String picId, Map<String, ExcelImportEntity> excelParams,
String titleString, Map<String, PictureData> pictures,
ImportParams params) throws Exception {
if (pictures == null) {
return;
}
PictureData image = pictures.get(picId);
if (image == null) {
return;
}
byte[] data = image.getData();
String fileName = "pic" + Math.round(Math.random() * 100000000000L);
fileName += "." + PoiPublicUtil.getFileExtendName(data);
if (excelParams.get(titleString).getSaveType() == 1) {
String path = PoiPublicUtil
.getWebRootPath(getSaveUrl(excelParams.get(titleString), object));
File savefile = new File(path);
if (!savefile.exists()) {
savefile.mkdirs();
}
savefile = new File(path + "/" + fileName);
FileOutputStream fos = new FileOutputStream(savefile);
try {
fos.write(data);
} finally {
IOUtils.closeQuietly(fos);
}
setValues(excelParams.get(titleString), object,
getSaveUrl(excelParams.get(titleString), object) + "/" + fileName);
} else {
setValues(excelParams.get(titleString), object, data);
}
}
/**
*
* @param object
* @param picId
* @param excelParams
* @param titleString
* @param pictures
* @param params
* @throws Exception
*/
private void saveImage(Object object, String picId, Map<String, ExcelImportEntity> excelParams,
String titleString, Map<String, PictureData> pictures,
ImportParams params) throws Exception {
if (pictures == null) {
return;
}
PictureData image = pictures.get(picId);
if (image == null) {
return;
}
byte[] data = image.getData();
String fileName = "pic" + Math.round(Math.random() * 100000000000L);
fileName += "." + PoiPublicUtil.getFileExtendName(data);
if (excelParams.get(titleString).getSaveType() == 1) {
String path = PoiPublicUtil
.getWebRootPath(getSaveUrl(excelParams.get(titleString), object));
File savefile = new File(path);
if (!savefile.exists()) {
savefile.mkdirs();
}
savefile = new File(path + "/" + fileName);
FileOutputStream fos = new FileOutputStream(savefile);
try {
fos.write(data);
} finally {
IOUtils.closeQuietly(fos);
}
setValues(excelParams.get(titleString), object,
getSaveUrl(excelParams.get(titleString), object) + "/" + fileName);
} else {
setValues(excelParams.get(titleString), object, data);
}
}
private void createErrorCellStyle(Workbook workbook) {
errorCellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setColor(Font.COLOR_RED);
errorCellStyle.setFont(font);
}
private void createErrorCellStyle(Workbook workbook) {
errorCellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setColor(Font.COLOR_RED);
errorCellStyle.setFont(font);
}
}

+ 4
- 3
easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiCellUtil.java 파일 보기

@@ -100,7 +100,6 @@ public class PoiCellUtil {
* @return
*/
public static String getCellValue(Cell cell) {

if (cell == null)
return "";

@@ -120,8 +119,10 @@ public class PoiCellUtil {

return String.valueOf(cell.getNumericCellValue());

} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
return cell.getStringCellValue();
}

return "";
}

}

+ 33
- 0
easypoi-web/pom.xml 파일 보기

@@ -7,4 +7,37 @@
<version>2.3.2-SNAPSHOT</version>
</parent>
<artifactId>easypoi-web</artifactId>
<dependencies>
<!--spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-base</artifactId>
</dependency>
</dependencies>
</project>

+ 10
- 2
easypoi-web/src/main/java/org/jeecgframework/poi/excel/view/PoiBaseView.java 파일 보기

@@ -25,6 +25,8 @@ import org.jeecgframework.poi.excel.entity.vo.MapExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.MapExcelGraphConstants;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.AbstractView;
/**
@@ -32,6 +34,8 @@ import org.springframework.web.servlet.view.AbstractView;
*/
public abstract class PoiBaseView extends AbstractView {
private static final Logger LOGGER = LoggerFactory.getLogger(PoiBaseView.class);
protected static boolean isIE(HttpServletRequest request) {
return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0
|| request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0
@@ -40,7 +44,7 @@ public abstract class PoiBaseView extends AbstractView {
}
public static void render(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response, String viewName) throws Exception {
HttpServletResponse response, String viewName) {
PoiBaseView view = null;
if (BigExcelConstants.BIG_EXCEL_VIEW.equals(viewName)) {
view = new BigExcelExportView();
@@ -53,7 +57,11 @@ public abstract class PoiBaseView extends AbstractView {
} else if (MapExcelGraphConstants.MAP_GRAPH_EXCEL_VIEW.equals(viewName)) {
view = new MapGraphExcelView();
}
view.renderMergedOutputModel(model, request, response);
try {
view.renderMergedOutputModel(model, request, response);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}

+ 12
- 1
pom.xml 파일 보기

@@ -209,5 +209,16 @@
</plugins>
</build>
<distributionManagement>
<repository>
<id>postaop-pay-platform-release</id>
<url>http://postaop.oicp.net:8081/nexus/content/repositories/postaop-common-release/</url>
</repository>
<snapshotRepository>
<id>postaop-pay-platform-snapshot</id>
<url>http://postaop.oicp.net:8081/nexus/content/repositories/postaop-common-snapshot/</url>
</snapshotRepository>
</distributionManagement>
</project>

불러오는 중...
취소
저장