Example usage for org.apache.poi.hssf.eventusermodel FormatTrackingHSSFListener FormatTrackingHSSFListener

List of usage examples for org.apache.poi.hssf.eventusermodel FormatTrackingHSSFListener FormatTrackingHSSFListener

Introduction

In this page you can find the example usage for org.apache.poi.hssf.eventusermodel FormatTrackingHSSFListener FormatTrackingHSSFListener.

Prototype

public FormatTrackingHSSFListener(HSSFListener childListener) 

Source Link

Document

Creates a format tracking wrapper around the given listener, using the Locale#getDefault() default locale for the formats.

Usage

From source file:XLS2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *//*from   w  ww .jav  a 2  s  .c  o  m*/
public void process() throws IOException {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    formatListener = new FormatTrackingHSSFListener(listener);

    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();

    if (outputFormulaValues) {
        request.addListenerForAllRecords(formatListener);
    } else {
        workbookBuildingListener = new SheetRecordCollectingListener(formatListener);
        request.addListenerForAllRecords(workbookBuildingListener);
    }

    factory.processWorkbookEvents(request, fs);
    close();
}

From source file:cn.sinobest.jzpt.minidemo.poidemo.example.XLS2CSVmra.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *///from   ww  w  . jav  a2 s  . c  om
public void process() throws IOException {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    formatListener = new FormatTrackingHSSFListener(listener);

    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();

    if (outputFormulaValues) {
        request.addListenerForAllRecords(formatListener);
    } else {
        workbookBuildingListener = new SheetRecordCollectingListener(formatListener);
        request.addListenerForAllRecords(workbookBuildingListener);
    }

    factory.processWorkbookEvents(request, fs);
}

From source file:com.bayareasoftware.chartengine.ds.util.XLS2Data.java

License:Apache License

public void process() throws IOException {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    formatListener = new FormatTrackingHSSFListener(listener);

    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();

    if (outputFormulaValues) {
        request.addListenerForAllRecords(formatListener);
    } else {//from  ww w . ja  va 2 s.com
        workbookBuildingListener = new SheetRecordCollectingListener(formatListener);
        request.addListenerForAllRecords(workbookBuildingListener);
    }
    try {
        // we can handle this when we only want to read a particular sheet
        // and use this exception when we've moved beyond that sheet
        factory.abortableProcessWorkbookEvents(request, fs);
    } catch (HSSFUserException hssue) {
        hssue.printStackTrace();
    } catch (MaxRowsReachedException re) {
        System.out.println("aborted? " + re);
    }
    // make sure we got the last sheet
    if (currentSheetName != null && sheet2data.get(currentSheetName) == null) {
        sheet2data.put(currentSheetName, currentData);
    }
    //factory.processWorkbookEvents(request, fs);        
}

From source file:com.googlecode.sqlsheet.stream.XlsSheetIterator.java

License:Apache License

/**
 * Initiates the processing//from w  w  w.  j  av a  2  s. c om
 * - position stream to the right sheet
 * - extracts columns
 * - extracts first row
 */
public void postConstruct() throws SQLException {
    try {
        boundSheetRecords = new ArrayList();
        sheetIndex = -1;
        inRequiredSheet = false;
        outputFormulaValues = true;

        fileSystem = new NPOIFSFileSystem(getFileName().openStream());
        recordStream = new RecordFactoryInputStream(fileSystem.getRoot().createDocumentInputStream("Workbook"),
                false);
        MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
        formatListener = new FormatTrackingHSSFListener(listener);
        requestPublic = new PublicMorozoffHSSFRequest();
        if (outputFormulaValues) {
            requestPublic.addListenerForAllRecords(formatListener);
        } else {
            workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
            requestPublic.addListenerForAllRecords(workbookBuildingListener);
        }

        // Process each record as they come in till we get to the right sheet
        while (!inRequiredSheet) {
            Record r = recordStream.nextRecord();
            if (r == null) {
                break;
            }
            try {
                short userCode = requestPublic.processRecord(r);
                if (userCode != 0) {
                    break;
                }

            } catch (HSSFUserException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        //Flush rows counter
        setCurrentSheetRowIndex(0L);
        //Fill current row
        processNextRecords();
    } catch (IOException e) {
        throw new SQLException(e.getMessage(), e);
    }

}

From source file:com.netxforge.netxstudio.screens.f4.support.XLSService.java

License:Open Source License

public int go(IProgressMonitor monitor, FileInputStream fin) throws IOException {
    reset();//www.  jav  a 2 s. c o m
    this.currentMonitor = monitor;
    monitor.subTask("POI Ongoing");

    // TODO, we would need to do a first evaluation of the workbook to get
    // an idea of the amount of work we can expect here.
    // For now increment worked, for each single row.

    formatTrackingListener = new FormatTrackingHSSFListener(this);

    // create a new file input stream with the input file specified
    // at the command line
    // create a new org.apache.poi.poifs.filesystem.Filesystem
    POIFSFileSystem poifs = new POIFSFileSystem(fin);

    // get the Workbook (excel part) stream in a InputStream
    // InputStream din = poifs.createDocumentInputStream("Workbook");

    // construct out HSSFRequest object
    HSSFRequest req = new HSSFRequest();
    // lazy listen for ALL records with the listener shown above
    req.addListenerForAllRecords(formatTrackingListener);
    // create our event factory
    HSSFEventFactory factory = new HSSFEventFactory();
    // process our events based on the document input stream
    try {
        factory.abortableProcessWorkbookEvents(req, poifs);
    } catch (Exception e) {
        // We have an issue or cancel.
        System.out.println(e.getMessage());
    } finally {
        // once all the events are processed close our file input stream
        fin.close();
        // and our document input stream (don't want to leak these!)
        // din.close();
    }
    // TODO, silent return, could also be an exception with information on
    // what went wrong.
    return currentReturnCode;
}

From source file:com.sonicle.webtop.core.io.input.XlsBaseProcessor.java

License:Open Source License

public XlsBaseProcessor(InputStream is) {
    this.is = is;
    hssfListener = new MissingRecordAwareHSSFListener(this);
    formatTrackingListener = new FormatTrackingHSSFListener(hssfListener);
}

From source file:com.toolsverse.etl.connector.excel.ExcelConnector.java

License:Open Source License

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;

    }//  w ww.j  a v a2 s. c om

    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);
    }
}

From source file:com.vaadin.addon.tableexport.XLS2CSVmra.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *//*from  w  w  w .java2 s.  c o  m*/
public void process() throws IOException {
    final MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    formatListener = new FormatTrackingHSSFListener(listener);

    final HSSFEventFactory factory = new HSSFEventFactory();
    final HSSFRequest request = new HSSFRequest();

    if (outputFormulaValues) {
        request.addListenerForAllRecords(formatListener);
    } else {
        workbookBuildingListener = new SheetRecordCollectingListener(formatListener);
        request.addListenerForAllRecords(workbookBuildingListener);
    }

    factory.processWorkbookEvents(request, fs);
}

From source file:my_poi.MyXLS2CSVmra.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *///from  w  ww .java 2 s .c  o m
public void process() throws IOException {
    formatListener = new FormatTrackingHSSFListener(null);

    //MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);

    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();

    if (outputFormulaValues) {
        request.addListenerForAllRecords(this);
    } else {
        workbookBuildingListener = new SheetRecordCollectingListener(this);
        request.addListenerForAllRecords(workbookBuildingListener);
    }

    factory.processWorkbookEvents(request, fs);
}

From source file:org.smarty.core.XLS2CSVmra.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *//*from   w  w  w  .j av  a2s .com*/
public void process() throws IOException {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    formatListener = new FormatTrackingHSSFListener(listener);

    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();

    if (outputFormulaValues) {
        request.addListenerForAllRecords(this);
    } else {
        workbookBuildingListener = new SheetRecordCollectingListener(this);
        request.addListenerForAllRecords(workbookBuildingListener);
    }

    factory.processWorkbookEvents(request, fs);
}