Read Excel and show it on tableview. Populate TableView using 2D array or List of List

前端 未结 1 611
遥遥无期
遥遥无期 2021-01-17 01:48

My aim is to read the excel (which I am able to, via Apache POI) and display the excel on TableView (JavaFX). I am able to display column names on TableView, bu

相关标签:
1条回答
  • 2021-01-17 02:11

    I have created a demo that explains how to populate a TableView when the number of columns is unknown. Comments in the code.

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import javafx.application.Application;
    import javafx.beans.property.ReadOnlyObjectWrapper;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import org.apache.poi.EncryptedDocumentException;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    
    /**
     *
     * @author blj0011
     */
    public class JavaFXApplication124 extends Application
    {
        @Override
        public void start(Stage primaryStage)
        {        
            ObservableList<ObservableList<String>> data = FXCollections.observableArrayList();        
            final List<List<String>> excelData = readExcelFile("fileName.xlsx");//Get data from excel file
            //Add excel data to an observable list
            for(int i = 0; i < excelData.size(); i++)
            {
                data.add(FXCollections.observableArrayList(excelData.get(i)));
            }
    
            TableView<ObservableList<String>> tableView = new TableView();
            tableView.setItems(data);
    
            //Create the table columns, set the cell value factory and add the column to the tableview.
            for (int i = 0; i < excelData.get(0).size(); i++) {
                final int curCol = i;
                final TableColumn<ObservableList<String>, String> column = new TableColumn<>(
                        "Col " + (curCol + 1)
                );
                column.setCellValueFactory(
                        param -> new ReadOnlyObjectWrapper<>(param.getValue().get(curCol))
                );
                tableView.getColumns().add(column);
            }
    
            VBox vbox = new VBox(tableView);
    
            Scene scene = new Scene(vbox);
    
            primaryStage.setScene(scene);
    
            primaryStage.show();
    
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
        public static List<List<String>> readExcelFile(String fileName)
        {
            List<List<String>> excelContent = new ArrayList();
            int columnCounter = 0;
            int rowCounter = 0;
    
            try (InputStream inputStream = new FileInputStream(fileName)) {
                DataFormatter formatter = new DataFormatter();
    
                Workbook workbook = WorkbookFactory.create(inputStream);
                Sheet sheet = workbook.getSheetAt(0);
    
                for (Row row : sheet) {
                    List<String> tempList = new ArrayList();
    
                    for (Cell cell : row) {
                        String text = formatter.formatCellValue(cell);
                        System.out.print(++columnCounter + ": " + text + " - ");
                        System.out.println(text.length());
                        tempList.add(text.length() == 0 ? "" : text);
                    }
                    columnCounter = 0;
                    excelContent.add(tempList);
    
                    ++rowCounter;
                    if (rowCounter == 5) {
                        break;
                    }
                }
            }
            catch (IOException | EncryptedDocumentException ex) {
                System.out.println(ex.toString());
            }
    
            return excelContent;
        }
    }
    

    I am going to go out on a limb and credit this answer to @jewelsea. https://coderanch.com/t/663384/java/Populating-TableView-method

    0 讨论(0)
提交回复
热议问题