I have a small problem. I want to display an excel file in the tableview. I would like to select the file with the filechooser and display it in the tableview.
I tried it with the teaxtarea as an example and it worked wonderfully. I need it in the tableview. I would be grateful if you could help me.
I program it with JavaFX
I'll show you how I did it with the textarea.
I have already created the tableview separately. I just need an example of how I get it to insert in the tableview.
I open the excel file using apache poi.
public class ReadExcel extends Application {
Pane pane = new Pane();
Button button = new Button("Hier");
Desktop desktop = Desktop.getDesktop();
TextArea textarea = new TextArea();
public void start(Stage primaryStage) throws IOException {
Scene scene = new Scene(pane);
scene.getStylesheets().add("style.css");
primaryStage.setTitle("GBO-Tool");
primaryStage.setScene(scene);
primaryStage.show();
textarea.setLayoutX(100);
textarea.setLayoutY(100);
pane.getChildren().addAll(button, textarea);
FileChooser filechooser = new FileChooser();
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
File file = filechooser.showOpenDialog(primaryStage);
if (file != null) {
try {
readExcel();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
public void readExcel() throws IOException {
String excelfile = "Gesamtpunktzahl.xlsx";
FileInputStream is = new FileInputStream(newFile(excelfile));
Workbook wb = new XSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0);
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case STRING:
textarea.appendText(cell.getStringCellValue() + "\n");
break;
case BOOLEAN:
textarea.appendText(cell.getBooleanCellValue() + "\n");
break;
case NUMERIC:
textarea.appendText(cell.getNumericCellValue() + "\n");
break;
}
textarea.appendText("\n");
}
}
wb.close();
is.close();
}
private static String newFile(String excelfile) {
return excelfile;
// TODO Auto-generated method stub
}
public static void main(String[] args0) {
launch();
}
}