2010-02-05 20:19:38 +01:00
|
|
|
package com.metaweb.gridworks.importers;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.apache.commons.lang.NotImplementedException;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
2010-02-09 00:44:33 +01:00
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
2010-02-05 20:19:38 +01:00
|
|
|
|
|
|
|
import com.metaweb.gridworks.model.Cell;
|
|
|
|
import com.metaweb.gridworks.model.Column;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
|
|
|
import com.metaweb.gridworks.model.Row;
|
|
|
|
|
|
|
|
public class ExcelImporter implements Importer {
|
2010-02-09 00:44:33 +01:00
|
|
|
final protected boolean _xmlBased;
|
|
|
|
|
|
|
|
public ExcelImporter(boolean xmlBased) {
|
|
|
|
_xmlBased = xmlBased;
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
|
|
|
|
public boolean takesReader() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-24 00:22:02 +01:00
|
|
|
public void read(Reader reader, Project project, Properties options, int limit)
|
2010-02-05 20:19:38 +01:00
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void read(InputStream inputStream, Project project,
|
2010-02-24 00:22:02 +01:00
|
|
|
Properties options, int limit) throws Exception {
|
2010-02-05 20:19:38 +01:00
|
|
|
|
2010-02-09 00:44:33 +01:00
|
|
|
Workbook wb = _xmlBased ?
|
|
|
|
new XSSFWorkbook(inputStream) :
|
|
|
|
new HSSFWorkbook(new POIFSFileSystem(inputStream));
|
|
|
|
|
|
|
|
Sheet sheet = wb.getSheetAt(0);
|
2010-02-05 20:19:38 +01:00
|
|
|
|
|
|
|
int firstRow = sheet.getFirstRowNum();
|
|
|
|
int lastRow = sheet.getLastRowNum();
|
|
|
|
int r = firstRow;
|
|
|
|
|
|
|
|
List<Integer> nonBlankIndices = null;
|
|
|
|
List<String> nonBlankHeaderStrings = null;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the header row
|
|
|
|
*/
|
|
|
|
for (; r <= lastRow; r++) {
|
2010-02-09 00:44:33 +01:00
|
|
|
org.apache.poi.ss.usermodel.Row row = sheet.getRow(r);
|
2010-02-05 20:19:38 +01:00
|
|
|
if (row == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
short firstCell = row.getFirstCellNum();
|
|
|
|
short lastCell = row.getLastCellNum();
|
|
|
|
if (firstCell >= 0 && firstCell <= lastCell) {
|
|
|
|
nonBlankIndices = new ArrayList<Integer>(lastCell - firstCell + 1);
|
|
|
|
nonBlankHeaderStrings = new ArrayList<String>(lastCell - firstCell + 1);
|
|
|
|
|
2010-02-08 20:54:09 +01:00
|
|
|
for (int c = firstCell; c <= lastCell; c++) {
|
2010-02-09 00:44:33 +01:00
|
|
|
org.apache.poi.ss.usermodel.Cell cell = row.getCell(c);
|
2010-02-05 20:19:38 +01:00
|
|
|
if (cell != null) {
|
|
|
|
String text = cell.getStringCellValue().trim();
|
|
|
|
if (text.length() > 0) {
|
|
|
|
nonBlankIndices.add((int) c);
|
|
|
|
nonBlankHeaderStrings.add(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonBlankIndices.size() > 0) {
|
|
|
|
r++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonBlankIndices == null || nonBlankIndices.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create columns
|
|
|
|
*/
|
|
|
|
for (int c = 0; c < nonBlankIndices.size(); c++) {
|
|
|
|
Column column = new Column(c, nonBlankHeaderStrings.get(c));
|
|
|
|
project.columnModel.columns.add(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now process the data rows
|
|
|
|
*/
|
|
|
|
for (; r <= lastRow; r++) {
|
2010-02-09 00:44:33 +01:00
|
|
|
org.apache.poi.ss.usermodel.Row row = sheet.getRow(r);
|
2010-02-05 20:19:38 +01:00
|
|
|
if (row == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
short firstCell = row.getFirstCellNum();
|
|
|
|
short lastCell = row.getLastCellNum();
|
|
|
|
if (firstCell >= 0 && firstCell <= lastCell) {
|
|
|
|
Row newRow = new Row(nonBlankIndices.size());
|
|
|
|
boolean hasData = false;
|
|
|
|
|
2010-02-08 20:54:09 +01:00
|
|
|
for (int c = 0; c < nonBlankIndices.size(); c++) {
|
2010-02-05 20:19:38 +01:00
|
|
|
if (c < firstCell || c > lastCell) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-09 00:44:33 +01:00
|
|
|
org.apache.poi.ss.usermodel.Cell cell = row.getCell(c);
|
2010-02-05 20:19:38 +01:00
|
|
|
if (cell == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cellType = cell.getCellType();
|
2010-02-09 00:44:33 +01:00
|
|
|
if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR ||
|
|
|
|
cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK) {
|
2010-02-05 20:19:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-09 02:13:11 +01:00
|
|
|
if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA) {
|
|
|
|
cellType = cell.getCachedFormulaResultType();
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
|
|
|
|
Object value = null;
|
2010-02-09 00:44:33 +01:00
|
|
|
if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN) {
|
2010-02-05 20:19:38 +01:00
|
|
|
value = cell.getBooleanCellValue();
|
2010-02-09 00:44:33 +01:00
|
|
|
} else if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC) {
|
2010-02-05 20:19:38 +01:00
|
|
|
value = cell.getNumericCellValue();
|
|
|
|
} else {
|
|
|
|
String text = cell.getStringCellValue().trim();
|
|
|
|
if (text.length() > 0) {
|
|
|
|
value = text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != null) {
|
|
|
|
newRow.setCell(c, new Cell(value, null));
|
|
|
|
hasData = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasData) {
|
|
|
|
project.rows.add(newRow);
|
2010-02-24 00:22:02 +01:00
|
|
|
if (limit > 0 && project.rows.size() >= limit) {
|
|
|
|
break;
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|