| @@ -114,12 +114,17 @@ public class CellValueServer { | |||||
| Map<String, ExcelImportEntity> excelParams, String titleString) | Map<String, ExcelImportEntity> excelParams, String titleString) | ||||
| throws Exception { | throws Exception { | ||||
| ExcelImportEntity entity = excelParams.get(titleString); | ExcelImportEntity entity = excelParams.get(titleString); | ||||
| Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 ? entity | |||||
| .getMethods().get(entity.getMethods().size() - 1) : entity.getMethod(); | |||||
| Type[] ts = setMethod.getGenericParameterTypes(); | |||||
| String xclass = ts[0].toString(); | |||||
| String xclass = "class java.lang.Object"; | |||||
| 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(); | |||||
| xclass = ts[0].toString(); | |||||
| } | |||||
| Object result = getCellValue(xclass, cell, entity); | Object result = getCellValue(xclass, cell, entity); | ||||
| result = replaceValue(entity.getReplace(), result); | |||||
| if (entity != null) { | |||||
| result = replaceValue(entity.getReplace(), result); | |||||
| } | |||||
| result = hanlderValue(dataHanlder, object, result, titleString); | result = hanlderValue(dataHanlder, object, result, titleString); | ||||
| return getValueByType(xclass, result); | return getValueByType(xclass, result); | ||||
| } | } | ||||
| @@ -182,7 +187,7 @@ public class CellValueServer { | |||||
| } | } | ||||
| return result; | return result; | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(),e); | |||||
| LOGGER.error(e.getMessage(), e); | |||||
| throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | ||||
| } | } | ||||
| } | } | ||||
| @@ -174,13 +174,15 @@ public class ExcelImportServer extends ImportBaseService { | |||||
| List collection = new ArrayList(); | List collection = new ArrayList(); | ||||
| Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); | Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); | ||||
| List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | ||||
| Field fileds[] = PoiPublicUtil.getClassFields(pojoClass); | |||||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | |||||
| String targetId = null; | String targetId = null; | ||||
| if (etarget != null) { | |||||
| targetId = etarget.value(); | |||||
| 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); | |||||
| } | } | ||||
| getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); | |||||
| Iterator<Row> rows = sheet.rowIterator(); | Iterator<Row> rows = sheet.rowIterator(); | ||||
| for (int j = 0; j < params.getTitleRows(); j++) { | for (int j = 0; j < params.getTitleRows(); j++) { | ||||
| rows.next(); | rows.next(); | ||||
| @@ -217,8 +219,9 @@ public class ExcelImportServer extends ImportBaseService { | |||||
| for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) { | for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) { | ||||
| Cell cell = row.getCell(i); | Cell cell = row.getCell(i); | ||||
| String titleString = (String) titlemap.get(i); | String titleString = (String) titlemap.get(i); | ||||
| if (excelParams.containsKey(titleString)) { | |||||
| if (excelParams.get(titleString).getType() == 2) { | |||||
| if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { | |||||
| if (excelParams.get(titleString) != null | |||||
| && excelParams.get(titleString).getType() == 2) { | |||||
| picId = row.getRowNum() + "_" + i; | picId = row.getRowNum() + "_" + i; | ||||
| saveImage(object, picId, excelParams, titleString, pictures, params); | saveImage(object, picId, excelParams, titleString, pictures, params); | ||||
| } else { | } else { | ||||
| @@ -308,19 +311,22 @@ public class ExcelImportServer extends ImportBaseService { | |||||
| private void saveFieldValue(ImportParams params, Object object, Cell cell, | private void saveFieldValue(ImportParams params, Object object, Cell cell, | ||||
| Map<String, ExcelImportEntity> excelParams, String titleString, | Map<String, ExcelImportEntity> excelParams, String titleString, | ||||
| Row row) throws Exception { | Row row) throws Exception { | ||||
| ExcelVerifyHanlderResult verifyResult; | |||||
| Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams, | Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams, | ||||
| titleString); | titleString); | ||||
| verifyResult = verifyHandlerServer.verifyData(object, value, titleString, | |||||
| excelParams.get(titleString).getVerify(), params.getVerifyHanlder()); | |||||
| if (verifyResult.isSuccess()) { | |||||
| setValues(excelParams.get(titleString), object, value); | |||||
| if (object instanceof Map) { | |||||
| ((Map) object).put(titleString, value); | |||||
| } else { | } else { | ||||
| Cell errorCell = row.createCell(row.getLastCellNum()); | |||||
| errorCell.setCellValue(verifyResult.getMsg()); | |||||
| errorCell.setCellStyle(errorCellStyle); | |||||
| verfiyFail = true; | |||||
| throw new ExcelImportException(ExcelImportEnum.VERIFY_ERROR); | |||||
| 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); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -74,6 +74,9 @@ public final class PoiPublicUtil { | |||||
| Object obj = null; | Object obj = null; | ||||
| Method setMethod; | Method setMethod; | ||||
| try { | try { | ||||
| if(clazz.equals(Map.class)){ | |||||
| return new HashMap<String, Object>(); | |||||
| } | |||||
| obj = clazz.newInstance(); | obj = clazz.newInstance(); | ||||
| Field[] fields = getClassFields(clazz); | Field[] fields = getClassFields(clazz); | ||||
| for (Field field : fields) { | for (Field field : fields) { | ||||
| @@ -5,18 +5,22 @@ import java.io.FileInputStream; | |||||
| import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||
| import java.util.Date; | import java.util.Date; | ||||
| import java.util.List; | import java.util.List; | ||||
| import java.util.Map; | |||||
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||||
| import org.jeecgframework.poi.excel.ExcelImportUtil; | import org.jeecgframework.poi.excel.ExcelImportUtil; | ||||
| import org.jeecgframework.poi.excel.entity.ImportParams; | import org.jeecgframework.poi.excel.entity.ImportParams; | ||||
| import org.jeecgframework.poi.test.entity.CourseEntity; | import org.jeecgframework.poi.test.entity.CourseEntity; | ||||
| import org.jeecgframework.poi.test.entity.MsgClient; | |||||
| import org.jeecgframework.poi.test.entity.TestEntity; | import org.jeecgframework.poi.test.entity.TestEntity; | ||||
| import org.jeecgframework.poi.test.entity.statistics.StatisticEntity; | import org.jeecgframework.poi.test.entity.statistics.StatisticEntity; | ||||
| import org.junit.Test; | import org.junit.Test; | ||||
| public class ExcelImportUtilTest { | public class ExcelImportUtilTest { | ||||
| ///ExcelExportMsgClient 测试是这个到处的数据 | |||||
| //@Test | //@Test | ||||
| public void test() { | public void test() { | ||||
| try { | try { | ||||
| @@ -24,7 +28,7 @@ public class ExcelImportUtilTest { | |||||
| params.setTitleRows(1); | params.setTitleRows(1); | ||||
| long start = new Date().getTime(); | long start = new Date().getTime(); | ||||
| List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new FileInputStream( | List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new FileInputStream( | ||||
| new File("d:/tt.xlsx")), StatisticEntity.class, params); | |||||
| new File("d:/tt.xlsx")), StatisticEntity.class, params); | |||||
| // List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new File("d:/tt.xlsx"), | // List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new File("d:/tt.xlsx"), | ||||
| // StatisticEntity.class, params); | // StatisticEntity.class, params); | ||||
| /*for (int i = 0; i < list.size(); i++) { | /*for (int i = 0; i < list.size(); i++) { | ||||
| @@ -42,21 +46,24 @@ public class ExcelImportUtilTest { | |||||
| ImportParams params = new ImportParams(); | ImportParams params = new ImportParams(); | ||||
| params.setTitleRows(1); | params.setTitleRows(1); | ||||
| params.setHeadRows(1); | params.setHeadRows(1); | ||||
| params.setSheetNum(8); | |||||
| //params.setSheetNum(9); | |||||
| long start = new Date().getTime(); | long start = new Date().getTime(); | ||||
| List<TestEntity> list = ExcelImportUtil.importExcel(new File("d:/tt.xlsx"), | |||||
| TestEntity.class, params); | |||||
| String str = ""; | |||||
| for (int i = 0; i < list.size(); i++) { | |||||
| if (StringUtils.isNotEmpty(list.get(i).getLanya())) { | |||||
| str += "','" + Double.valueOf(list.get(i).getLanya()).intValue(); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(list.get(i).getPos())) { | |||||
| str += "','" + Double.valueOf(list.get(i).getPos()).intValue(); | |||||
| } | |||||
| } | |||||
| System.out.println(str); | |||||
| System.out.println(str.split(",").length); | |||||
| List<MsgClient> list = ExcelImportUtil.importExcel(new File("d:/tt.xlsx"), MsgClient.class, | |||||
| params); | |||||
| System.out.println(new Date().getTime() - start); | |||||
| System.out.println(list.size()); | |||||
| System.out.println(ReflectionToStringBuilder.toString(list.get(0))); | |||||
| } | |||||
| @Test | |||||
| public void testMapImport() { | |||||
| ImportParams params = new ImportParams(); | |||||
| params.setTitleRows(1); | |||||
| params.setHeadRows(1); | |||||
| long start = new Date().getTime(); | |||||
| List<Map<String, Object>> list = ExcelImportUtil.importExcel(new File("d:/tt.xlsx"), | |||||
| Map.class, params); | |||||
| System.out.println(new Date().getTime() - start); | |||||
| System.out.println(list.size()); | |||||
| System.out.println(list.get(0)); | |||||
| } | } | ||||
| } | } | ||||