将excel文件导入表视图JAVAFX excel流式读取器
我想将xlsx文件导入到tableView中,为此我使用JAVAFX,为了读取文件,我使用这个 我无法导入文件,因为只有两个变量nb3Col和amountCol返回此错误 PropertyValueFactory中的参数与xml文件中的参数相同 对于其他变量nb1、nb2、account、idClient。。。它很有魅力 错误将excel文件导入表视图JAVAFX excel流式读取器,javafx,apache-poi,xlsx,streamreader,tablecolumn,Javafx,Apache Poi,Xlsx,Streamreader,Tablecolumn,我想将xlsx文件导入到tableView中,为此我使用JAVAFX,为了读取文件,我使用这个 我无法导入文件,因为只有两个变量nb3Col和amountCol返回此错误 PropertyValueFactory中的参数与xml文件中的参数相同 对于其他变量nb1、nb2、account、idClient。。。它很有魅力 错误 Error:(100, 34) java: incompatible types: javafx.scene.control.cell.PropertyValue
Error:(100, 34) java: incompatible types: javafx.scene.control.cell.PropertyValueFactory<sample.Data,java.lang.String> cannot be converted to javafx.util.Callback<javafx.scene.control.TableColumn<sample.Data,java.lang.String>,javafx.scene.control.TableCell<sample.Data,java.lang.String>>
数据类
package sample;
import javafx.beans.InvalidationListener;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class Data {
private StringProperty nb1;
private StringProperty account;
private StringProperty nb2;
private StringProperty nb3;
private StringProperty idClient;
private StringProperty amount;
private StringProperty name;
private StringProperty typeAccount;
public Data(String nb1, String account, String nb2, String nb3, String idClient, String amount, String name, String typeAccount) {
this.nb1 = new SimpleStringProperty(nb1);
this.account = new SimpleStringProperty(account);
this.nb2 = new SimpleStringProperty(nb2);
this.nb3 = new SimpleStringProperty(nb3);
this.idClient = new SimpleStringProperty(idClient);
this.amount = new SimpleStringProperty(amount);
this.name = new SimpleStringProperty(name);
this.typeAccount = new SimpleStringProperty(typeAccount);
}
public void setNb1(String value) { nb1Property().set(value); }
public String getNb1() { return nb1Property().get(); }
public StringProperty nb1Property() {
if (nb1 == null) nb1 = new SimpleStringProperty(this, "firstName");
return nb1;
}
public void setAccount(String value) { accountProperty().set(value); }
public String getAccount() { return accountProperty().get(); }
public StringProperty accountProperty() {
if (account == null) account= new SimpleStringProperty(this,"account","");
return account;
}
public void setNb2(String value) { this.nb2.set(value); }
public String getNb2() {return nb2.get(); }
public StringProperty nb2Property() {
if (nb2 == null) nb2= new SimpleStringProperty(this,"nb2","");
return nb2;
}
public void setNb3(String nb3) { this.nb3.set(nb3);}
public String getNb3() { return nb3.get();}
public StringProperty nb3Property() {
if(nb3 == null) nb3 = new SimpleStringProperty(this,"nb3","");
return nb3;
}
public void setIdClient(String idClient) { this.idClient.set(idClient); }
public String getIdClient() { return idClient.get(); }
public StringProperty idClientProperty() {
if(idClient==null) idClient= new SimpleStringProperty(this,"idClient","");
return idClient;
}
public void setAmount(String amount) { this.amount.set(amount); }
public String getAmount() { return amount.get(); }
public StringProperty amountProperty() {
if(amount==null) amount= new SimpleStringProperty(this,"amount","");
return amount;
}
public void setName(String name) { this.name.set(name); }
public String getName() { return name.get(); }
public StringProperty nameProperty() {
if(name==null) name = new SimpleStringProperty(this,"name","");
return name;
}
public void setTypeAccount(String typeAccount) { this.typeAccount.set(typeAccount); }
public String getTypeAccount() { return typeAccount.get();}
public StringProperty typeAccountProperty() {
if(typeAccount==null) typeAccount= new SimpleStringProperty(this,"typeAccount","");
return typeAccount;
}
@Override
public String toString() {
return "Data{" +
"nb1=" + nb1 +
", account=" + account +
", nb2=" + nb2 +
", nb3=" + nb3 +
", idClient=" + idClient +
", amount=" + amount +
", name=" + name +
", typeAccount=" + typeAccount +
'}';
}
}
控制器类
package sample;
import com.monitorjbl.xlsx.StreamingReader;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.FileChooser;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
ObservableList<Data> observableDataList = FXCollections.observableArrayList();
@FXML
private ResourceBundle resources;
@FXML
private URL location;
//-------------TABLE COLUMN------------
@FXML
private TableColumn<Data, String> nb1Col; // fx:id="firstNameCol"
@FXML
private TableColumn<Data, String> accountCol; // fx:id="lastNameCol"
@FXML
private TableColumn<Data, String> nb2Col; // fx:id="netIDCol"
@FXML
private TableColumn<Data, Number> nb3Col; // fx:id="ageCol"
@FXML
private TableColumn<Data, String> idClientCol; // fx:id="gpaCol"
@FXML
private TableColumn<Data,String> amountCol ;
@FXML
private TableColumn<Data, String> nameCol; // fx:id="uinCol"
@FXML
private TableColumn<Data, String> typeAccountCol; // fx:id="genderCol"
//-----------------TEXT FIELD---------
@FXML
private TextField nameField; // fx:id="nameField"
@FXML
private TextField clientIDField; // fx:id="clientIDField"
@FXML
private TextField impIDField; // fx:id="impIDField"
//----------Combo Box---------------
@FXML
private ComboBox<String> accountBox = new ComboBox<>(); // fx:id="accountBox"
ObservableList<String> accountBoxData = FXCollections.observableArrayList("ANSEG", "Credit auto confort", "credit immobilier", "CNAC", "ANGEM");
//-----------------Observable Data List-------------
@FXML
private TableView<Data> dataTableView;
//--------------- BUTTON--------------
@FXML
private Button clearBtn; // fx:id="clearBtn"
@FXML
private Button filterBtn; // fx:id="filterBtn"
//-----------------MENU BAR-------------
@FXML
private MenuBar fileMenu;
@Override
public void initialize(URL location, ResourceBundle resources) {
accountBox.setItems(accountBoxData);
nb1Col.setCellValueFactory(new PropertyValueFactory<Data, String>("nb1"));
accountCol.setCellValueFactory(new PropertyValueFactory<Data, String>("account"));
nb2Col.setCellValueFactory(new PropertyValueFactory<Data, String>("nb2"));
nb3Col.setCellValueFactory(new PropertyValueFactory<Data, String>("nb3"));
idClientCol.setCellValueFactory(new PropertyValueFactory<Data, String>("idClient"));
amountCol.setCellFactory(new PropertyValueFactory<Data,String>("amount"));
accountCol.setCellValueFactory(new PropertyValueFactory<Data, String>("account"));
nameCol.setCellValueFactory(new PropertyValueFactory<Data, String>("name"));
typeAccountCol.setCellValueFactory(new PropertyValueFactory<Data, String>("typeAccount"));
}
public void load(ActionEvent event) throws IOException {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open file");
File file = fileChooser.showOpenDialog(accountBox.getScene().getWindow());
System.out.println(file.getPath());
InputStream fileIn ;
Workbook workbook ;
fileIn = new FileInputStream(file.getPath());
workbook = StreamingReader.builder().rowCacheSize(100).bufferSize(4096).open(fileIn);
Sheet sheet= workbook.getSheetAt(0);
String a0=null,a= null,a1= null,a2= null,a3= null,a4= null,a5= null,a6= null,a7= null;
System.out.println(sheet.getSheetName());
int i=0;
for (Row row : sheet ) {
i++;
if(i != 1) {
try {
Data data = new Data(a0=row.getCell(0).getStringCellValue(),
a =row.getCell(1).getStringCellValue(),
a1 =row.getCell(2).getStringCellValue(),
a2 =row.getCell(3).getStringCellValue(),
a3 =row.getCell(4).getStringCellValue(),
a4 =row.getCell(5).getStringCellValue(),
a5 =row.getCell(6).getStringCellValue(),
a6 =row.getCell(7).getStringCellValue()
);
// Data data = new Data("katia", "asmaa", "152", "588", "5", "as", "88", "78");
fillTable(observableDataList,data);
System.out.print(a0+"\t"+a+"\t"+"\t"+a1+"\t"+a2+"\t"+a3+"\t"+a4+"\t"+a5+"\t"+a5+"\t"+a6);
}catch (NullPointerException nullp){
//System.out.print(a0+"\t"+a+"\t"+"\t"+a1+"\t"+a2+"\t"+a3+"\t"+a4+"\t"+a5+"\t"+a5+"\t"+a6);
System.out.println("null");
}
}
}
}
private void fillTable(ObservableList<Data> observableDataList,Data data) {
observableDataList.add(data);
dataTableView.setItems(observableDataList);
}
}
包装样品;
导入com.monitorjbl.xlsx.StreamingReader;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.*;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.stage.FileChooser;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.usermodel.Cell;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.net.URL;
导入java.util.ResourceBundle;
公共类控制器实现可初始化{
ObservableList observableDataList=FXCollections.observableArrayList();
@FXML
私人资源;
@FXML
私有URL位置;
//-------------表列------------
@FXML
私有表列nb1Col;//fx:id=“firstNameCol”
@FXML
私有表列accountCol;//fx:id=“lastNameCol”
@FXML
私有表列nb2Col;//fx:id=“netIDCol”
@FXML
私有表列nb3Col;//fx:id=“ageCol”
@FXML
私有表列idClientCol;//fx:id=“gpaCol”
@FXML
私有表列数量;
@FXML
private TableColumn nameCol;//fx:id=“uinCol”
@FXML
私有表列typeAccountCol;//fx:id=“genderCol”
//-----------------文本字段---------
@FXML
私有TextField nameField;//fx:id=“nameField”
@FXML
private TextField clientIDField;//fx:id=“clientIDField”
@FXML
私有TextField impIDField;//fx:id=“impIDField”
//----------组合框---------------
@FXML
私有ComboBox accountBox=新ComboBox();//fx:id=“accountBox”
ObservableList accountBoxData=FXCollections.observableArrayList(“ANSEG”、“信用自动转换”、“信用冻结”、“CNAC”、“ANGEM”);
//-----------------可观测数据表-------------
@FXML
私有TableView-dataTableView;
//---------------钮扣--------------
@FXML
私有按钮clearBtn;//fx:id=“clearBtn”
@FXML
私有按钮filterBtn;//fx:id=“filterBtn”
//-----------------菜单栏-------------
@FXML
私有菜单栏文件菜单;
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
accountBox.setItems(accountBoxData);
nb1Col.setCellValueFactory(新属性价值工厂(“nb1”));
accountCol.setCellValueFactory(新属性ValueFactory(“账户”);
nb2Col.setCellValueFactory(新属性价值工厂(“nb2”));
nb3Col.setCellValueFactory(新的PropertyValueFactory(“nb3”));
idClientCol.setCellValueFactory(新属性值工厂(“idClient”);
金额setCellFactory(新财产价值工厂(“金额”);
accountCol.setCellValueFactory(新属性ValueFactory(“账户”);
nameCol.setCellValueFactory(新属性ValueFactory(“名称”);
typeAccountCol.setCellValueFactory(新属性ValueFactory(“typeAccount”));
}
公共无效加载(ActionEvent事件)引发IOException{
FileChooser FileChooser=newfilechooser();
setTitle(“打开文件”);
File File=fileChooser.showOpenDialog(accountBox.getScene().getWindow());
System.out.println(file.getPath());
输入流文件;
工作手册;
fileIn=newfileinputstream(file.getPath());
工作簿=StreamingReader.builder().rowCacheSize(100).bufferSize(4096).open(fileIn);
工作表=工作簿。getSheetAt(0);
字符串a0=null,a=null,a1=null,a2=null,a3=null,a4=null,a5=null,a6=null,a7=null;
System.out.println(sheet.getSheetName());
int i=0;
用于(行:页){
i++;
如果(i!=1){
试一试{
数据数据=新数据(a0=行。getCell(0)。getStringCellValue(),
a=行.getCell(1).getStringCellValue(),
a1=行.getCell(2).getStringCellValue(),
a2=行.getCell(3).getStringCellValue(),
a3=行.getCell(4).getStringCellValue(),
a4=行.getCell(5).getStringCellValue(),
a5=行.getCell(6).getStringCellValue(),
a6=行.getCell(7).getStringCellValue()
);
//数据=新数据(“katia”、“asmaa”、“152”、“588”、“5”、“as”、“88”、“78”);
填充表(Observedatalist,数据);
系统输出打印(a0+“\t”+a+“\t”+“\t”+a1+“\t”+a2+“\t”+a3+“\t”+a4+“\t”+a5+“\t”+a5+“\t”+a6);
}捕获(NullPointerException nullp){
//系统输出打印(a0+“\t”+a+“\t”+“\t”+a1+“\t”+a2+“\t”+a3+“\t”+a4+“\t”+a5+“\t”+a5+“\t”+a6);
System.out.println(“空”);
}
}
}
}
私有无效填充表(ObservableList observableDataList,数据){
添加(数据);
dataTableView.setItems(observableDataList);
}
}
XML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<SplitPane dividerPositions="0.7506265664160401" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="800.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<items>
<AnchorPane>
<children>
<HBox minWidth="-Infinity" prefWidth="1000.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar fx:id="fileMenu" prefWidth="1000.0" HBox.hgrow="ALWAYS">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#load" text="importer un fichier excel" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Exporter " />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Fermer" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</HBox>
<TableView fx:id="dataTableView" layoutX="1.0" layoutY="30.0" prefHeight="566.0" prefWidth="1000.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="30.0">
<columns>
<TableColumn fx:id="nb1Col" prefWidth="124.0" text="NB " />
<TableColumn fx:id="accountCol" prefWidth="131.0" text="Compte" />
<TableColumn fx:id="nb2Col" prefWidth="121.0" text="NB" />
<TableColumn fx:id="nb3Col" prefWidth="149.0" text="NB"/>
<TableColumn fx:id="idClientCol" prefWidth="170.0" text="IDENT" />
<TableColumn fx:id="amountCol" prefWidth="76.0" text="Montant" />
<TableColumn fx:id="nameCol" prefWidth="70.0" text="Nom et prénom" />
<TableColumn fx:id="genderCol" prefWidth="155.0" text="Gender" />
<TableColumn fx:id="typeAccountCol" prefWidth="75.0" text="Type Compte" />
</columns>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="1000.0" prefHeight="100.0" prefWidth="1000.0">
<children>
<HBox />
<TextField fx:id="nameField" layoutX="14.0" layoutY="14.0" promptText="Nom et Prénom..." />
<TextField fx:id="clientIDField" layoutX="201.0" layoutY="14.0" promptText="ID Client..." />
<TextField fx:id="impIDField" layoutX="201.0" layoutY="54.0" promptText="ID IMP..." />
<ComboBox fx:id="accountBox" layoutX="389.0" layoutY="14.0" prefWidth="150.0" promptText="Compte" />
<Button fx:id="clearBtn" layoutX="624.0" layoutY="14.0" mnemonicParsing="false" text="Supprimer " />
<Button fx:id="filterBtn" layoutX="624.0" layoutY="54.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="75.0" text="Filtrer" />
</children></AnchorPane>
</items>
</SplitPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<SplitPane dividerPositions="0.7506265664160401" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="800.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<items>
<AnchorPane>
<children>
<HBox minWidth="-Infinity" prefWidth="1000.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar fx:id="fileMenu" prefWidth="1000.0" HBox.hgrow="ALWAYS">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#load" text="importer un fichier excel" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Exporter " />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Fermer" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</HBox>
<TableView fx:id="dataTableView" layoutX="1.0" layoutY="30.0" prefHeight="566.0" prefWidth="1000.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="30.0">
<columns>
<TableColumn fx:id="nb1Col" prefWidth="124.0" text="NB " />
<TableColumn fx:id="accountCol" prefWidth="131.0" text="Compte" />
<TableColumn fx:id="nb2Col" prefWidth="121.0" text="NB" />
<TableColumn fx:id="nb3Col" prefWidth="149.0" text="NB"/>
<TableColumn fx:id="idClientCol" prefWidth="170.0" text="IDENT" />
<TableColumn fx:id="amountCol" prefWidth="76.0" text="Montant" />
<TableColumn fx:id="nameCol" prefWidth="70.0" text="Nom et prénom" />
<TableColumn fx:id="genderCol" prefWidth="155.0" text="Gender" />
<TableColumn fx:id="typeAccountCol" prefWidth="75.0" text="Type Compte" />
</columns>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="1000.0" prefHeight="100.0" prefWidth="1000.0">
<children>
<HBox />
<TextField fx:id="nameField" layoutX="14.0" layoutY="14.0" promptText="Nom et Prénom..." />
<TextField fx:id="clientIDField" layoutX="201.0" layoutY="14.0" promptText="ID Client..." />
<TextField fx:id="impIDField" layoutX="201.0" layoutY="54.0" promptText="ID IMP..." />
<ComboBox fx:id="accountBox" layoutX="389.0" layoutY="14.0" prefWidth="150.0" promptText="Compte" />
<Button fx:id="clearBtn" layoutX="624.0" layoutY="14.0" mnemonicParsing="false" text="Supprimer " />
<Button fx:id="filterBtn" layoutX="624.0" layoutY="54.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="75.0" text="Filtrer" />
</children></AnchorPane>
</items>
</SplitPane>