
dk.eobjects.metamodel.ExcelDataContextStrategy Maven / Gradle / Ivy
The newest version!
/**
* This file is part of MetaModel.
*
* MetaModel is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MetaModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MetaModel. If not, see .
*/
package dk.eobjects.metamodel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import dk.eobjects.metamodel.data.DataSet;
import dk.eobjects.metamodel.query.SelectItem;
import dk.eobjects.metamodel.schema.Column;
import dk.eobjects.metamodel.schema.ColumnType;
import dk.eobjects.metamodel.schema.Schema;
import dk.eobjects.metamodel.schema.Table;
import dk.eobjects.metamodel.util.FormatHelper;
/**
* DataContextStrategy to use for Excel files
*/
public class ExcelDataContextStrategy extends QueryPostprocessDataContextStrategy {
private static final NumberFormat _numberFormat = FormatHelper.getUiNumberFormat();
private File _file;
public ExcelDataContextStrategy(File file) {
if (file == null) {
throw new IllegalArgumentException("File cannot be null");
}
if (!file.exists() || !file.canRead()) {
throw new IllegalArgumentException("Cannot read from file");
}
_file = file;
}
@Override
public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
if (columns == null || columns.length == 0)
columns = table.getColumns();
InputStream in = null;
try {
in = new FileInputStream(_file);
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(in));
HSSFSheet sheet = wb.getSheet(table.getName());
// the first row is the Column names
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy