我需要帮助从表视图迭代数据以将其导出到excel,每次按下按钮它只显示我的Excel工作表上的表视图中的第一行数据。
我希望你能得到我说的这是我的代码
package application;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.RichTextString;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class SafeguardTrackingToolController implements Initializable {
@FXML
private TextField txtActivity;
@FXML
private TextArea txtComments;
@FXML
private ComboBox<String> cmbComponent;
@FXML
private TableColumn
tblComponent,
tblActivity,
tableColumnForTor,
tblContract,
tblfirst,
tblFinal ,
tblBank,
tblDisclosure,
tblNema,
tblBudgetRe,
tblBudgetPro,
tblBegin,
tblComments;
@FXML
private TableView<Items> tableViewForExcel;
@FXML
private DatePicker
dateTor,
dateContract,
dateFirstDraft,
dateFinalDraft,
dateBank,
dateDisclosure,
dateNema,
dateBudget,
dateProvided,
dateImplementation;
@FXML
private Button btnAdd,btnReset
,btnExcel
,btnDelete;
private final ObservableList<Items> data=FXCollections.observableArrayList(new Items(null, null, null, null, null, null, null, null, null, null, null, null, null));
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
tblComponent.setCellValueFactory(new PropertyValueFactory("Component"));
tblActivity.setCellValueFactory(new PropertyValueFactory("Activity"));
tableColumnForTor.setCellValueFactory(new PropertyValueFactory("Torr"));
tblContract.setCellValueFactory(new PropertyValueFactory("Contract"));
tblfirst.setCellValueFactory(new PropertyValueFactory("FirstDraft"));
tblFinal.setCellValueFactory(new PropertyValueFactory("FinalDraft"));
tblBank.setCellValueFactory(new PropertyValueFactory("Bank"));
tblDisclosure.setCellValueFactory(new PropertyValueFactory("Disclosure"));
tblNema.setCellValueFactory(new PropertyValueFactory("Nema"));
tblBudgetRe.setCellValueFactory(new PropertyValueFactory("Budget"));
tblBudgetPro.setCellValueFactory(new PropertyValueFactory("Provided"));
tblBegin.setCellValueFactory(new PropertyValueFactory("Implementation"));
tblComments.setCellValueFactory(new PropertyValueFactory("Comment"));
cmbComponent.getItems().addAll("Hi","You","Well","Me"
);
tableViewForExcel.getItems().setAll(this.data);
}
public static class Items{
//the setters and getters should be of the same name with the simple string property
private final SimpleStringProperty Component;
private final SimpleStringProperty Activity;
private final SimpleStringProperty Torr;
private final SimpleStringProperty Contract;
private final SimpleStringProperty FirstDraft;
private final SimpleStringProperty FinalDraft;
private final SimpleStringProperty Bank;
private final SimpleStringProperty Disclosure;
private final SimpleStringProperty Nema;
private final SimpleStringProperty Budget;
private final SimpleStringProperty Provided;
private final SimpleStringProperty Implementation;
private final SimpleStringProperty Comment;
private Items(String Comp, String Act,
String Tor, String Co,
String FDraft, String FinalDra,
String Ban, String Dis, String Nem,
String Bud,String Pro, String Im,String Comm) {
this.Component = new SimpleStringProperty(Comp);
this.Activity = new SimpleStringProperty(Act);
this.Torr= new SimpleStringProperty(Tor);
this.Contract= new SimpleStringProperty(Co);
this.FirstDraft = new SimpleStringProperty(FDraft);
this.FinalDraft = new SimpleStringProperty(FinalDra);
this.Bank = new SimpleStringProperty(Ban);
this.Disclosure = new SimpleStringProperty(Dis);
this.Nema= new SimpleStringProperty(Nem);
this.Budget = new SimpleStringProperty(Bud);
this.Provided = new SimpleStringProperty(Pro);
this.Implementation= new SimpleStringProperty(Im);
this.Comment = new SimpleStringProperty(Comm);
//this.Torr= new SimpleStringProperty(Tor);
}
public String getComponent() {
return Component.get();
}
public void setComponent(String Comp) {
Component.set(Comp);
}
public String getActivity() {
return Activity.get();
}
public void setAcivity(String Act) {
Activity.set(Act);
}
public String getTorr() {
return Torr.get();
}
public void setTorr(String Tor) {
Torr.set(Tor);
}
public String getContract() {
return Contract.get();
}
public void setContract(String Co) {
Contract.set(Co);
}
public String getFirstDraft() {
return FirstDraft.get();
}
public void setFirstDraft(String FDraft) {
FirstDraft.set(FDraft);
}
public String getFinalDraft() {
return FinalDraft.get();
}
public void setFinalDraft(String FinalDra) {
FinalDraft.set(FinalDra);
}
public String getBank() {
return Bank.get();
}
public void setBank(String Ban) {
Bank.set(Ban);
}
public String getDisclosure() {
return Disclosure.get();
}
public void setDisclosure(String Dis) {
Disclosure.set(Dis);
}
public String getNema() {
return Nema.get();
}
public void setNema(String Nem) {
Nema.set(Nem);
}
public String getBudget() {
return Budget.get();
}
public void setBudget(String Bud) {
Budget.set(Bud);
}
public String getProvided() {
return Provided.get();
}
public void setProvide(String Pro) {
Provided.set(Pro);
}
public String getImplementation() {
return Implementation.get();
}
public void setImplementation(String Im) {
Implementation.set(Im);
}
public String getComment() {
return Comment.get();
}
public void setComment(String Comm) {
Comment.set(Comm);
}
}
private void write() throws FileNotFoundException, IOException{
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet spreadsheet = workbook.createSheet("sample");
HSSFRow row =null;
HSSFCell cell=null;
row= spreadsheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue("First name");
cell = row.createCell(2);
cell.setCellValue("Last NAME");
cell = row.createCell(3);
cell.setCellValue("Email");
cell = row.createCell(1);
cell.setCellValue(tblComponent.getCellData(1).toString());
cell = row.createCell(2);
cell.setCellValue(tblActivity.getCellData(1).toString());
cell = row.createCell(3);
cell.setCellValue(tableColumnForTor.getCellData(1).toString());
cell = row.createCell(4);
cell.setCellValue(tblContract.getCellData(1).toString());
cell = row.createCell(5);
cell.setCellValue(tblfirst.getCellData(1).toString());
cell = row.createCell(6);
cell.setCellValue(tblFinal.getCellData(1).toString());
cell = row.createCell(7);
cell.setCellValue(tblBank.getCellData(1).toString());
cell = row.createCell(8);
cell.setCellValue(tblDisclosure.getCellData(1).toString());
cell = row.createCell(9);
cell.setCellValue(tblNema.getCellData(1).toString());
cell = row.createCell(10);
cell.setCellValue(tblBudgetRe .getCellData(1).toString());
cell = row.createCell(11);
cell.setCellValue(tblBudgetPro.getCellData(1).toString());
cell = row.createCell(12);
cell.setCellValue(tblBegin.getCellData(1).toString());
cell = row.createCell(13);
cell.setCellValue(tblComments.getCellData(1).toString());
//row = spreadsheet.createRow(2);
/*cell = row.createCell(1);
cell.setCellValue(tblComponent.getCellData(1).toString());
cell = row.createCell(2);
cell.setCellValue(tblActivity.getCellData(1).toString());
cell = row.createCell(3);
cell.setCellValue(tableColumnForTor.getCellData(1).toString());
cell = row.createCell(4);
cell.setCellValue(tblContract.getCellData(1).toString());
cell = row.createCell(5);
cell.setCellValue(tblfirst.getCellData(1).toString());
cell = row.createCell(6);
cell.setCellValue(tblFinal.getCellData(1).toString());
cell = row.createCell(7);
cell.setCellValue(tblBank.getCellData(1).toString());
cell = row.createCell(8);
cell.setCellValue(tblDisclosure.getCellData(1).toString());
cell = row.createCell(9);
cell.setCellValue(tblNema.getCellData(1).toString());
cell = row.createCell(10);
cell.setCellValue(tblBudgetRe .getCellData(1).toString());
cell = row.createCell(11);
cell.setCellValue(tblBudgetPro.getCellData(1).toString());
cell = row.createCell(12);
cell.setCellValue(tblBegin.getCellData(1).toString());
cell = row.createCell(13);
cell.setCellValue(tblComments.getCellData(1).toString());*/
/* Row row = null;
Cell cell = null;
for (int i=0;i<jtable.getRowCount();i++) {
row = sheet.createRow(i);
for (int j=0;j<jtable.getColumnCount();j++) {
cell = row.createCell(j);
cell.setCellValue((String) jtable.getValueAt(i, j));
}
}*/
// row = spreadsheet.createRow(2);
FileOutputStream out = new FileOutputStream(new File("example.xlsx"));
workbook.write(out);
out.close();
System.out.println("Data is wrtten Successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
@FXML
private void onClickToAddOntheTable (ActionEvent event){
tableViewForExcel.getItems().add(new Items(
cmbComponent.getValue().toString(),
txtActivity.getText(),
dateTor.getValue().toString(),
dateContract.getValue().toString(),
dateFirstDraft.getValue().toString(),
dateFinalDraft.getValue().toString(),
dateBank.getValue().toString(),
dateDisclosure.getValue().toString(),
dateNema.getValue().toString(),
dateBudget.getValue().toString(),
dateProvided.getValue().toString(),
dateImplementation.getValue().toString(),
txtComments.getText()));
}
@FXML
private void onClickToSaveInExcel (ActionEvent event){
try {
write();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@FXML
private void onClickToReset (ActionEvent event){
cmbComponent.setValue("");
txtActivity.clear();
dateTor.setValue(null);;
dateContract.setValue(null);
dateFirstDraft.setValue(null);
dateFinalDraft.setValue(null);
dateBank.setValue(null);
dateDisclosure.setValue(null);
dateNema.setValue(null);
dateBudget.setValue(null);
dateProvided.setValue(null);
dateImplementation.setValue(null);
txtComments.clear();
}
@FXML
private void onClickToResetTable (ActionEvent event){
tableViewForExcel.getItems().clear();
}
}
特别是在代码的这一部分,我不知道如何迭代,以便我可以获取表视图上的每个项目并将其导出到excel表格中的多个
cell = row.createCell(1);
cell.setCellValue(tblComponent.getCellData(1).toString());
cell = row.createCell(2);
cell.setCellValue(tblActivity.getCellData(1).toString());
cell = row.createCell(3);
cell.setCellValue(tableColumnForTor.getCellData(1).toString());
cell = row.createCell(4);
cell.setCellValue(tblContract.getCellData(1).toString());
cell = row.createCell(5);
cell.setCellValue(tblfirst.getCellData(1).toString());
cell = row.createCell(6);
cell.setCellValue(tblFinal.getCellData(1).toString());
cell = row.createCell(7);
cell.setCellValue(tblBank.getCellData(1).toString());
cell = row.createCell(8);
cell.setCellValue(tblDisclosure.getCellData(1).toString());
cell = row.createCell(9);
cell.setCellValue(tblNema.getCellData(1).toString());
cell = row.createCell(10);
cell.setCellValue(tblBudgetRe .getCellData(1).toString());
cell = row.createCell(11);
cell.setCellValue(tblBudgetPro.getCellData(1).toString());
cell = row.createCell(12);
cell.setCellValue(tblBegin.getCellData(1).toString());
cell = row.createCell(13);
cell.setCellValue(tblComments.getCellData(1).toString());
需要帮助吗?
答案 0 :(得分:0)
您需要为每一行创建新的HSSFRow()
个对象。现在,您只创建一行并仅将第一行数据传递给它。这就是为什么在你的excel中你只看到你的第一排。使用循环为每个HSSFRows
创建新的tableRow
。使用以下
HSSFRow row =null;
int i=1;
for(Items item: tableViewForExcel.getItems()){
row= spreadsheet.createRow(i);
row.createCell(1).setCellValue(item.getComponent);
//.... add other column data as well
i++;
}
或使用以下
HSSFRow row =null;
for(int i=0;i<tableViewForExcel.getItems().size();i++){
row= spreadsheet.createRow(i);
for(int j=0; j< tableViewForExcel.getColumns().size();j++) {
if(tableViewForExcel.getColumns().get(j).getCellData(i) != null) {
row.createCell(j).setCellValue(tableViewForExcel.getColumns().get(j).getCellData(i).toString());
}else{
row.createCell(j).setCellValue("");
}
}
}