起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: dh_n

[结贴] 导入Excel报错

[复制链接]

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
36185
发表于 2018-1-4 09:47:21 | 显示全部楼层
/BIZ/SA/excel/logic/code/src/com/justep/excel/ImportGenerator.java这个文件也发一下,报错的行平台默认的这行没有代码
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

213

主题

790

帖子

2056

积分

金牌会员

Rank: 6Rank: 6

积分
2056
QQ
 楼主| 发表于 2018-1-4 10:42:21 | 显示全部楼层
  1. package com.justep.excel;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.math.BigDecimal;
  7. import java.text.DecimalFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Locale;
  14. import java.util.Map;

  15. import javax.swing.JOptionPane;

  16. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  19. import org.apache.poi.ss.usermodel.Sheet;
  20. import org.apache.poi.ss.usermodel.Workbook;
  21. import org.dom4j.Document;
  22. import org.dom4j.DocumentException;
  23. import org.dom4j.Element;
  24. import org.dom4j.io.SAXReader;

  25. import com.justep.message.SAMessages;
  26. import com.justep.system.action.ActionUtils;
  27. import com.justep.system.context.ActionContext;
  28. import com.justep.system.data.Row;
  29. import com.justep.system.data.Table;
  30. import com.justep.system.transform.SimpleTransform;
  31. import com.justep.util.Utils;

  32. public class ImportGenerator {
  33.         private Workbook workBook = null;
  34.         private FormulaEvaluator evaluator = null;
  35.         private Element configE = null;
  36.         private ImportConfig importConfig = null;
  37.         private String excelFileName = null;
  38.         private String configFileName = null;

  39.         public ImportGenerator() {
  40.         }

  41.         public static void generateBefore(Workbook excel, ImportConfig importConfig) {
  42.                 //调用事件接口,没有代码
  43.         }

  44.         public static void generateAfter(Table data, Workbook excel, ImportConfig importConfig) {
  45.                 //调用事件接口,没有代码
  46.         }

  47.         private void init(Document config) {
  48.                 configE = config.getRootElement();
  49.                 excelFileName = configE.attributeValue("excel-file");
  50.                 configFileName = configE.attributeValue("mapping-file");
  51.                 LoadExcel();
  52.                 loadMapping();
  53.         }

  54.         private void LoadExcel() {
  55.                 try {
  56.                         FileInputStream fis = new FileInputStream(excelFileName);
  57.                         workBook = org.apache.poi.ss.usermodel.WorkbookFactory.create(fis);
  58.                         evaluator = workBook.getCreationHelper().createFormulaEvaluator();
  59.                 } catch (FileNotFoundException e) {
  60.                         throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_LOAD_EXCEL_ERROR);
  61.                 } catch (IOException e) {
  62.                         throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_EXCEL_FILE_ERROR);
  63.                 } catch (InvalidFormatException e) {
  64.                         throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_EXCEL_FILE_ERROR);
  65.                 }
  66.         }

  67.         private void loadMapping() {
  68.                 if (Utils.isNotEmptyString(configFileName)) {
  69.                         SAXReader reader = new SAXReader();
  70.                         Document doc = null;
  71.                         try {
  72.                                 doc = reader.read(configFileName);
  73.                         } catch (DocumentException e) {
  74.                                 throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_LOAD_EXCEL_MAPPING_ERROR);
  75.                         }
  76.                         importConfig = new ImportConfig(doc.getRootElement(), configE);
  77.                 } else
  78.                         throw ExcelException.create(SAMessages.IMPORT_EXCEL_EXCEL_MAPPING_NOT_EXISTS);
  79.         }

  80.         private Table getCreateActionTable(Map<String, Object> createActionParam) {
  81.                 if (Utils.isNull(createActionParam))
  82.                         return null;
  83.                 @SuppressWarnings("unchecked")
  84.                 Map<String, Object> parameters = (Map<String, Object>) createActionParam.get("parameters");
  85.                 return (Table) parameters.get("table");
  86.         }

  87.         private void setSaveActionTable(Map<String, Object> saveActionParam, Table t) {
  88.                 if (Utils.isNotNull(saveActionParam)) {
  89.                         @SuppressWarnings("unchecked")
  90.                         Map<String, Object> parameters = (Map<String, Object>) saveActionParam.get("parameters");
  91.                         parameters.put("table", t);
  92.                 }
  93.         }

  94.         @SuppressWarnings("unchecked")
  95.         public Object generate(Document config, Map<String, Object> createActionParam, Map<String, Object> saveActionParam) {
  96.                 try {
  97.                         init(config);
  98.                         ImportConfig importConfig = getImportConfig();

  99.                         //使用action机制给出控制接口
  100.                         ActionContext actionContext = ActionUtils.getRequestContext().getActionContext();
  101.                         String process = actionContext.getProcess().getFullName();
  102.                         String activity = actionContext.getActivity().getName();
  103.                         String executor = actionContext.getExecutor();
  104.                         String executeContext = actionContext.getExecuteContext();
  105.                         Map<String, Object> p = new HashMap<String, Object>();
  106.                         p.put("excel", workBook);
  107.                         p.put("config", importConfig);
  108.                         System.out.println(process+"|"+activity+"|"+Const.IMPORT_BEFORE_ACTION+"|"+executor+"|"+executeContext+"|"+p);
  109.                         ActionUtils.invokeAction(process, activity, Const.IMPORT_BEFORE_ACTION, executor, executeContext, p);

  110.                         ImportRange sheetRange = importConfig.getSheetRange(workBook.getNumberOfSheets());

  111.                         Table t = getCreateActionTable(createActionParam);
  112.                         if (Utils.isNull(t)){
  113.                                 return null;
  114.                         }
  115.                                

  116.                         for (int i = sheetRange.getStart() - 1; i <= sheetRange.getEnd() - 1; i++) {
  117.                                 ImportRange rowRange = importConfig.getRowRange(i, workBook);
  118.                                 generateSheet(i, rowRange.getStart() - 1, rowRange.getEnd() - 1, t);
  119.                         }
  120.                        
  121.                         if(t.size()>0)//有数据才进行新增
  122.                                 t = (Table) com.justep.system.action.ActionUtils.invokeAction((String) createActionParam.get("process"),
  123.                                         (String) createActionParam.get("activity"), (String) createActionParam.get("action"), (String) createActionParam.get("executor"),
  124.                                         (Map<String, Object>) createActionParam.get("parameters"));

  125.                         p.put("data", t);
  126.                         ActionUtils.invokeAction(process, activity, Const.IMPORT_AFTER_ACTION, executor, executeContext, p);
  127.                         //JOptionPane.showMessageDialog(null,"导入"+t.size()+"行","提示",JOptionPane.ERROR_MESSAGE);
  128.                         if (Const.KIND_UI.equalsIgnoreCase(importConfig.getToKind())) {
  129.                                 return t;
  130.                         } else if (Const.KIND_DB.equalsIgnoreCase(importConfig.getToKind())) {
  131.                                 setSaveActionTable(saveActionParam, t);
  132.                                 return com.justep.system.action.ActionUtils.invokeAction((String) saveActionParam.get("process"),
  133.                                                 (String) saveActionParam.get("activity"), (String) saveActionParam.get("action"), (String) saveActionParam.get("executor"),
  134.                                                 (Map<String, Object>) saveActionParam.get("parameters"));
  135.                         }
  136.                         return null;
  137.                 } finally {
  138.                         File file = new File(excelFileName);
  139.                         if (file.exists())
  140.                                 file.delete();
  141.                         file = new File(configFileName);
  142.                         if (file.exists())
  143.                                 file.delete();
  144.                 }
  145.         }

  146.         private boolean cellIsBlank(Cell cell){
  147.                 if(null==cell) return true;
  148.                 int cellType = cell.getCellType();
  149.                 if (Cell.CELL_TYPE_FORMULA == cellType)
  150.                         cellType = evaluator.evaluateFormulaCell(cell);
  151.                 return cellType==Cell.CELL_TYPE_BLANK;
  152.         }
  153.        
  154.         /*
  155.          * 判断导入的excel行数据是否有效
  156.          */
  157.         private boolean isValidRowData(org.apache.poi.ss.usermodel.Row row, ImportConfig importConfig) {
  158.                 if (null != row) {
  159.                         ImportConceptConfig conceptConfig = importConfig.getConceptConfig();
  160.                         for (int i=0;i<conceptConfig.getChecks().size();i++){
  161.                                 if(conceptConfig.getChecks().get(i)){
  162.                                         Cell cell = row.getCell(conceptConfig.getCellNumbers().get(i));
  163.                                         if(cellIsBlank(cell)) return false;
  164.                                 }
  165.                         }
  166.                         List<ImportRelationConfig> relationConfigs = conceptConfig.getRelationConfigs();
  167.                         for (ImportRelationConfig rc : relationConfigs) {
  168.                                 if(rc.isCheck()){
  169.                                         Cell cell = row.getCell(rc.getExcelCellIndex());
  170.                                         if(cellIsBlank(cell)) return false;
  171.                                 }
  172.                         }
  173.                         return true;
  174.                 }else return false;
  175.         }

  176.         private void generateSheet(int sheetIndex, int rowStart, int rowEnd, Table t) {
  177.                 Sheet sheet = this.workBook.getSheetAt(sheetIndex);
  178.                 for (int i = rowStart; i <= rowEnd; i++) {
  179.                         org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
  180.                         if (!isValidRowData(row, getImportConfig()))
  181.                                 continue;
  182.                         generateRecord(t.appendRow(), row, sheetIndex, i);
  183.                 }
  184.         }

  185.         private void generateRecord(Row tableRow, org.apache.poi.ss.usermodel.Row row, int sheetIndex, int rowIndex) {
  186.                 setCellsValue(tableRow, row, sheetIndex, rowIndex);
  187.                 setPrimarykey(tableRow, row, sheetIndex, rowIndex);
  188.         }

  189.         private void setPrimarykey(Row tableRow, org.apache.poi.ss.usermodel.Row row, int sheetIndex, int rowIndex) {
  190.                 ImportConceptConfig conceptConfig = getImportConfig().getConceptConfig();
  191.                 if (Utils.isNotNull(tableRow)) {
  192.                         Table t = tableRow.getTable();
  193.                         if (conceptConfig.isUseKey()) {
  194.                                 // TODO lzg 只是支持单主键,多主键通过keyrelation方式完成
  195.                                 if (1 == conceptConfig.getCellNumbers().size()) {
  196.                                         String idColumn = (String) t.getProperties().get(Table.PROP_NAME_ROWID);
  197.                                         Cell cell = row.getCell(conceptConfig.getCellNumbers().get(0).intValue());
  198.                                         if (t.getMetaData().containsColumn(idColumn)) {
  199.                                                 String dataType = t.getMetaData().getColumnMetaData(idColumn).getType();
  200.                                                 Object keyValue = getValue(cell, dataType, conceptConfig.getDatePatterns().get(0), sheetIndex, rowIndex);
  201.                                                 if (Utils.isNotNull(keyValue))
  202.                                                         tableRow.setValue(idColumn, keyValue);
  203.                                         }
  204.                                 }
  205.                         }
  206.                 }
  207.         }

复制代码
回复 支持 反对

使用道具 举报

213

主题

790

帖子

2056

积分

金牌会员

Rank: 6Rank: 6

积分
2056
QQ
 楼主| 发表于 2018-1-4 10:42:45 | 显示全部楼层
  1. private void setCellsValue(Row tableRow, org.apache.poi.ss.usermodel.Row row, int sheetIndex, int rowIndex) {
  2. if (Utils.isNotNull(tableRow)) {
  3. Table t = tableRow.getTable();
  4. ImportConfig mappingConfig = getImportConfig();
  5. ImportConceptConfig conceptConfig = mappingConfig.getConceptConfig();
  6. List<ImportRelationConfig> relationConfigs = conceptConfig.getRelationConfigs();
  7. for (ImportRelationConfig rc : relationConfigs) {
  8. String rName = rc.getMetaName();
  9. Cell cell = row.getCell(rc.getExcelCellIndex());
  10. if (t.getMetaData().containsColumn(rName)) {
  11. String dataType = t.getMetaData().getColumnMetaData(rName).getType();
  12. Object value = getValue(cell, dataType, rc.getDatePattern(), sheetIndex, rowIndex);
  13. if (Utils.isNotNull(value))
  14. tableRow.setValue(rName, value);
  15. }
  16. }
  17. }
  18. }

  19. private ImportConfig getImportConfig() {
  20. return importConfig;
  21. }

  22. private Object formatDate(String dateType, String value, String pattern) {
  23. Locale locale = Locale.ENGLISH;
  24. SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
  25. Date date = null;
  26. try {
  27. date = formatter.parse(value);
  28. } catch (ParseException e) {
  29. throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_PARSE_DATE_ERROR, value);
  30. //new RuntimeException("时间类型转换失败!value:" + value, e);
  31. }
  32. return date;
  33. }

  34. private Object getValue(Cell cell, String dataType, String datePattern, int sheetIndex, int rowIndex) {
  35. if (null == cell)
  36. return null;
  37. Object value = null;
  38. int cellType = cell.getCellType();
  39. if (Cell.CELL_TYPE_FORMULA == cellType)
  40. cellType = evaluator.evaluateFormulaCell(cell);
  41. switch (cellType) {
  42. case Cell.CELL_TYPE_BOOLEAN: {
  43. value = cell.getBooleanCellValue();
  44. break;
  45. }
  46. case Cell.CELL_TYPE_NUMERIC: {
  47. if ("date".equalsIgnoreCase(dataType)) {
  48. value = new java.sql.Date(cell.getDateCellValue().getTime());
  49. } else if ("time".equalsIgnoreCase(dataType)) {
  50. value = new java.sql.Time(cell.getDateCellValue().getTime());
  51. } else if ("dateTime".equalsIgnoreCase(dataType)) {
  52. value = new java.sql.Timestamp(cell.getDateCellValue().getTime());
  53. } else
  54. value = new DecimalFormat("0.##########").format(cell.getNumericCellValue());
  55. break;
  56. }
  57. case Cell.CELL_TYPE_ERROR: {
  58. value = null;
  59. break;
  60. }
  61. case Cell.CELL_TYPE_FORMULA: {
  62. break;
  63. }
  64. case Cell.CELL_TYPE_BLANK:
  65. case Cell.CELL_TYPE_STRING: {
  66. String s = cell.getStringCellValue();
  67. if ("date".equalsIgnoreCase(dataType) || "time".equalsIgnoreCase(dataType) || "dateTime".equalsIgnoreCase(dataType)) {
  68. if (null != datePattern && !"".equals(datePattern)){
  69. value = formatDate(dataType, s, datePattern);
  70. if(null!=value){
  71. long t = ((Date)value).getTime();
  72. if ("date".equalsIgnoreCase(dataType)) {
  73. value = new java.sql.Date(t);
  74. } else if ("time".equalsIgnoreCase(dataType)) {
  75. value = new java.sql.Time(t);
  76. } else if ("dateTime".equalsIgnoreCase(dataType)) {
  77. value = new java.sql.Timestamp(t);
  78. }
  79. }
  80. }else
  81. value = null;
  82. } else
  83. try {
  84. value = SimpleTransform.transToObj(dataType, s);
  85. } catch (Exception e) {
  86. throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_PARSE_CELL_ERROR, sheetIndex,rowIndex,cell.getColumnIndex());
  87. //new RuntimeException("excel数据与mapping声明的类型不一致,对应excel的sheet index: " + sheetIndex + " ;row index: " + rowIndex
  88. //        + " ;column index: " + cell.getColumnIndex(), e);
  89. }
  90. break;
  91. }
  92. }
  93. // 数据类型检查,处理
  94. if (Utils.isNotEmptyString(dataType) && Utils.isNotNull(value)) {
  95. try {
  96. if ("integer".equalsIgnoreCase(dataType)) {
  97. Double d = null;
  98. if (value instanceof String) {
  99. d = Double.parseDouble((String) value);
  100. value = d.intValue();
  101. } else if (value instanceof Double) {
  102. d = (Double) value;
  103. value = d.intValue();
  104. }
  105. if (!(value instanceof Integer))
  106. throw ExcelException.create(SAMessages.IMPORT_EXCEL_CHACK_ERROR,value,"Integer");
  107. } else if ("float".equalsIgnoreCase(dataType)) {
  108. if (value instanceof String)
  109. value = Double.parseDouble((String) value);
  110. else if (!(value instanceof Double))
  111. throw ExcelException.create(SAMessages.IMPORT_EXCEL_CHACK_ERROR,value,"Float");
  112. //new RuntimeException("获取Float类型数据失败!");
  113. } else if ("decimal".equalsIgnoreCase(dataType)) {
  114. if (value instanceof String)
  115. value = BigDecimal.valueOf(Double.parseDouble((String) value));
  116. else if (!(value instanceof BigDecimal))
  117. throw ExcelException.create(SAMessages.IMPORT_EXCEL_CHACK_ERROR,value,"Decimal");
  118. //new RuntimeException("获取Decimal类型数据失败!");
  119. } else if ("boolean".equalsIgnoreCase(dataType)) {
  120. if (value instanceof String)
  121. value = Boolean.parseBoolean((String) value);
  122. else if (!(value instanceof Boolean))
  123. throw ExcelException.create(SAMessages.IMPORT_EXCEL_CHACK_ERROR,value,"Boolean");
  124. //new RuntimeException("获取Boolean类型数据失败!");
  125. }
  126. } catch (Exception e) {
  127. throw ExcelException.create(e, SAMessages.IMPORT_EXCEL_PARSE_CELL_ERROR, sheetIndex,rowIndex,cell.getColumnIndex());
  128. }
  129. }
  130. return value;
  131. }
  132. }
复制代码
回复 支持 反对

使用道具 举报

213

主题

790

帖子

2056

积分

金牌会员

Rank: 6Rank: 6

积分
2056
QQ
 楼主| 发表于 2018-1-4 10:43:17 | 显示全部楼层
我加了输出语句
回复 支持 反对

使用道具 举报

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
36185
发表于 2018-1-4 13:32:48 | 显示全部楼层
在整个BIZ下搜索importExcelBeforeAction看看有几个定义
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

213

主题

790

帖子

2056

积分

金牌会员

Rank: 6Rank: 6

积分
2056
QQ
 楼主| 发表于 2018-1-4 15:47:01 | 显示全部楼层
没有搜到半个月了啊

Y3_ZW{O`({HBY[OEI@1[VTO.png
回复 支持 反对

使用道具 举报

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
36185
发表于 2018-1-4 15:59:34 | 显示全部楼层
搜索的信息发一下,默认肯定有一个
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

213

主题

790

帖子

2056

积分

金牌会员

Rank: 6Rank: 6

积分
2056
QQ
 楼主| 发表于 2018-1-5 10:20:06 | 显示全部楼层
没有搜到
OEEE0DVN%V(B_$$N}PU69WL.png
回复

使用道具 举报

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
36185
发表于 2018-1-5 10:41:13 | 显示全部楼层
参考
action.png
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复

使用道具 举报

213

主题

790

帖子

2056

积分

金牌会员

Rank: 6Rank: 6

积分
2056
QQ
 楼主| 发表于 2018-1-5 11:50:08 | 显示全部楼层
如图
~_1$VAK_8[6{IC[T0U`@AO0.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2025-7-4 12:23 , Processed in 0.116066 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表