Skip to content
allen edited this page Dec 30, 2015 · 13 revisions

Welcome to the poi-excel-utils wiki!

##Read example

  • Target excel

  • Code for processing

      ExcelReadSheetProcessor<TestBean> sheetProcessor = new ExcelReadSheetProcessor<TestBean>() {
          public void beforeProcess(ExcelReadContext<TestBean> context) {
          }
          public void process(ExcelReadContext<TestBean> context, List<TestBean> list) {
            //TODO process your result list
          }
          public void onExcepton(ExcelReadContext<TestBean> context, RuntimeException e) {
              throw e;
          }
          public void afterProcess(ExcelReadContext<TestBean> context) {
          }
      };
    
      ExcelReadFieldMapping fieldMapping = new ExcelReadFieldMapping();
      fieldMapping.put("A", "byteField");
     	//ignore some code here....
      fieldMapping.put("J", null, new ExcelReadCellProcessor() {
          public Object process(ExcelReadContext<?> context, Cell cell, ExcelCellValue cellValue) {
              return cellValue.getStringValue();//you can modify the value here.
          }
      });
      ExcelReadCellValueMapping valueMapping = new ExcelReadCellValueMapping();
      valueMapping.put("Please select", null);
      valueMapping.put("Option1", TestEnum.AA.toString());
      valueMapping.put("Option2", TestEnum.BB.toString());
      valueMapping.put("Option3", TestEnum.CC.toString());
      fieldMapping.put("K", "enumField2", valueMapping, false);
    
      sheetProcessor.setSheetIndex(0);// required.it can be replaced with 'setSheetName(sheetName)';
      sheetProcessor.setRowStartIndex(1);// not necessary ,but always set
      // sheetProcessor.setRowEndIndex(3);// not necessary
      sheetProcessor.setTargetClass(TestBean.class);// required
      sheetProcessor.setFieldMapping(fieldMapping);// required
      sheetProcessor.setPageSize(2);// not necessary
      // not necessary
      sheetProcessor.setRowProcessor(new ExcelReadRowProcessor<TestBean>() {
          public TestBean process(ExcelReadContext<TestBean> context, Row row, TestBean t) {
              return t;
          }
      });
      ExcelUtils.readInputStreamToObject(in, sheetProcessor);
    

##Write example

  • Template excel

  • Code for processing

  • Writting result

Clone this wiki locally