Java源码示例:org.jeecgframework.poi.exception.excel.ExcelImportException
示例1
/**
* 保存字段值(获取值,校验值,追加错误信息)
*
* @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 {
ExcelVerifyHanlderResult verifyResult = verifyHandlerServer.verifyData(object, value, titleString, excelParams.get(titleString).getVerify(), params.getVerifyHanlder());
if (verifyResult.isSuccess()) {
setValues(excelParams.get(titleString), object, value);
} else {
Cell errorCell = row.createCell(row.getLastCellNum());
errorCell.setCellValue(verifyResult.getMsg());
errorCell.setCellStyle(errorCellStyle);
verfiyFail = true;
throw new ExcelImportException(ExcelImportEnum.VERIFY_ERROR);
}
}
}
示例2
@Override
public void parse(int index, List<SaxReadCellEntity> datas) {
try {
if (datas == null || datas.size() == 0) {
return;
}
// 标题行跳过
if (index < params.getTitleRows()) {
return;
}
// 表头行
if (index < params.getTitleRows() + params.getHeadRows()) {
addHeadData(datas);
} else {
addListData(datas);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例3
/**
* 保存字段值(获取值,校验值,追加错误信息)
*
* @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 {
ExcelVerifyHanlderResult verifyResult = verifyHandlerServer.verifyData(object, value, titleString, excelParams.get(titleString).getVerify(), params.getVerifyHanlder());
if (verifyResult.isSuccess()) {
setValues(excelParams.get(titleString), object, value);
} else {
Cell errorCell = row.createCell(row.getLastCellNum());
errorCell.setCellValue(verifyResult.getMsg());
errorCell.setCellStyle(errorCellStyle);
verfiyFail = true;
throw new ExcelImportException(ExcelImportEnum.VERIFY_ERROR);
}
}
}
示例4
@Override
public void parse(int index, List<SaxReadCellEntity> datas) {
try {
if (datas == null || datas.size() == 0) {
return;
}
// 标题行跳过
if (index < params.getTitleRows()) {
return;
}
// 表头行
if (index < params.getTitleRows() + params.getHeadRows()) {
addHeadData(datas);
} else {
addListData(datas);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例5
/**
* 保存字段值(获取值,校验值,追加错误信息)
*
* @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 {
ExcelVerifyHanlderResult verifyResult = verifyHandlerServer.verifyData(object, value,
titleString, excelParams.get(titleString).getVerify(), params.getVerifyHanlder());
if (verifyResult.isSuccess()) {
setValues(excelParams.get(titleString), object, value);
} else {
Cell errorCell = row.createCell(row.getLastCellNum());
errorCell.setCellValue(verifyResult.getMsg());
errorCell.setCellStyle(errorCellStyle);
verfiyFail = true;
throw new ExcelImportException(ExcelImportEnum.VERIFY_ERROR);
}
}
}
示例6
@Override
public void parse(int index, List<SaxReadCellEntity> datas) {
try {
if (datas == null || datas.size() == 0) {
return;
}
//标题行跳过
if (index < params.getTitleRows()) {
return;
}
//表头行
if (index < params.getTitleRows() + params.getHeadRows()) {
addHeadData(datas);
} else {
addListData(datas);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例7
/**
* 获取日期类型数据
*
* @Author JEECG
* @date 2013年11月26日
* @param entity
* @param value
* @return
*/
private Date getDateData(ExcelImportEntity entity, String value) {
if (StringUtils.isNotEmpty(entity.getFormat()) && StringUtils.isNotEmpty(value)) {
SimpleDateFormat format = new SimpleDateFormat(entity.getFormat());
try {
return format.parse(value);
} catch (ParseException e) {
LOGGER.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value);
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR);
}
}
return null;
}
示例8
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params, ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) {
try {
OPCPackage opcPackage = OPCPackage.open(inputstream);
return readExcel(opcPackage, pojoClass, params, rowRead, hanlder);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例9
private void initParams(Class<?> pojoClass, ImportParams params) {
try {
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
if (etarget != null) {
targetId = etarget.value();
}
getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例10
/**
* 获取日期类型数据
*
* @Author JueYue
* @date 2013年11月26日
* @param entity
* @param value
* @return
*/
private Date getDateData(ExcelImportEntity entity, String value) {
if (StringUtils.isNotEmpty(entity.getFormat()) && StringUtils.isNotEmpty(value)) {
SimpleDateFormat format = new SimpleDateFormat(entity.getFormat());
try {
return format.parse(value);
} catch (ParseException e) {
LOGGER.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value);
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR);
}
}
return null;
}
示例11
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params, ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) {
try {
OPCPackage opcPackage = OPCPackage.open(inputstream);
return readExcel(opcPackage, pojoClass, params, rowRead, hanlder);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例12
private void initParams(Class<?> pojoClass, ImportParams params) {
try {
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
if (etarget != null) {
targetId = etarget.value();
}
getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例13
/**
* 获取日期类型数据
*
* @Author JueYue
* @date 2013年11月26日
* @param entity
* @param value
* @return
*/
private Date getDateData(ExcelImportEntity entity, String value) {
if (StringUtils.isNotEmpty(entity.getFormat()) && StringUtils.isNotEmpty(value)) {
SimpleDateFormat format = new SimpleDateFormat(entity.getFormat());
try {
return format.parse(value);
} catch (ParseException e) {
LOGGER.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value);
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR);
}
}
return null;
}
示例14
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params,
ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) {
try {
OPCPackage opcPackage = OPCPackage.open(inputstream);
return readExcel(opcPackage, pojoClass, params, rowRead, hanlder);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例15
private void initParams(Class<?> pojoClass, ImportParams params) {
try {
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
if (etarget != null) {
targetId = etarget.value();
}
getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(e.getMessage());
}
}
示例16
/**
* 根据返回类型获取返回值
*
* @param xclass
* @param result
* @param entity
* @return
*/
private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) {
try {
if ("class java.util.Date".equals(xclass)) {
return result;
}
if ("class java.lang.Boolean".equals(xclass) || "boolean".equals(xclass)) {
return Boolean.valueOf(String.valueOf(result));
}
if ("class java.lang.Double".equals(xclass) || "double".equals(xclass)) {
return Double.valueOf(String.valueOf(result));
}
if ("class java.lang.Long".equals(xclass) || "long".equals(xclass)) {
return Long.valueOf(String.valueOf(result));
}
if ("class java.lang.Float".equals(xclass) || "float".equals(xclass)) {
return Float.valueOf(String.valueOf(result));
}
if ("class java.lang.Integer".equals(xclass) || "int".equals(xclass)) {
return Integer.valueOf(String.valueOf(result));
}
if ("class java.math.BigDecimal".equals(xclass)) {
return new BigDecimal(String.valueOf(result));
}
if ("class java.lang.String".equals(xclass)) {
// 针对String 类型,但是Excel获取的数据却不是String,比如Double类型,防止科学计数法
if (result instanceof String) {
return result;
}
// double类型防止科学计数法
if (result instanceof Double) {
return PoiPublicUtil.doubleToString((Double) result);
}
return String.valueOf(result);
}
return result;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR);
}
}
示例17
/**
* 根据返回类型获取返回值
*
* @param xclass
* @param result
* @param entity
* @return
*/
private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) {
try {
if ("class java.util.Date".equals(xclass)) {
return result;
}
if ("class java.lang.Boolean".equals(xclass) || "boolean".equals(xclass)) {
return Boolean.valueOf(String.valueOf(result));
}
if ("class java.lang.Double".equals(xclass) || "double".equals(xclass)) {
return Double.valueOf(String.valueOf(result));
}
if ("class java.lang.Long".equals(xclass) || "long".equals(xclass)) {
return Long.valueOf(String.valueOf(result));
}
if ("class java.lang.Float".equals(xclass) || "float".equals(xclass)) {
return Float.valueOf(String.valueOf(result));
}
if ("class java.lang.Integer".equals(xclass) || "int".equals(xclass)) {
return Integer.valueOf(String.valueOf(result));
}
if ("class java.math.BigDecimal".equals(xclass)) {
return new BigDecimal(String.valueOf(result));
}
if ("class java.lang.String".equals(xclass)) {
//针对String 类型,但是Excel获取的数据却不是String,比如Double类型,防止科学计数法
if (result instanceof String) {
return result;
}
// double类型防止科学计数法
if (result instanceof Double) {
return PoiPublicUtil.doubleToString((Double) result);
}
return String.valueOf(result);
}
return result;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR);
}
}