/**
   * @Title: processRecord @Description: process parse excel.
   *
   * @param inputStream
   * @throws IOException
   * @throws
   */
  public void processRecord(InputStream inputStream) throws IOException {
    MissingRecordAwareHSSFListener missingRecordAwarelistener =
        new MissingRecordAwareHSSFListener(this);
    formatTrackingHSSFListener = new FormatTrackingHSSFListener(missingRecordAwarelistener);
    sheetRecordCollectingListener =
        new EventWorkbookBuilder.SheetRecordCollectingListener(formatTrackingHSSFListener);

    HSSFRequest request = new HSSFRequest();

    request.addListenerForAllRecords(sheetRecordCollectingListener);

    HSSF_EVENT_FACTORY.processWorkbookEvents(request, new POIFSFileSystem(inputStream));
  }
  private void processFile(String filename) throws Exception {
    HSSFRequest req = new HSSFRequest();
    mockListen = new MockHSSFListener();
    listener = new FormatTrackingHSSFListener(mockListen);
    req.addListenerForAllRecords(listener);

    HSSFEventFactory factory = new HSSFEventFactory();
    try {
      InputStream is = HSSFTestDataSamples.openSampleFileStream(filename);
      POIFSFileSystem fs = new POIFSFileSystem(is);
      factory.processWorkbookEvents(req, fs);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  @Override
  public void setUp() {
    HSSFRequest req = new HSSFRequest();
    mockListen = new MockHSSFListener();
    listener = new SheetRecordCollectingListener(mockListen);
    req.addListenerForAllRecords(listener);

    HSSFEventFactory factory = new HSSFEventFactory();
    try {
      InputStream is = HSSFTestDataSamples.openSampleFileStream("3dFormulas.xls");
      POIFSFileSystem fs = new POIFSFileSystem(is);
      factory.processWorkbookEvents(req, fs);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Esempio n. 4
0
  @Override
  public void parse(Key key) throws IOException {
    _firstRow = true;
    InputStream is = DKV.get(key).openStream();
    try {
      _fs = new POIFSFileSystem(is);
      MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
      _formatListener = new FormatTrackingHSSFListener(listener);

      HSSFEventFactory factory = new HSSFEventFactory();
      HSSFRequest request = new HSSFRequest();
      request.addListenerForAllRecords(_formatListener);

      factory.processWorkbookEvents(request, _fs);
    } finally {
      try {
        is.close();
      } catch (IOException e) {
      }
    }
  }
 /**
  * Processes a file into essentially record events.
  *
  * @param req an Instance of HSSFRequest which has your registered listeners
  * @param fs a POIFS filesystem containing your workbook
  */
 public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) throws IOException {
   processWorkbookEvents(req, fs.getRoot());
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.toolsverse.etl.connector.DataSetConnector#populate(com.toolsverse
   * .etl.connector.DataSetConnectorParams, com.toolsverse.etl.common.DataSet,
   * com.toolsverse.etl.driver.Driver)
   */
  public ConnectorResult populate(ExcelConnectorParams params, DataSet dataSet, Driver driver)
      throws Exception {
    if (dataSet == null
        || params == null
        || Utils.isNothing(dataSet.getName())
        || (driver == null && dataSet.getDriver() == null)) {
      ConnectorResult result = new ConnectorResult();
      result.setRetCode(ConnectorResult.VALIDATION_FAILED_CODE);

      if (dataSet == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NULL.getValue());
      if (driver == null && dataSet.getDriver() == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DRIVER_NULL.getValue());
      if (params == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_PARAMS_NULL.getValue());
      if (dataSet != null && Utils.isNothing(dataSet.getName()))
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NO_NAME.getValue());

      return result;
    }

    dataSet.clear();

    driver = driver != null ? driver : dataSet.getDriver();

    if (!params.isSilent())
      Logger.log(
          Logger.INFO,
          EtlLogger.class,
          EtlResource.LOADING_DATASET_MSG.getValue() + dataSet.getName() + "...");

    FileInputStream fin = null;
    POIFSFileSystem poifs = null;

    try {
      String fileName = null;

      if (params.getInputStream() == null) {
        fileName =
            SystemConfig.instance()
                .getPathUsingAppFolders(
                    params.getFileName(
                        dataSet.getOwnerName() != null ? dataSet.getOwnerName() : dataSet.getName(),
                        ".xls",
                        true));

        fin = new FileInputStream(fileName);

        poifs = new POIFSFileSystem(fin);
      } else poifs = new POIFSFileSystem(params.getInputStream());

      XlsProcessor xlsProcessor = new XlsProcessor(params, dataSet, driver);

      MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(xlsProcessor);
      FormatTrackingHSSFListener formatListener = new FormatTrackingHSSFListener(listener);

      xlsProcessor.setFormatListener(formatListener);

      HSSFRequest request = new HSSFRequest();
      request.addListenerForAllRecords(formatListener);

      HSSFEventFactory factory = new HSSFEventFactory();

      try {
        factory.processWorkbookEvents(request, poifs);
      } catch (Exception ex) {
        if (!params.isMaxRowsExceededException(ex) && !params.isSheetAlreadyExatractedException(ex))
          throw ex;
      }

      if (dataSet.getFieldCount() > 0
          && dataSet.getRecordCount() == 0
          && params.getAddRecordCallback() != null) {
        params.getAddRecordCallback().onAddRecord(dataSet, driver, null, 0);
      }

      ConnectorResult connectorResult = new ConnectorResult();

      connectorResult.addResult(
          Utils.format(
              FileConnectorResource.FILE_POPULATED.getValue(),
              new String[] {FilenameUtils.getName(fileName)}));

      return connectorResult;

    } finally {
      if (fin != null) fin.close();

      if (params.getInputStream() != null && params.isCloseInput()) params.getInputStream().close();

      if (params.getAfterCallback() != null) params.getAfterCallback().onAfter(dataSet, driver);
    }
  }