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

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

Introduction

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

Prototype

public HSSFRequest() 

Source Link

Document

Creates a new instance of HSSFRequest

Usage

From source file:XLS2CSVmra.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *///from  w  w w . j a va  2 s .c  o  m
public void process() throws IOException {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();
    request.addListenerForAllRecords(listener);

    factory.processWorkbookEvents(request, fs);
}

From source file:XLS2CSV.java

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *///from   w w  w.java 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:ambit.test.io.POItest.java

License:Open Source License

/**
 * Read an excel file and spit out what we find.
 * //from ww  w .j a v a  2 s.co  m
 * @param file
 *            Expect one argument that is the file to read.
 * @throws IOException
 *             When there is an error processing the file.
 */
public void readXLSFile(String file) throws IOException {
    // create a new file input stream with the input file specified
    // at the command line
    FileInputStream fin = new FileInputStream(file);
    // 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(new POIExample());
    // create our event factory
    HSSFEventFactory factory = new HSSFEventFactory();
    // process our events based on the document input stream
    factory.processEvents(req, din);
    // 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();
    System.out.println("done.");
}

From source file:ambit2.core.test.io.POItest.java

License:Open Source License

/**
 * Read an excel file and spit out what we find.
 * /*from   w  w w .java2s  .  c  o  m*/
 * @param file
 *            Expect one argument that is the file to read.
 * @throws IOException
 *             When there is an error processing the file.
 */
public void readXLSFile(String file) throws Exception {

    // create a new file input stream with the input file specified
    // at the command line
    FileInputStream fin = new FileInputStream(getClass().getClassLoader().getResource(file).getFile());
    // 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(new POIListener());
    // create our event factory
    HSSFEventFactory factory = new HSSFEventFactory();
    // process our events based on the document input stream
    factory.processEvents(req, din);
    // 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();
}

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

License:Apache License

/**
 * Initiates the processing of the XLS file to CSV
 *///w  w  w .  ja v  a2  s  .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(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 {/* www  .j a va2  s . c o m*/
        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.daphne.es.showcase.excel.service.ExcelDataService.java

License:Apache License

/**
 *  excel 2003 biff?/* www.j  av a2s  .  c om*/
 * xml? ?SAX
 * @param user
 * @param is
 */
@Async
public void importExcel2003(final User user, final InputStream is) {

    ExcelDataService proxy = ((ExcelDataService) AopContext.currentProxy());

    BufferedInputStream bis = null;
    InputStream dis = null;
    try {
        long beginTime = System.currentTimeMillis();

        List<ExcelData> dataList = Lists.newArrayList();

        //?
        bis = new BufferedInputStream(is);
        //  org.apache.poi.poifs.filesystem.Filesystem
        POIFSFileSystem poifs = new POIFSFileSystem(bis);
        // ?  Workbook(excel )?
        dis = poifs.createDocumentInputStream("Workbook");
        //  HSSFRequest
        HSSFRequest req = new HSSFRequest();

        // ?
        req.addListenerForAllRecords(new Excel2003ImportListener(proxy, dataList, batchSize));
        //  
        HSSFEventFactory factory = new HSSFEventFactory();
        // ???
        factory.processEvents(req, dis);

        //??batchSize?
        if (dataList.size() > 0) {
            proxy.doBatchSave(dataList);
        }

        long endTime = System.currentTimeMillis();

        Map<String, Object> context = Maps.newHashMap();
        context.put("seconds", (endTime - beginTime) / 1000);
        notificationApi.notify(user.getId(), "excelImportSuccess", context);
    } catch (Exception e) {
        log.error("excel import error", e);
        Map<String, Object> context = Maps.newHashMap();
        context.put("error", e.getMessage());
        notificationApi.notify(user.getId(), "excelImportError", context);
    } finally {
        // ?
        IOUtils.closeQuietly(bis);
        // ?
        IOUtils.closeQuietly(dis);
    }
}

From source file:com.jkoolcloud.tnt4j.streams.inputs.ExcelSXSSFRowStream.java

License:Apache License

/**
 * Reads HSSF (XLS) format excel file using Apache POI streaming SXSSF API.
 *
 * @param xlsFile//from w w  w.ja  va2  s  .co m
 *            excel HSSF format file to read
 *
 * @throws IOException
 *             if excel file or workbook can't be read
 */
protected void readXLS(File xlsFile) throws IOException {
    NPOIFSFileSystem fs = null;
    InputStream dis = null;
    boolean passwordSet = false;

    try {
        fs = new NPOIFSFileSystem(xlsFile, true);
        DirectoryNode root = fs.getRoot();
        if (root.hasEntry("EncryptedPackage")) { // NON-NLS
            dis = DocumentFactoryHelper.getDecryptedStream(fs, wbPass);
        } else {
            if (wbPass != null) {
                Biff8EncryptionKey.setCurrentUserPassword(wbPass);
                passwordSet = true;
            }
            dis = fs.createDocumentInputStream("Workbook"); // NON-NLS
        }
        HSSFRequest req = new HSSFRequest();

        XLSEventListener listener = new XLSEventListener(this);
        FormatTrackingHSSFListener formatsListener = new FormatTrackingHSSFListener(listener,
                Locale.getDefault());
        listener.setFormatListener(formatsListener);
        req.addListenerForAllRecords(formatsListener);
        HSSFEventFactory factory = new HSSFEventFactory();
        factory.processEvents(req, dis);
    } finally {
        if (passwordSet) {
            Biff8EncryptionKey.setCurrentUserPassword((String) null);
        }

        Utils.close(fs);
        Utils.close(dis);
    }
}

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();//from w  w  w  . jav  a 2s.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.XlsColumnsProcessor.java

License:Open Source License

@Override
protected HSSFRequest createRequest() {
    HSSFRequest request = new HSSFRequest();
    request.addListenerForAllRecords(formatTrackingListener);
    return request;
}