com.alibaba.excel.context.AnalysisContext Java Examples
The following examples show how to use
com.alibaba.excel.context.AnalysisContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseExcelListener.java From easyexcel-utils with Apache License 2.0 | 6 votes |
/** * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则继续读取下一行。 * 如果不重写该方法,默认抛出异常,停止读取 * * @param exception exception * @param context context */ @Override public void onException(Exception exception, AnalysisContext context) throws Exception { // 如果continueAfterThrowing为false,则直接将异常抛出 if (!continueAfterThrowing) { throw exception; } Integer sheetNo = getCurrentSheetNo(context); Integer rowIndex = context.readRowHolder().getRowIndex(); log.error("/*------- 读取发生错误! 错误SheetNo:{},错误行号:{} -------*/ ", sheetNo, rowIndex, exception); List<Integer> errRowNumList = errRowsMap.get(String.valueOf(sheetNo)); if (Objects.isNull(errRowNumList)) { errRowNumList = new ArrayList<>(); errRowNumList.add(rowIndex); errRowsMap.put(String.valueOf(sheetNo), errRowNumList); } else { errRowNumList.add(rowIndex); } }
Example #2
Source File: ConverterDataListener.java From easyexcel with Apache License 2.0 | 6 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); ConverterData data = list.get(0); try { Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate()); } catch (ParseException e) { throw new ExcelCommonException("Test Exception", e); } Assert.assertEquals(data.getBooleanData(), Boolean.TRUE); Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); Assert.assertEquals((long)data.getLongData(), 1L); Assert.assertEquals((long)data.getIntegerData(), 1L); Assert.assertEquals((long)data.getShortData(), 1L); Assert.assertEquals((long)data.getByteData(), 1L); Assert.assertEquals(data.getDoubleData(), 1.0, 0.0); Assert.assertEquals(data.getFloatData(), (float)1.0, 0.0); Assert.assertEquals(data.getString(), "测试"); Assert.assertEquals(data.getCellData().getStringValue(), "自定义"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #3
Source File: ExceptionDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void invoke(ExceptionData data, AnalysisContext context) { list.add(data); if (list.size() == 5) { int i = 5 / 0; } }
Example #4
Source File: DemoExceptionListener.java From easyexcel with Apache License 2.0 | 5 votes |
/** * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。 * * @param exception * @param context * @throws Exception */ @Override public void onException(Exception exception, AnalysisContext context) { LOGGER.error("解析失败,但是继续解析下一行:{}", exception.getMessage()); // 如果是某一个单元格的转换异常 能获取到具体行号 // 如果要获取头的信息 配合invokeHeadMap使用 if (exception instanceof ExcelDataConvertException) { ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; LOGGER.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData()); } }
Example #5
Source File: UserExcelListener.java From fw-spring-cloud with Apache License 2.0 | 5 votes |
@Override public void invoke(SysUser sysUser, AnalysisContext analysisContext) { log.info("解析到一条数据:{}", JSONUtil.toJsonStr(sysUser)); sysUser.setCreateTime(DateUtil.date()); sysUser.setUpdateTime(DateUtil.date()); sysUser.setCreateUser("sys"); sysUser.setUpdateUser("sys"); sysUser.setDeleteFlag(0); sysUser.setDisableFlag(0); list.add(sysUser); if (list.size() >= BATCH_COUNT) { saveData(); list.clear(); } }
Example #6
Source File: EncryptDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 10); Assert.assertEquals(list.get(0).getName(), "姓名0"); Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); Assert.assertEquals( context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #7
Source File: SimpleDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 10); Assert.assertEquals(list.get(0).getName(), "姓名0"); Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); Assert.assertEquals( context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #8
Source File: ExcelReadDataListener.java From jetlinks-community with Apache License 2.0 | 5 votes |
/** * 这个每一条数据解析都会来调用 */ @Override public void invoke(T data, AnalysisContext analysisContext) { RowResult<T> result=new RowResult<>(); result.setResult(data); result.setRowIndex(analysisContext.readRowHolder().getRowIndex()); sink.next(result); }
Example #9
Source File: AnnotationDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); AnnotationData data = list.get(0); try { Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01")); } catch (ParseException e) { throw new ExcelCommonException("Test Exception", e); } Assert.assertEquals(data.getNumber(), 99.99, 0.00); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #10
Source File: CompatibilityDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 10); List<String> data = list.get(0); Assert.assertEquals(data.get(0), "字符串00"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #11
Source File: UserImportListener.java From SpringBlade with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { // 调用importer方法 userService.importUser(list); // 存储完成清理list list.clear(); }
Example #12
Source File: LargeDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void invoke(LargeData data, AnalysisContext context) { if (count == 0) { LOGGER.info("First row:{}", JSON.toJSONString(data)); } count++; if (count % 100000 == 0) { LOGGER.info("Already read:{}", count); } }
Example #13
Source File: AbstractExcelImportListener.java From spring-microservice-exam with MIT License | 5 votes |
@Override public void invoke(T dataModel, AnalysisContext context) { dataList.add(dataModel); // 达到BATCH_COUNT则保存进数据库,防止数据几万条数据在内存,容易OOM if (dataList.size() >= BATCH_COUNT) { saveData(dataList); // 存储完成清理list dataList.clear(); } }
Example #14
Source File: UserExcelListener.java From fw-spring-cloud with Apache License 2.0 | 5 votes |
@Override public void invoke(SysUser sysUser, AnalysisContext analysisContext) { log.info("解析到一条数据:{}", JSONUtil.toJsonStr(sysUser)); sysUser.setCreateTime(DateUtil.date()); sysUser.setUpdateTime(DateUtil.date()); sysUser.setCreateUser("sys"); sysUser.setUpdateUser("sys"); sysUser.setDeleteFlag(0); sysUser.setDisableFlag(0); list.add(sysUser); if (list.size() >= BATCH_COUNT) { saveData(); list.clear(); } }
Example #15
Source File: ListHeadDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); Map<Integer, String> data = list.get(0); Assert.assertEquals("字符串0", data.get(0)); Assert.assertEquals("1", data.get(1)); Assert.assertEquals("2020-01-01 01:01:01", data.get(2)); Assert.assertEquals("额外数据", data.get(3)); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #16
Source File: ExcelDataListener.java From SpringMVC-Project with MIT License | 5 votes |
/** * 每解析一行调用一次 */ @Override public void invoke(ExcelData excelData, AnalysisContext analysisContext) { dataList.add(excelData); logger.info("读取到excel上传数据 | {} | {}", analysisContext.readRowHolder().getRowIndex(), JSON.toJSONString(excelData)); //这里可以处理批量入库 }
Example #17
Source File: ExcelFormatAdapter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void onException(Exception exception, AnalysisContext context) { if (exception instanceof ExcelDataConvertException) { ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception; logger.error("遍历Excel第{}行,第{}列异常,数据为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData()); } }
Example #18
Source File: UploadDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
/** * 这个每一条数据解析都会来调用 * * @param data * one row value. Is is same as {@link AnalysisContext#readRowHolder()} * @param context */ @Override public void invoke(UploadData data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); list.add(data); // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM if (list.size() >= BATCH_COUNT) { saveData(); // 存储完成清理 list list.clear(); } }
Example #19
Source File: ComplexDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); ComplexHeadData data = list.get(0); Assert.assertEquals(data.getString4(), "字符串4"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #20
Source File: UploadDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
/** * 所有数据解析完成了 都会来调用 * * @param context */ @Override public void doAfterAllAnalysed(AnalysisContext context) { // 这里也要保存数据,确保最后遗留的数据也存储到数据库 saveData(); LOGGER.info("所有数据解析完成!"); }
Example #21
Source File: ModelBuildEventListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void invoke(Map<Integer, CellData> cellDataMap, AnalysisContext context) { ReadHolder currentReadHolder = context.currentReadHolder(); if (HeadKindEnum.CLASS.equals(currentReadHolder.excelReadHeadProperty().getHeadKind())) { context.readRowHolder() .setCurrentRowAnalysisResult(buildUserModel(cellDataMap, currentReadHolder, context)); return; } context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(cellDataMap, currentReadHolder, context)); }
Example #22
Source File: ModelBuildEventListener.java From easyexcel with Apache License 2.0 | 5 votes |
private Object buildUserModel(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder, AnalysisContext context) { ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty(); Object resultModel; try { resultModel = excelReadHeadProperty.getHeadClazz().newInstance(); } catch (Exception e) { throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0, new CellData(CellDataTypeEnum.EMPTY), null, "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e); } Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap(); Map<String, Object> map = new HashMap<String, Object>(headMap.size() * 4 / 3 + 1); Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap(); for (Map.Entry<Integer, Head> entry : headMap.entrySet()) { Integer index = entry.getKey(); if (!cellDataMap.containsKey(index)) { continue; } CellData cellData = cellDataMap.get(index); if (cellData.getType() == CellDataTypeEnum.EMPTY) { continue; } ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(), excelContentProperty, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration(), context.readRowHolder().getRowIndex(), index); if (value != null) { map.put(excelContentProperty.getField().getName(), value); } } BeanMap.create(resultModel).putAll(map); return resultModel; }
Example #23
Source File: DefaultAnalysisEventProcessor.java From easyexcel with Apache License 2.0 | 5 votes |
private void onException(AnalysisContext analysisContext, Exception e) { for (ReadListener readListenerException : analysisContext.currentReadHolder().readListenerList()) { try { readListenerException.onException(e, analysisContext); } catch (RuntimeException re) { throw re; } catch (Exception e1) { throw new ExcelAnalysisException(e1.getMessage(), e1); } } }
Example #24
Source File: LockDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void invoke(LockData data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); list.add(data); if (list.size() >= BATCH_COUNT) { saveData(); list.clear(); } }
Example #25
Source File: DemoDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
/** * 这个每一条数据解析都会来调用 * * @param data * one row value. Is is same as {@link AnalysisContext#readRowHolder()} * @param context */ @Override public void invoke(DemoData data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); list.add(data); // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM if (list.size() >= BATCH_COUNT) { saveData(); // 存储完成清理 list list.clear(); } }
Example #26
Source File: SortDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); SortData sortData = list.get(0); Assert.assertEquals("column1", sortData.getColumn1()); Assert.assertEquals("column2", sortData.getColumn2()); Assert.assertEquals("column3", sortData.getColumn3()); Assert.assertEquals("column4", sortData.getColumn4()); Assert.assertEquals("column5", sortData.getColumn5()); Assert.assertEquals("column6", sortData.getColumn6()); }
Example #27
Source File: CellDataDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); CellDataData cellDataData = list.get(0); Assert.assertEquals(cellDataData.getDate().getStringValue(), "2020年01月01日"); Assert.assertEquals((long)cellDataData.getInteger1().getData(), 2L); Assert.assertEquals((long)cellDataData.getInteger2(), 2L); Assert.assertEquals(cellDataData.getFormulaValue().getFormulaValue(), "B2+C2"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); }
Example #28
Source File: CellDataDemoHeadDataListener.java From easyexcel with Apache License 2.0 | 5 votes |
@Override public void invoke(CellDataReadDemoData data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); if (list.size() >= BATCH_COUNT) { saveData(); list.clear(); } }
Example #29
Source File: DefaultAnalysisEventProcessor.java From easyexcel with Apache License 2.0 | 4 votes |
private void buildHead(AnalysisContext analysisContext, Map<Integer, CellData> cellDataMap) { if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { return; } Map<Integer, String> dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext); ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap(); Map<Integer, ExcelContentProperty> contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1); Map<Integer, ExcelContentProperty> tmpContentPropertyMap = new HashMap<Integer, ExcelContentProperty>(contentPropertyMapData.size() * 4 / 3 + 1); for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) { Head headData = entry.getValue(); if (headData.getForceIndex() || !headData.getForceName()) { tmpHeadMap.put(entry.getKey(), headData); tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); continue; } List<String> headNameList = headData.getHeadNameList(); String headName = headNameList.get(headNameList.size() - 1); for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) { if (stringEntry == null) { continue; } String headString = stringEntry.getValue(); Integer stringKey = stringEntry.getKey(); if (StringUtils.isEmpty(headString)) { continue; } if (analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { headString = headString.trim(); } if (headName.equals(headString)) { headData.setColumnIndex(stringKey); tmpHeadMap.put(stringKey, headData); tmpContentPropertyMap.put(stringKey, contentPropertyMapData.get(entry.getKey())); break; } } } excelHeadPropertyData.setHeadMap(tmpHeadMap); excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); }
Example #30
Source File: TemplateDataListener.java From easyexcel with Apache License 2.0 | 4 votes |
@Override public void invoke(TemplateData data, AnalysisContext context) { list.add(data); }