简体   繁体   English

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

[英]How To Iterate data from Tableview to Export it to Microsoft Excel?

I need help in iterating data from table-view to export it to excel, every time i press a button it just displays the first row data from the table-view on my excel sheet. 我需要帮助从表视图迭代数据以将其导出到excel,每次按下按钮它只显示我的Excel工作表上的表视图中的第一行数据。

I hope you are getting what am saying this is my code 我希望你得到的是我的代码

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();

}


}

Especialy on this section of the code i dont know how to iterate so that i could get every items on the table-view and export it on excel sheet in multiple 特别是在代码的这一部分,我不知道如何迭代,以便我可以得到表视图上的每个项目,并将其导出到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());

need help ? 需要帮忙 ?

You need to create new HSSFRow() objects for each row. 您需要为每一行创建新的HSSFRow()对象。 Right now you are creating just one row and passing the first row data only to it. 现在,您只创建一行并仅将第一行数据传递给它。 That's why in your excel you are seeing just your first row. 这就是为什么在你的excel中你只看到你的第一排。 Use loop, to create new HSSFRows for each of your tableRow . 使用循环为每个tableRow创建新的HSSFRows Use the following 使用以下内容

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++;
}

or use the following 或使用以下内容

    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("");
                 }
             }
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM