@@ -0,0 +1,46 @@ | |||
package cn.afterturn.easypoi.csv; | |||
import cn.afterturn.easypoi.csv.entity.CsvImportParams; | |||
import cn.afterturn.easypoi.csv.handler.ICsvSaveDataHandler; | |||
import cn.afterturn.easypoi.csv.imports.CsvImportService; | |||
import java.io.InputStream; | |||
import java.util.List; | |||
/** | |||
* CSV 导入工具类 | |||
* 具体和Excel类似,但是比Excel简单 | |||
* 需要处理一些字符串的处理 | |||
* | |||
* @author by jueyue on 18-10-3. | |||
*/ | |||
public final class CsvImportUtil { | |||
/** | |||
* Csv 导入流适合大数据导入 | |||
* 导入 数据源IO流,不返回校验结果 导入 字段类型 Integer,Long,Double,Date,String,Boolean | |||
* | |||
* @param inputstream | |||
* @param pojoClass | |||
* @param params | |||
* @return | |||
*/ | |||
public static <T> List<T> importCsv(InputStream inputstream, Class<?> pojoClass, | |||
CsvImportParams params) { | |||
return new CsvImportService().readExcel(inputstream, pojoClass, params, null); | |||
} | |||
/** | |||
* Csv 导入流适合大数据导入 | |||
* 导入 数据源IO流,不返回校验结果 导入 字段类型 Integer,Long,Double,Date,String,Boolean | |||
* | |||
* @param inputstream | |||
* @param pojoClass | |||
* @param params | |||
* @return | |||
*/ | |||
public static <T> List<T> importCsv(InputStream inputstream, Class<?> pojoClass, | |||
CsvImportParams params, ICsvSaveDataHandler saveDataHandler) { | |||
return new CsvImportService().readExcel(inputstream, pojoClass, params, saveDataHandler); | |||
} | |||
} |
@@ -0,0 +1,161 @@ | |||
package cn.afterturn.easypoi.csv.entity; | |||
import cn.afterturn.easypoi.excel.entity.ExcelBaseParams; | |||
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler; | |||
/** | |||
* CSV 导入参数 | |||
* | |||
* @author by jueyue on 18-10-3. | |||
*/ | |||
public class CsvImportParams extends ExcelBaseParams { | |||
public static final String UTF8 = "utf-8"; | |||
public static final String GBK = "gbk"; | |||
public static final String GB2312 = "gb2312"; | |||
private String encoding = UTF8; | |||
/** | |||
* 分隔符 | |||
*/ | |||
private String spiltMark = ","; | |||
/** | |||
* 字符串标识符 | |||
*/ | |||
private String textMark = "\""; | |||
/** | |||
* 表格标题行数,默认0 | |||
*/ | |||
private int titleRows = 0; | |||
/** | |||
* 表头行数,默认1 | |||
*/ | |||
private int headRows = 1; | |||
/** | |||
* 字段真正值和列标题之间的距离 默认0 | |||
*/ | |||
private int startRows = 0; | |||
/** | |||
* 校验组 | |||
*/ | |||
private Class[] verifyGroup = null; | |||
/** | |||
* 是否需要校验上传的Excel,默认为false | |||
*/ | |||
private boolean needVerify = false; | |||
/** | |||
* 校验处理接口 | |||
*/ | |||
private IExcelVerifyHandler verifyHandler; | |||
/** | |||
* 最后的无效行数 | |||
*/ | |||
private int lastOfInvalidRow = 0; | |||
/** | |||
* 主键设置,如何这个cell没有值,就跳过 或者认为这个是list的下面的值 | |||
* 大家不理解,去掉这个 | |||
*/ | |||
private Integer keyIndex = null; | |||
public CsvImportParams() { | |||
} | |||
public CsvImportParams(String encoding) { | |||
this.encoding = encoding; | |||
} | |||
public String getEncoding() { | |||
return encoding; | |||
} | |||
public void setEncoding(String encoding) { | |||
this.encoding = encoding; | |||
} | |||
public String getSpiltMark() { | |||
return spiltMark; | |||
} | |||
public void setSpiltMark(String spiltMark) { | |||
this.spiltMark = spiltMark; | |||
} | |||
public String getTextMark() { | |||
return textMark; | |||
} | |||
public void setTextMark(String textMark) { | |||
this.textMark = textMark; | |||
} | |||
public int getTitleRows() { | |||
return titleRows; | |||
} | |||
public void setTitleRows(int titleRows) { | |||
this.titleRows = titleRows; | |||
} | |||
public int getHeadRows() { | |||
return headRows; | |||
} | |||
public void setHeadRows(int headRows) { | |||
this.headRows = headRows; | |||
} | |||
public int getStartRows() { | |||
return startRows; | |||
} | |||
public void setStartRows(int startRows) { | |||
this.startRows = startRows; | |||
} | |||
public Class[] getVerifyGroup() { | |||
return verifyGroup; | |||
} | |||
public void setVerifyGroup(Class[] verifyGroup) { | |||
this.verifyGroup = verifyGroup; | |||
} | |||
public boolean isNeedVerify() { | |||
return needVerify; | |||
} | |||
public void setNeedVerify(boolean needVerify) { | |||
this.needVerify = needVerify; | |||
} | |||
public IExcelVerifyHandler getVerifyHandler() { | |||
return verifyHandler; | |||
} | |||
public void setVerifyHandler(IExcelVerifyHandler verifyHandler) { | |||
this.verifyHandler = verifyHandler; | |||
} | |||
public int getLastOfInvalidRow() { | |||
return lastOfInvalidRow; | |||
} | |||
public void setLastOfInvalidRow(int lastOfInvalidRow) { | |||
this.lastOfInvalidRow = lastOfInvalidRow; | |||
} | |||
public Integer getKeyIndex() { | |||
return keyIndex; | |||
} | |||
public void setKeyIndex(Integer keyIndex) { | |||
this.keyIndex = keyIndex; | |||
} | |||
} |
@@ -0,0 +1,17 @@ | |||
package cn.afterturn.easypoi.csv.handler; | |||
/** | |||
* 保存数据 | |||
* 鉴于CSV可能都是大数据,还是调用接口直接保存,避免内存占用 | |||
* | |||
* @author by jueyue on 18-10-3. | |||
*/ | |||
public interface ICsvSaveDataHandler<T> { | |||
/** | |||
* 保存数据 | |||
* | |||
* @param t | |||
*/ | |||
public void save(T t); | |||
} |
@@ -0,0 +1,291 @@ | |||
package cn.afterturn.easypoi.csv.imports; | |||
import cn.afterturn.easypoi.csv.entity.CsvImportParams; | |||
import cn.afterturn.easypoi.csv.handler.ICsvSaveDataHandler; | |||
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | |||
import cn.afterturn.easypoi.excel.entity.params.ExcelCollectionParams; | |||
import cn.afterturn.easypoi.excel.entity.params.ExcelImportEntity; | |||
import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; | |||
import cn.afterturn.easypoi.excel.imports.CellValueService; | |||
import cn.afterturn.easypoi.excel.imports.base.ImportBaseService; | |||
import cn.afterturn.easypoi.exception.excel.ExcelImportException; | |||
import cn.afterturn.easypoi.exception.excel.enums.ExcelImportEnum; | |||
import cn.afterturn.easypoi.handler.inter.IExcelModel; | |||
import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
import cn.afterturn.easypoi.util.PoiReflectorUtil; | |||
import cn.afterturn.easypoi.util.PoiValidationUtil; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||
import org.apache.poi.ss.usermodel.Cell; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.lang.reflect.Field; | |||
import java.util.*; | |||
/** | |||
* @author by jueyue on 18-10-3. | |||
*/ | |||
public class CsvImportService extends ImportBaseService { | |||
private final static Logger LOGGER = LoggerFactory.getLogger(CsvImportService.class); | |||
private CellValueService cellValueServer; | |||
private boolean verifyFail = false; | |||
public CsvImportService() { | |||
this.cellValueServer = new CellValueService(); | |||
} | |||
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, CsvImportParams params, ICsvSaveDataHandler saveDataHandler) { | |||
List collection = new ArrayList(); | |||
try { | |||
Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); | |||
List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | |||
String targetId = null; | |||
i18nHandler = params.getI18nHandler(); | |||
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, null); | |||
} | |||
BufferedReader rows = new BufferedReader(new InputStreamReader(inputstream, params.getEncoding())); | |||
for (int j = 0; j < params.getTitleRows(); j++) { | |||
rows.readLine(); | |||
} | |||
Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection, excelParams); | |||
int readRow = 0; | |||
//跳过无效行 | |||
for (int i = 0; i < params.getStartRows(); i++) { | |||
rows.readLine(); | |||
} | |||
//判断index 和集合,集合情况默认为第一列 | |||
if (excelCollection.size() > 0 && params.getKeyIndex() == null) { | |||
params.setKeyIndex(0); | |||
} | |||
StringBuilder errorMsg; | |||
String row = null; | |||
Object object = null; | |||
String[] cells; | |||
while ((row = rows.readLine()) != null) { | |||
errorMsg = new StringBuilder(); | |||
cells = row.split(params.getSpiltMark()); | |||
// 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象 | |||
// keyIndex 如果为空就不处理,仍然处理这一行 | |||
if (params.getKeyIndex() != null && (cells[params.getKeyIndex()] == null | |||
|| StringUtils.isEmpty(cells[params.getKeyIndex()])) | |||
&& object != null) { | |||
for (ExcelCollectionParams param : excelCollection) { | |||
addListContinue(object, param, row, titlemap, targetId, params, errorMsg); | |||
} | |||
} else { | |||
object = PoiPublicUtil.createObject(pojoClass, targetId); | |||
try { | |||
Set<Integer> keys = titlemap.keySet(); | |||
for (Integer cn : keys) { | |||
String titleString = (String) titlemap.get(cn); | |||
if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { | |||
try { | |||
saveFieldValue(params, object, cells[cn], excelParams, titleString); | |||
} catch (ExcelImportException e) { | |||
// 如果需要去校验就忽略,这个错误,继续执行 | |||
if (params.isNeedVerify() && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())) { | |||
errorMsg.append(" ").append(titleString).append(ExcelImportEnum.GET_VALUE_ERROR.getMsg()); | |||
} | |||
} | |||
} | |||
} | |||
for (ExcelCollectionParams param : excelCollection) { | |||
addListContinue(object, param, row, titlemap, targetId, params, errorMsg); | |||
} | |||
if (verifyingDataValidity(object, params, pojoClass, errorMsg)) { | |||
if (saveDataHandler != null) { | |||
saveDataHandler.save(object); | |||
} else { | |||
collection.add(object); | |||
} | |||
} | |||
} catch (ExcelImportException e) { | |||
LOGGER.error("excel import error , row num:{},obj:{}", readRow, ReflectionToStringBuilder.toString(object)); | |||
if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) { | |||
throw new ExcelImportException(e.getType(), e); | |||
} | |||
} catch (Exception e) { | |||
LOGGER.error("excel import error , row num:{},obj:{}", readRow, ReflectionToStringBuilder.toString(object)); | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
readRow++; | |||
} | |||
} catch (Exception e) { | |||
LOGGER.error(e.getMessage(), e); | |||
} | |||
return collection; | |||
} | |||
private void addListContinue(Object object, ExcelCollectionParams param, String row, | |||
Map<Integer, String> titlemap, String targetId, | |||
CsvImportParams params, StringBuilder errorMsg) throws Exception { | |||
Collection collection = (Collection) PoiReflectorUtil.fromCache(object.getClass()) | |||
.getValue(object, param.getName()); | |||
Object entity = PoiPublicUtil.createObject(param.getType(), targetId); | |||
// 是否需要加上这个对象 | |||
boolean isUsed = false; | |||
String[] cells = row.split(params.getSpiltMark()); | |||
for (int i = 0; i < cells.length; i++) { | |||
String cell = cells[i]; | |||
String titleString = (String) titlemap.get(i); | |||
if (param.getExcelParams().containsKey(titleString)) { | |||
try { | |||
saveFieldValue(params, entity, cell, param.getExcelParams(), titleString); | |||
} catch (ExcelImportException e) { | |||
// 如果需要去校验就忽略,这个错误,继续执行 | |||
if (params.isNeedVerify() && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())) { | |||
errorMsg.append(" ").append(titleString).append(ExcelImportEnum.GET_VALUE_ERROR.getMsg()); | |||
} | |||
} | |||
isUsed = true; | |||
} | |||
} | |||
if (isUsed) { | |||
collection.add(entity); | |||
} | |||
} | |||
/** | |||
* 校验数据合法性 | |||
*/ | |||
private boolean verifyingDataValidity(Object object, CsvImportParams params, | |||
Class<?> pojoClass, StringBuilder fieldErrorMsg) { | |||
boolean isAdd = true; | |||
Cell cell = null; | |||
if (params.isNeedVerify()) { | |||
String errorMsg = PoiValidationUtil.validation(object, params.getVerifyGroup()); | |||
if (StringUtils.isNotEmpty(errorMsg)) { | |||
if (object instanceof IExcelModel) { | |||
IExcelModel model = (IExcelModel) object; | |||
model.setErrorMsg(errorMsg); | |||
} | |||
isAdd = false; | |||
verifyFail = true; | |||
} | |||
} | |||
if (params.getVerifyHandler() != null) { | |||
ExcelVerifyHandlerResult result = params.getVerifyHandler().verifyHandler(object); | |||
if (!result.isSuccess()) { | |||
if (object instanceof IExcelModel) { | |||
IExcelModel model = (IExcelModel) object; | |||
model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg()) | |||
? model.getErrorMsg() + "," : "") + result.getMsg()); | |||
} | |||
isAdd = false; | |||
verifyFail = true; | |||
} | |||
} | |||
if ((params.isNeedVerify() || params.getVerifyHandler() != null) && fieldErrorMsg.length() > 0) { | |||
if (object instanceof IExcelModel) { | |||
IExcelModel model = (IExcelModel) object; | |||
model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg()) | |||
? model.getErrorMsg() + "," : "") + fieldErrorMsg.toString()); | |||
} | |||
isAdd = false; | |||
verifyFail = true; | |||
} | |||
return isAdd; | |||
} | |||
/** | |||
* 保存字段值(获取值,校验值,追加错误信息) | |||
*/ | |||
private void saveFieldValue(CsvImportParams params, Object object, String cell, | |||
Map<String, ExcelImportEntity> excelParams, String titleString) throws Exception { | |||
if (cell.startsWith(params.getTextMark()) && cell.endsWith(params.getTextMark())) { | |||
cell = cell.replaceFirst(cell, params.getTextMark()); | |||
cell = cell.substring(0, cell.lastIndexOf(params.getTextMark())); | |||
} | |||
Object value = cellValueServer.getValue(params.getDataHandler(), object, cell, excelParams, | |||
titleString, params.getDictHandler()); | |||
if (object instanceof Map) { | |||
if (params.getDataHandler() != null) { | |||
params.getDataHandler().setMapValue((Map) object, titleString, value); | |||
} else { | |||
((Map) object).put(titleString, value); | |||
} | |||
} else { | |||
setValues(excelParams.get(titleString), object, value); | |||
} | |||
} | |||
/** | |||
* 获取表格字段列名对应信息 | |||
*/ | |||
private Map<Integer, String> getTitleMap(BufferedReader rows, CsvImportParams params, | |||
List<ExcelCollectionParams> excelCollection, | |||
Map<String, ExcelImportEntity> excelParams) throws IOException { | |||
Map<Integer, String> titlemap = new LinkedHashMap<Integer, String>(); | |||
String collectionName = null; | |||
ExcelCollectionParams collectionParams = null; | |||
String row = null; | |||
String[] cellTitle; | |||
for (int j = 0; j < params.getHeadRows(); j++) { | |||
row = rows.readLine(); | |||
if (row == null) { | |||
continue; | |||
} | |||
cellTitle = row.split(params.getSpiltMark()); | |||
for (int i = 0; i < cellTitle.length; i++) { | |||
String value = cellTitle[i]; | |||
//用以支持重名导入 | |||
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); | |||
} | |||
} | |||
} | |||
} | |||
// 处理指定列的情况 | |||
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; | |||
} | |||
/** | |||
* 获取这个名称对应的集合信息 | |||
*/ | |||
private ExcelCollectionParams getCollectionParams(List<ExcelCollectionParams> excelCollection, | |||
String collectionName) { | |||
for (ExcelCollectionParams excelCollectionParams : excelCollection) { | |||
if (collectionName.equals(excelCollectionParams.getExcelName())) { | |||
return excelCollectionParams; | |||
} | |||
} | |||
return null; | |||
} | |||
} |
@@ -63,11 +63,11 @@ public class ImportParams extends ExcelBaseParams { | |||
/** | |||
* 校验组 | |||
*/ | |||
private Class[] verfiyGroup = null; | |||
private Class[] verifyGroup = null; | |||
/** | |||
* 是否需要校验上传的Excel,默认为false | |||
*/ | |||
private boolean needVerfiy = false; | |||
private boolean needVerify = false; | |||
/** | |||
* 校验处理接口 | |||
*/ | |||
@@ -135,7 +135,6 @@ public class ImportParams extends ExcelBaseParams { | |||
this.headRows = headRows; | |||
} | |||
@Deprecated | |||
public void setKeyIndex(Integer keyIndex) { | |||
this.keyIndex = keyIndex; | |||
} | |||
@@ -180,12 +179,12 @@ public class ImportParams extends ExcelBaseParams { | |||
this.startSheetIndex = startSheetIndex; | |||
} | |||
public boolean isNeedVerfiy() { | |||
return needVerfiy; | |||
public boolean isNeedVerify() { | |||
return needVerify; | |||
} | |||
public void setNeedVerfiy(boolean needVerfiy) { | |||
this.needVerfiy = needVerfiy; | |||
public void setNeedVerify(boolean needVerify) { | |||
this.needVerify = needVerify; | |||
} | |||
public String[] getImportFields() { | |||
@@ -224,12 +223,12 @@ public class ImportParams extends ExcelBaseParams { | |||
this.readSingleCell = readSingleCell; | |||
} | |||
public Class[] getVerfiyGroup() { | |||
return verfiyGroup; | |||
public Class[] getVerifyGroup() { | |||
return verifyGroup; | |||
} | |||
public void setVerfiyGroup(Class[] verfiyGroup) { | |||
this.verfiyGroup = verfiyGroup; | |||
public void setVerifyGroup(Class[] verifyGroup) { | |||
this.verifyGroup = verifyGroup; | |||
} | |||
public boolean isNeedCheckOrder() { | |||
@@ -16,6 +16,9 @@ | |||
package cn.afterturn.easypoi.excel.entity; | |||
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl; | |||
import org.apache.poi.ss.usermodel.Workbook; | |||
import java.io.InputStream; | |||
/** | |||
* 模板导出参数设置 | |||
@@ -34,6 +37,10 @@ public class TemplateExportParams extends ExcelBaseParams { | |||
* 模板的路径 | |||
*/ | |||
private String templateUrl; | |||
/** | |||
* 模板 | |||
*/ | |||
private Workbook templateWb; | |||
/** | |||
* 需要导出的第几个 sheetNum,默认是第0个 | |||
@@ -205,4 +212,11 @@ public class TemplateExportParams extends ExcelBaseParams { | |||
this.colForEach = colForEach; | |||
} | |||
public void setTemplateWb(Workbook templateWb) { | |||
this.templateWb = templateWb; | |||
} | |||
public Workbook getTemplateWb() { | |||
return templateWb; | |||
} | |||
} |
@@ -1,13 +1,13 @@ | |||
/** | |||
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* <p> | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* <p> | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* <p> | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
@@ -59,13 +59,13 @@ import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||
*/ | |||
public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
private static final Logger LOGGER = LoggerFactory | |||
private static final Logger LOGGER = LoggerFactory | |||
.getLogger(ExcelExportOfTemplateUtil.class); | |||
/** | |||
* 缓存TEMP 的for each创建的cell ,跳过这个cell的模板语法查找,提高效率 | |||
*/ | |||
private Set<String> tempCreateCellSet = new HashSet<String>(); | |||
private Set<String> tempCreateCellSet = new HashSet<String>(); | |||
/** | |||
* 模板参数,全局都用到 | |||
*/ | |||
@@ -73,9 +73,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
/** | |||
* 单元格合并信息 | |||
*/ | |||
private MergedRegionHelper mergedRegionHelper; | |||
private MergedRegionHelper mergedRegionHelper; | |||
private TemplateSumHandler templateSumHandler; | |||
private TemplateSumHandler templateSumHandler; | |||
/** | |||
* 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射 | |||
@@ -159,8 +159,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
if (isShift && datas.size() * rowspan > 1 && cell.getRowIndex() + rowspan < cell.getRow().getSheet().getLastRowNum()) { | |||
cell.getRow().getSheet().shiftRows(cell.getRowIndex() + rowspan, | |||
cell.getRow().getSheet().getLastRowNum(), (datas.size() - 1) * rowspan, true, true); | |||
mergedRegionHelper.shiftRows(cell.getSheet(),cell.getRowIndex() + rowspan,(datas.size() - 1) * rowspan); | |||
templateSumHandler.shiftRows(cell.getRowIndex() + rowspan,(datas.size() - 1) * rowspan); | |||
mergedRegionHelper.shiftRows(cell.getSheet(), cell.getRowIndex() + rowspan, (datas.size() - 1) * rowspan); | |||
templateSumHandler.shiftRows(cell.getRowIndex() + rowspan, (datas.size() - 1) * rowspan); | |||
} | |||
while (its.hasNext()) { | |||
@@ -202,7 +202,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||
entity = excelParams.get(k); | |||
if (entity.getList() != null) { | |||
Collection<?> list = (Collection<?>) entity.getMethod().invoke(t, new Object[] {}); | |||
Collection<?> list = (Collection<?>) entity.getMethod().invoke(t, new Object[]{}); | |||
if (list != null && list.size() > maxHeight) { | |||
maxHeight = list.size(); | |||
} | |||
@@ -215,14 +215,18 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
public Workbook createExcleByTemplate(TemplateExportParams params, Class<?> pojoClass, | |||
Collection<?> dataSet, Map<String, Object> map) { | |||
// step 1. 判断模板的地址 | |||
if (params == null || map == null || StringUtils.isEmpty(params.getTemplateUrl())) { | |||
if (params == null || map == null || (StringUtils.isEmpty(params.getTemplateUrl()) || params.getTemplateWb() == null)) { | |||
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||
} | |||
Workbook wb = null; | |||
// step 2. 判断模板的Excel类型,解析模板 | |||
try { | |||
this.teplateParams = params; | |||
wb = getCloneWorkBook(); | |||
if (params.getTemplateWb() != null) { | |||
wb = params.getTemplateWb(); | |||
} else { | |||
wb = getCloneWorkBook(); | |||
} | |||
if (wb instanceof XSSFWorkbook) { | |||
super.type = ExcelType.XSSF; | |||
} | |||
@@ -380,7 +384,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
Object t = its.next(); | |||
setForEeachRowCellValue(true, cell.getRow(), cell.getColumnIndex(), t, columns, map, | |||
rowspan, colspan, mergedRegionHelper); | |||
if(cell.getRow().getCell(cell.getColumnIndex() + colspan) == null){ | |||
if (cell.getRow().getCell(cell.getColumnIndex() + colspan) == null) { | |||
cell.getRow().createCell(cell.getColumnIndex() + colspan); | |||
} | |||
cell = cell.getRow().getCell(cell.getColumnIndex() + colspan); | |||
@@ -452,15 +456,15 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
Object obj = PoiPublicUtil.getRealValue(oldString, map); | |||
//如何是数值 类型,就按照数值类型进行设置// 如果是图片就设置为图片 | |||
if (obj instanceof ImageEntity) { | |||
ImageEntity img = (ImageEntity)obj; | |||
ImageEntity img = (ImageEntity) obj; | |||
cell.setCellValue(""); | |||
if (img.getRowspan()>1 || img.getColspan() > 1){ | |||
if (img.getRowspan() > 1 || img.getColspan() > 1) { | |||
img.setHeight(0); | |||
PoiMergeCellUtil.addMergedRegion(cell.getSheet(),cell.getRowIndex(), | |||
cell.getRowIndex() + img.getRowspan() - 1, cell.getColumnIndex(), cell.getColumnIndex() + img.getColspan() -1); | |||
PoiMergeCellUtil.addMergedRegion(cell.getSheet(), cell.getRowIndex(), | |||
cell.getRowIndex() + img.getRowspan() - 1, cell.getColumnIndex(), cell.getColumnIndex() + img.getColspan() - 1); | |||
} | |||
createImageCell(cell,img.getHeight(),img.getUrl(),img.getData()); | |||
}else if (isNumber && StringUtils.isNotBlank(obj.toString())) { | |||
createImageCell(cell, img.getHeight(), img.getUrl(), img.getData()); | |||
} else if (isNumber && StringUtils.isNotBlank(obj.toString())) { | |||
cell.setCellValue(Double.parseDouble(obj.toString())); | |||
cell.setCellType(CellType.NUMERIC); | |||
} else { | |||
@@ -530,13 +534,13 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
for (int k = 0; k < rowspan; k++) { | |||
int ci = columnIndex;//cell的序号 | |||
short high=columns.get(0).getHeight(); | |||
int n=k; | |||
while (n>0) { | |||
if ( columns.get(n * colspan).getHeight()==0) { | |||
short high = columns.get(0).getHeight(); | |||
int n = k; | |||
while (n > 0) { | |||
if (columns.get(n * colspan).getHeight() == 0) { | |||
n--; | |||
} else { | |||
high= columns.get(n * colspan).getHeight(); | |||
high = columns.get(n * colspan).getHeight(); | |||
break; | |||
} | |||
} | |||
@@ -569,11 +573,11 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
obj = eval(tempStr, map); | |||
val = obj.toString(); | |||
} | |||
if (obj != null && obj instanceof ImageEntity) { | |||
ImageEntity img = (ImageEntity)obj; | |||
if (obj != null && obj instanceof ImageEntity) { | |||
ImageEntity img = (ImageEntity) obj; | |||
row.getCell(ci).setCellValue(""); | |||
createImageCell(row.getCell(ci),img.getHeight(),img.getUrl(),img.getData()); | |||
}else if (isNumber && StringUtils.isNotEmpty(val)) { | |||
createImageCell(row.getCell(ci), img.getHeight(), img.getUrl(), img.getData()); | |||
} else if (isNumber && StringUtils.isNotEmpty(val)) { | |||
row.getCell(ci).setCellValue(Double.parseDouble(val)); | |||
row.getCell(ci).setCellType(CellType.NUMERIC); | |||
} else { | |||
@@ -594,8 +598,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
if ((params.getRowspan() != 1 || params.getColspan() != 1) | |||
&& !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci)) { | |||
PoiMergeCellUtil.addMergedRegion(row.getSheet(), row.getRowNum(), | |||
row.getRowNum() + params.getRowspan() - 1, ci, | |||
ci + params.getColspan() - 1); | |||
row.getRowNum() + params.getRowspan() - 1, ci, | |||
ci + params.getColspan() - 1); | |||
} | |||
ci = ci + params.getColspan(); | |||
} | |||
@@ -644,8 +648,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
//保存col 的开始列 | |||
int startIndex = cell.getColumnIndex(); | |||
Row row = cell.getRow(); | |||
while (index < row.getLastCellNum()) { | |||
int colSpan = columns.get(columns.size() - 1) != null | |||
while (index < row.getLastCellNum()) { | |||
int colSpan = columns.get(columns.size() - 1) != null | |||
? columns.get(columns.size() - 1).getColspan() : 1; | |||
index += colSpan; | |||
@@ -682,7 +686,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
columns.add(getExcelTemplateParams(cellStringString.replace(END_STR, EMPTY), | |||
cell, mergedRegionHelper)); | |||
//补全缺失的cell(合并单元格后面的) | |||
int lastCellColspan =columns.get(columns.size() - 1).getColspan(); | |||
int lastCellColspan = columns.get(columns.size() - 1).getColspan(); | |||
for (int i = 1; i < lastCellColspan; i++) { | |||
//添加合并的单元格,这些单元可能不是空,但是没有值,所以也需要跳过 | |||
columns.add(null); | |||
@@ -707,7 +711,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
colspan += columns.get(i) != null ? columns.get(i).getColspan() : 0; | |||
} | |||
colspan = colspan / rowspan; | |||
return new Object[] { rowspan, colspan, columns }; | |||
return new Object[]{rowspan, colspan, columns}; | |||
} | |||
/** | |||
@@ -180,6 +180,41 @@ public class CellValueService { | |||
return null; | |||
} | |||
/** | |||
* 获取cell的值 | |||
* @param object | |||
* @param cell | |||
* @param excelParams | |||
* @param titleString | |||
* @param dictHandler | |||
*/ | |||
public Object getValue(IExcelDataHandler<?> dataHandler, Object object, String cell, | |||
Map<String, ExcelImportEntity> excelParams, | |||
String titleString, IExcelDictHandler dictHandler) throws Exception { | |||
ExcelImportEntity entity = excelParams.get(titleString); | |||
String classFullName = "class java.lang.Object"; | |||
Class clazz = null; | |||
if (!(object instanceof Map)) { | |||
Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 | |||
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod(); | |||
Type[] ts = setMethod.getGenericParameterTypes(); | |||
classFullName = ts[0].toString(); | |||
clazz = (Class) ts[0]; | |||
} | |||
Object result = cell; | |||
if (entity != null) { | |||
result = handlerSuffix(entity.getSuffix(), result); | |||
result = replaceValue(entity.getReplace(), result); | |||
result = replaceValue(entity.getReplace(), result); | |||
if (dictHandler != null && StringUtils.isNoneBlank(entity.getDict())) { | |||
dictHandler.toValue(entity.getDict(), object, entity.getName(), result); | |||
} | |||
} | |||
result = handlerValue(dataHandler, object, result, titleString); | |||
return getValueByType(classFullName, result, entity, clazz); | |||
} | |||
/** | |||
* 获取cell的值 | |||
* @param object | |||
@@ -38,7 +38,6 @@ 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.*; | |||
@@ -69,7 +68,7 @@ public class ExcelImportService extends ImportBaseService { | |||
private CellValueService cellValueServer; | |||
private boolean verfiyFail = false; | |||
private boolean verifyFail = false; | |||
/** | |||
* 异常数据styler | |||
*/ | |||
@@ -121,7 +120,7 @@ public class ExcelImportService extends ImportBaseService { | |||
saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row); | |||
} catch (ExcelImportException e) { | |||
// 如果需要去校验就忽略,这个错误,继续执行 | |||
if(params.isNeedVerfiy() && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())){ | |||
if(params.isNeedVerify() && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())){ | |||
errorMsg.append(" ").append(titleString).append(ExcelImportEnum.GET_VALUE_ERROR.getMsg()); | |||
} | |||
} | |||
@@ -235,7 +234,7 @@ public class ExcelImportService extends ImportBaseService { | |||
saveFieldValue(params, object, cell, excelParams, titleString, row); | |||
} catch (ExcelImportException e) { | |||
// 如果需要去校验就忽略,这个错误,继续执行 | |||
if(params.isNeedVerfiy() && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())){ | |||
if(params.isNeedVerify() && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())){ | |||
errorMsg.append(" ").append(titleString).append(ExcelImportEnum.GET_VALUE_ERROR.getMsg()); | |||
} | |||
} | |||
@@ -278,8 +277,8 @@ public class ExcelImportService extends ImportBaseService { | |||
Class<?> pojoClass, StringBuilder fieldErrorMsg) { | |||
boolean isAdd = true; | |||
Cell cell = null; | |||
if (params.isNeedVerfiy()) { | |||
String errorMsg = PoiValidationUtil.validation(object, params.getVerfiyGroup()); | |||
if (params.isNeedVerify()) { | |||
String errorMsg = PoiValidationUtil.validation(object, params.getVerifyGroup()); | |||
if (StringUtils.isNotEmpty(errorMsg)) { | |||
cell = row.createCell(row.getLastCellNum()); | |||
cell.setCellValue(errorMsg); | |||
@@ -288,7 +287,7 @@ public class ExcelImportService extends ImportBaseService { | |||
model.setErrorMsg(errorMsg); | |||
} | |||
isAdd = false; | |||
verfiyFail = true; | |||
verifyFail = true; | |||
} | |||
} | |||
if (params.getVerifyHandler() != null) { | |||
@@ -305,10 +304,10 @@ public class ExcelImportService extends ImportBaseService { | |||
? model.getErrorMsg() + "," : "") + result.getMsg()); | |||
} | |||
isAdd = false; | |||
verfiyFail = true; | |||
verifyFail = true; | |||
} | |||
} | |||
if((params.isNeedVerfiy() || params.getVerifyHandler() != null) && fieldErrorMsg.length() > 0){ | |||
if((params.isNeedVerify() || params.getVerifyHandler() != null) && fieldErrorMsg.length() > 0){ | |||
if (object instanceof IExcelModel) { | |||
IExcelModel model = (IExcelModel) object; | |||
model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg()) | |||
@@ -320,7 +319,7 @@ public class ExcelImportService extends ImportBaseService { | |||
cell.setCellValue((StringUtils.isNoneBlank(cell.getStringCellValue()) | |||
? cell.getStringCellValue() + "," : "")+ fieldErrorMsg.toString()); | |||
isAdd = false; | |||
verfiyFail = true; | |||
verifyFail = true; | |||
} | |||
if (cell != null) { | |||
cell.setCellStyle(errorCellStyle); | |||
@@ -467,7 +466,7 @@ public class ExcelImportService extends ImportBaseService { | |||
importResult.setWorkbook(removeSuperfluousRows(successBook, failRow, params)); | |||
importResult.setFailWorkbook(removeSuperfluousRows(book, successRow, params)); | |||
importResult.setFailList(failCollection); | |||
importResult.setVerfiyFail(verfiyFail); | |||
importResult.setVerfiyFail(verifyFail); | |||
} finally { | |||
successIs.close(); | |||
} | |||
@@ -1,40 +0,0 @@ | |||
/** | |||
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |||
* in compliance with the License. You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software distributed under the License | |||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |||
* or implied. See the License for the specific language governing permissions and limitations under | |||
* the License. | |||
*/ | |||
package cn.afterturn.easypoi.word.entity; | |||
import cn.afterturn.easypoi.entity.ImageEntity; | |||
/** | |||
* word导出,图片设置和图片信息 | |||
* | |||
* @author JueYue | |||
* 2013-11-17 | |||
* @version 1.0 | |||
*/ | |||
@Deprecated | |||
public class WordImageEntity extends ImageEntity { | |||
public WordImageEntity() { | |||
} | |||
public WordImageEntity(byte[] data, int width, int height) { | |||
super(data, width, height); | |||
} | |||
public WordImageEntity(String url, int width, int height) { | |||
super(url, width, height); | |||
} | |||
} |