我正在编写一个函数来从 JFXTableView 创建 xls 表。
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("user details");
XSSFRow header = sheet.createRow(0);
String arrayOfHeaders [] = {"Sr. No.","Name of the Member", "Customized workout card status","Contact No.","Current programme taken","Current package taken",
"Purpose of taking customized workout card", "body type identified"," Current Body weight","Current height","payment amount",
"Mode of payment"};
for(int i=0; i<arrayOfHeaders.length; i++){
header.createCell(i).setCellValue(arrayOfHeaders[i]);
}
int index=1;
for(Member item: tableView.getItems()){
XSSFRow row = sheet.createRow(index);
int cellIndex=0;
for (Method m : item.getClass().getMethods()) {
// The getter should start with "get"
// I ignore getClass() method because it never returns null
if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {
row.createCell(cellIndex).setCellValue((String) m.invoke(item));
}
cellIndex++;
}
index++;
}
使用上面的代码,我可以通过 getter 从表中检索值,但问题是item.getClass().getMethods()以随机顺序返回 getter,这是不可接受的,因为我希望根据定义的标题。我有很多这样的表,每个都有自己的 Class 和 getter,为每个表编写不同的函数似乎太冗长了。所以,我打算做的是编写一个函数,我可以在其中传递数组中每个不同表对象的 getter,这样它就可以循环遍历特定 tableView 对象的所有 getter。像这样的东西:
createSheets(arrayOfHeaders, tableView.getItems(), arrayOfGetters);
我当前的 tableView 使用的一个此类成员类的示例是:
public static class Member{
private final SimpleStringProperty name;
private final SimpleStringProperty status;
private final SimpleStringProperty contact;
private final SimpleStringProperty programme;
private final SimpleStringProperty packages;
private final SimpleStringProperty purpose;
private final SimpleStringProperty bodyType;
private final SimpleStringProperty weight;
private final SimpleStringProperty height;
private final SimpleIntegerProperty paymentAmount;
private final SimpleStringProperty paymentMode;
public Member (String name, String status, String contact, String programme, String packages, String purpose,
String bodyType, String weight, String height, int paymentAmount, String paymentMode){
this.name = new SimpleStringProperty(name);
this.status = new SimpleStringProperty(status);
this.contact = new SimpleStringProperty(contact);
this.programme = new SimpleStringProperty(programme);
this.packages = new SimpleStringProperty(packages);
this.purpose = new SimpleStringProperty(purpose);
this.bodyType = new SimpleStringProperty(bodyType);
this.weight = new SimpleStringProperty(weight);
this.height = new SimpleStringProperty(height);
this.paymentAmount = new SimpleIntegerProperty(paymentAmount);
this.paymentMode = new SimpleStringProperty(paymentMode);
}
public String getName() {
return name.get();
}
public String getStatus() {
return status.get();
}
public String getContact() {
return contact.get();
}
public String getProgramme() {
return programme.get();
}
public String getPackages() {
return packages.get();
}
public String getPurpose() {
return purpose.get();
}
public String getBodyType() {
return bodyType.get();
}
public String getWeight() {
return weight.get();
}
public String getHeight() {
return height.get();
}
public int getPaymentAmount() {
return paymentAmount.get();
}
public String getPaymentMode() {
return paymentMode.get();
}
}