Last active
February 22, 2019 17:30
-
-
Save sedj601/da2717d4d075b4f85f3c8322df6659b8 to your computer and use it in GitHub Desktop.
JavaFX Excel into TableView: StackOverflow Question: https://stackoverflow.com/questions/54830492/open-an-excel-file-in-a-tableview-with-filechooser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Brittney 2 F | |
Andra 3 F | |
Elease 4 F | |
Alejandrina 7 M | |
Jefferey 11 M | |
Gregoria 13 F | |
Laquita 17 F | |
Shyla 19 F | |
Mona 23 F | |
Joannie 29 F | |
Laurette 31 F | |
Rosalina 37 F | |
Maisie 43 F | |
Francoise 47 M | |
Denisse 53 F | |
Chantel 59 F | |
Sue 61 F | |
Randee 67 M | |
Minnie 71 F | |
Candance 73 F | |
By Column below: | |
Brittney | |
Andra | |
Elease | |
Alejandrina | |
Jefferey | |
Gregoria | |
Laquita | |
Shyla | |
Mona | |
Joannie | |
Laurette | |
Rosalina | |
Maisie | |
Francoise | |
Denisse | |
Chantel | |
Sue | |
Randee | |
Minnie | |
Candance | |
2 | |
3 | |
4 | |
7 | |
11 | |
13 | |
17 | |
19 | |
23 | |
29 | |
31 | |
37 | |
43 | |
47 | |
53 | |
59 | |
61 | |
67 | |
71 | |
73 | |
F | |
F | |
F | |
M | |
M | |
F | |
F | |
F | |
F | |
F | |
F | |
F | |
F | |
M | |
F | |
F | |
F | |
M | |
F | |
F | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javafx.application.Application; | |
import javafx.beans.property.IntegerProperty; | |
import javafx.beans.property.SimpleIntegerProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.FileChooser; | |
import javafx.stage.FileChooser.ExtensionFilter; | |
import javafx.stage.Stage; | |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | |
import org.apache.poi.ss.usermodel.Cell; | |
import org.apache.poi.ss.usermodel.Row; | |
import org.apache.poi.ss.usermodel.Sheet; | |
import org.apache.poi.ss.usermodel.Workbook; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
/** | |
* | |
* @author blj0011 | |
*/ | |
public class StackOverflowQ1 extends Application | |
{ | |
File file; | |
ObservableList<Person> persons = FXCollections.observableArrayList(); | |
@Override | |
public void start(Stage primaryStage) | |
{ | |
TableView<Person> tableView = new TableView(); | |
tableView.setItems(persons); | |
TableColumn<Person, String> tableColumnName = new TableColumn("Name"); | |
TableColumn<Person, Integer> tableColumnAge = new TableColumn("Age"); | |
TableColumn<Person, String> tableColumnSex = new TableColumn("Sex"); | |
tableColumnName.setCellValueFactory(new PropertyValueFactory("name")); | |
tableColumnAge.setCellValueFactory(new PropertyValueFactory("age")); | |
tableColumnSex.setCellValueFactory(new PropertyValueFactory("sex")); | |
tableView.getColumns().setAll(tableColumnName, tableColumnAge, tableColumnSex); | |
Button btn = new Button(); | |
btn.setText("Open File"); | |
btn.setOnAction((ActionEvent event) -> { | |
FileChooser fileChooser = new FileChooser(); | |
fileChooser.setTitle("Open Resource File"); | |
fileChooser.getExtensionFilters().add(new ExtensionFilter("Excel Files", "*.xlsx")); | |
File selectedFile = fileChooser.showOpenDialog(primaryStage); | |
if (selectedFile != null) { | |
file = selectedFile; | |
List<Person> personsData = extractDataFromFile(file); | |
persons.addAll(personsData); | |
} | |
}); | |
VBox root = new VBox(tableView, btn); | |
Scene scene = new Scene(root, 500, 500); | |
primaryStage.setTitle("Hello World!"); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
private List<Person> extractDataFromFile(File file) | |
{ | |
List<Person> personsData = new ArrayList(); | |
try { | |
Workbook workbook = new XSSFWorkbook(file); | |
Sheet sheet = workbook.getSheetAt(0); | |
for (int i = 0; i < sheet.getLastRowNum(); i++) { | |
Row row = sheet.getRow(i); | |
Person person = new Person(); | |
for (int t = 0; t < row.getLastCellNum(); t++) { | |
Cell cell = row.getCell(t); | |
switch (t) { | |
case 0: | |
person.setName(cell.getStringCellValue()); | |
System.out.println("row: " + i + " cell: " + t + " - " + cell.getStringCellValue()); | |
break; | |
case 1: | |
Double age = cell.getNumericCellValue(); | |
person.setAge(age.intValue()); | |
System.out.println("row: " + i + " cell: " + t + " - " + cell.getNumericCellValue()); | |
break; | |
case 2: | |
person.setSex(cell.getStringCellValue()); | |
System.out.println("row: " + i + " cell: " + t + " - " + cell.getStringCellValue()); | |
break; | |
} | |
} | |
personsData.add(person); | |
} | |
} | |
catch (IOException | InvalidFormatException ex) { | |
ex.printStackTrace(); | |
} | |
return personsData; | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) | |
{ | |
launch(args); | |
} | |
public static class Person | |
{ | |
private final StringProperty name = new SimpleStringProperty(); | |
private final IntegerProperty age = new SimpleIntegerProperty(); | |
private final StringProperty sex = new SimpleStringProperty(); | |
public Person() | |
{ | |
} | |
public Person(String name, int age, String sex) | |
{ | |
this.name.setValue(name); | |
this.age.setValue(age); | |
this.sex.setValue(sex); | |
} | |
public final String getName() | |
{ | |
return name.get(); | |
} | |
public final void setName(String value) | |
{ | |
name.set(value); | |
} | |
public StringProperty nameProperty() | |
{ | |
return name; | |
} | |
public final int getAge() | |
{ | |
return age.get(); | |
} | |
public final void setAge(int value) | |
{ | |
age.set(value); | |
} | |
public IntegerProperty ageProperty() | |
{ | |
return age; | |
} | |
public final String getSex() | |
{ | |
return sex.get(); | |
} | |
public final void setSex(String value) | |
{ | |
sex.set(value); | |
} | |
public StringProperty sexProperty() | |
{ | |
return sex; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment