Java 如何迭代Tableview中的数据以将其导出到Microsoft Excel?

Java 如何迭代Tableview中的数据以将其导出到Microsoft Excel?,java,excel,javafx,javafx-2,Java,Excel,Javafx,Javafx 2,我需要从表视图中迭代数据以将其导出到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 ja

我需要从表视图中迭代数据以将其导出到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();

}


}

需要帮助吗?

您需要为每行创建新的
HSSFRow()
对象。现在您只创建了一行,并且只将第一行数据传递给它。这就是为什么在excel中,您只看到第一行。使用循环,为每个
表格行创建新的
hssfrow
。使用以下命令

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("");
                 }
             }
         }
或者使用以下命令

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("");
                 }
             }
         }
HSSFRow行=null;

对于(int i=0;i)你指的是哪个列表?