All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.databene.document.xls.XLSLineIterator Maven / Gradle / Ivy

Go to download

'databene webdecs' is an open source software library for WEB Data Extraction, Conversion and Scripting, written by Volker Bergmann.

The newest version!
/*
 * (c) Copyright 2009-2012 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package org.databene.document.xls;

import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.databene.commons.Converter;
import org.databene.commons.IOUtil;
import org.databene.commons.converter.NoOpConverter;
import org.databene.webdecs.DataContainer;
import org.databene.webdecs.DataIterator;

/**
 * Iterates the lines of a sheet in an Excel document.
*
* Created at 27.01.2009 22:04:47 * @since 0.4.8 * @author Volker Bergmann */ public class XLSLineIterator implements DataIterator { private String emptyMarker; private String nullMarker; private Iterator rowIterator; private Converter stringPreprocessor; // constructors ---------------------------------------------------------------------------------------------------- public XLSLineIterator(String uri) throws IOException { this(uri, 0); } public XLSLineIterator(String uri, int sheetIndex) throws IOException { this(uri, sheetIndex, null); } public XLSLineIterator(String uri, int sheetIndex, Converter preprocessor) throws IOException { this(sheet(uri, sheetIndex), preprocessor); } public XLSLineIterator(String uri, String sheetName) throws IOException { this(sheet(uri, sheetName), null); } public XLSLineIterator(HSSFSheet sheet, Converter stringPreprocessor) { this.emptyMarker = "'"; if (stringPreprocessor == null) stringPreprocessor = new NoOpConverter(); this.stringPreprocessor = stringPreprocessor; rowIterator = sheet.rowIterator(); if (!rowIterator.hasNext()) { close(); return; } } // properties ------------------------------------------------------------------------------------------------------ public String getEmptyMarker() { return emptyMarker; } public void setEmptyMarker(String emptyMarker) { this.emptyMarker = emptyMarker; } public String getNullMarker() { return nullMarker; } public void setNullMarker(String nullMarker) { this.nullMarker = nullMarker; } // interface ------------------------------------------------------------------------------------------------------- public Class getType() { return Object[].class; } public synchronized DataContainer next(DataContainer wrapper) { if (rowIterator == null || !rowIterator.hasNext()) return null; Row row = rowIterator.next(); int cellCount = row.getLastCellNum(); Object[] result = new Object[cellCount]; for (int cellnum = 0; cellnum < cellCount; cellnum++) result[cellnum] = HSSFUtil.resolveCellValue(row.getCell(cellnum), emptyMarker, nullMarker, stringPreprocessor); return wrapper.setData(result); } public synchronized void close() { rowIterator = null; } // helper methods -------------------------------------------------------------------------------------------------- private static HSSFSheet sheet(String uri, String sheetName) throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(IOUtil.getInputStreamForURI(uri)); HSSFSheet sheet = sheetName != null ? workbook.getSheet(sheetName) : workbook.getSheetAt(0); if (sheet == null) throw new IllegalArgumentException("Sheet not found in file " + uri + ": " + sheetName); return sheet; } private static HSSFSheet sheet(String uri, int sheetIndex) throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(IOUtil.getInputStreamForURI(uri)); return workbook.getSheetAt(sheetIndex); } @Override public String toString() { return getClass().getSimpleName() + "[" + rowIterator + "]"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy