{
final SheetSelectorDialog selectorDialog = new SheetSelectorDialog(workbook, parent);
sheetIndex = selectorDialog.getSelectedIndex();
}
final TypedTableModel tableModel = new TypedTableModel();
final Sheet sheet = workbook.getSheetAt(sheetIndex);
final Iterator rowIterator = sheet.rowIterator();
if (firstRowIsHeader)
{
if (rowIterator.hasNext())
{
final Row headerRow = (Row) rowIterator.next();
final short cellCount = headerRow.getLastCellNum();
for (short colIdx = 0; colIdx < cellCount; colIdx++)
{
final Cell cell = headerRow.getCell(colIdx);
if (cell != null)
{
while(colIdx > tableModel.getColumnCount())
{
tableModel.addColumn(Messages.getString("TableDataSourceEditor.Column",
String.valueOf(tableModel.getColumnCount())), Object.class);
}
final RichTextString string = cell.getRichStringCellValue();
if (string != null)
{
tableModel.addColumn(string.getString(), Object.class);
}
else
{
tableModel.addColumn(Messages.getString("TableDataSourceEditor.Column", String.valueOf(colIdx)), Object.class);
}
}
}
}
}
Object[] rowData = null;
while (rowIterator.hasNext())
{
final Row row = (Row) rowIterator.next();
final short cellCount = row.getLastCellNum();
if (cellCount == -1)
{
continue;
}
if (rowData == null || rowData.length != cellCount)
{
rowData = new Object[cellCount];
}
for (short colIdx = 0; colIdx < cellCount; colIdx++)
{
final Cell cell = row.getCell(colIdx);
final Object value;
if (cell != null)
{
if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
final RichTextString string = cell.getRichStringCellValue();
if (string != null)
{
value = string.getString();
}
else
{
value = null;
}
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
final CellStyle hssfCellStyle = cell.getCellStyle();
if (isDateFormat(hssfCellStyle.getDataFormat(), hssfCellStyle.getDataFormatString()))
{
value = cell.getDateCellValue();
}
else
{
value = cell.getNumericCellValue();
}
}
else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
{
value = cell.getBooleanCellValue();
}
else
{
value = cell.getStringCellValue();
}
}
else
{
value = null;
}
if (value != null && "".equals(value) == false)
{
while (colIdx >= tableModel.getColumnCount())
{
tableModel.addColumn(Messages.getString("TableDataSourceEditor.Column",
String.valueOf(tableModel.getColumnCount())), Object.class);
}
}
rowData[colIdx] = value;
}
if (Thread.currentThread().isInterrupted())
{
return;
}
tableModel.addRow(rowData);
}
final int colCount = tableModel.getColumnCount();
final int rowCount = tableModel.getRowCount();
for (int col = 0; col < colCount; col++)
{
Class type = null;
for (int row = 0; row < rowCount; row += 1)
{
final Object value = tableModel.getValueAt(row, col);
if (value == null)
{
continue;
}
if (type == null)
{
type = value.getClass();
}
else if (type != Object.class)
{
if (type.isInstance(value) == false)
{
type = Object.class;
}
}
}
if (Thread.currentThread().isInterrupted())
{
return;
}
if (type != null)
{
tableModel.setColumnType(col, type);
}
}
parent.importComplete(tableModel);
}