1

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

5
  • You say you don't know if you are using setCellValueFactory properly, but I don't see you calling that. Can you post that code? Commented Sep 16, 2015 at 14:59
  • I'm not using it at all in the actual code. I couldn't find a way of inserting the rows with or without setCellValueFactory Commented Sep 16, 2015 at 15:27
  • Without a cellValueFactory the table columns won't have any data to display. You need to set a cellValueFactory on each column. Commented Sep 16, 2015 at 15:28
  • But how can I set cellValueFactory if the data is all gathered in an 'ArrayList<String> parameter'. I can't "attach" a column to the var 'parameter' or i'll have all the information from the Excel file into a single column. Commented Sep 16, 2015 at 15:36
  • Just get the respective element from the array list, wrap it in a ReadOnlyStringWrapper and return it from the factory. Similar example: stackoverflow.com/questions/28255870/… Commented Sep 16, 2015 at 15:37

1 Answer 1

0

In you code you have only added empty columns to your Table View. in order to fill your table with a data. you have to set table data like this :

 private TableView<your Object type> table = new TableView<>();
List<your Object type> list=new ArrayList<>();

fill you list and then:

 ObservableList<your Object type> fxlist=FXCollections.observableList(list);


table.getItems().addAll(fxlist);

Data object Type must be the class that represents each row of data. Your list must be filled by you exsl data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.