I am trying to develop a piece of code to import/export data from Excel files into Java. I've completed importing and exporting into defined templates (example: Class Employee, where I know that the Excel sheet has the "id" in column1, "name" in column2, and so on). What I am having trouble is adding columns and rows in order to make the TableView have the same information as the imported Excel file, no matter the number of rows/columns.
When I import data from an Excel file I send it to an 'ArrayList parameter'. So if Excel file has:
Id | Name
1 | John
2 | Mary
When I list the ArrayList 'parameter' I get: "Id Name 01 John 02 Mary"
I know how many columns I have from (since All Cells = Row Number * Column Number):
columnSize = parameter.size() / importedExcel.RowNumber()
I've managed to add columns dynamically according to generic Excel files.
public class ExcelObject {
private int rowNum;
private ArrayList<String> parameters;
public void readExcel(String filePath, int sheetInt) {
try {
FileInputStream file = new FileInputStream(new File(filePath));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(sheetInt);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
this.incRowNum();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
this.parameters.add(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
this.parameters.add(cell.getDateCellValue().toString());
} else {
this.parameters.add(Double.toString(cell.getNumericCellValue()));
}
break;
case Cell.CELL_TYPE_BOOLEAN:
this.parameters.add("" +cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
this.parameters.add(cell.getCellFormula());
break;
default:
System.out.println("ERROR");
}
}
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public class vfUIController implements Initializable {
private ExcelReader read;
@FXML
private TableView table = new TableView();
@Override
public void initialize(URL url, ResourceBundle rb) {
List<String> columns = new ArrayList<String>();
ExcelObject obj = new ExcelObject();
obj.readExcel("file.xlsx", 2);
for (int i = 0; i< obj.getColumnNum; i++) {
columns.add(obj.getParametersValue(i));
System.out.println("ADDED COLUMN:" +i);
}
ArrayList<TableColumn> columns = new ArrayList<TableColumn>();
for (int i = 0 ; i < obj.getColNum() ; i++) {
columns.add(new TableColumn(obj.getParameters().get(i)));
}
table.getColumns().addAll(columns);
//missing code to add rows from 'ArrayList<String> parameters' at obj.getParameterValue(int arrayListPosition)
}
But I can't seem to add any Rows using table.setItems() or other methods. I get blank rows all the time.
I'm not sure if I'm failing by not using setCellValueFactory properly or because the whole method I use to import cells data into an ArrayList is not smart.
In summary I would like to import any Excel file information into a TableView in JAVA.
Thanks for the help. Simon
setCellValueFactoryproperly, but I don't see you calling that. Can you post that code?cellValueFactorythe table columns won't have any data to display. You need to set acellValueFactoryon each column.ReadOnlyStringWrapperand return it from the factory. Similar example: stackoverflow.com/questions/28255870/…