简体   繁体   中英

write Data from JFrame to excel using java apache poi

Here the Data is entered through a JFrame - JText field which is meant to be saved into an Excel table

public DetailsPanel(){    
    final JTextField insertName = new JTextField(20);
    final JTextField insertEmail = new JTextField(20);
    final JTextField insertPhone = new JTextField(20);
    final JTextField insertAddress = new JTextField(20);
...
    Create = new JButton("Create");

    Create.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String name = insertName.getText();
            String email = insertEmail.getText();
            String phone = insertPhone.getText();
            String address = insertAddress.getText();               

            Customer C = new Customer(name, email, address, phone);
            C.setName(name);
            C.setEmail(email);
            C.setAddress(address);
            C.setPhone(phone);

            Connect c = new Connect();
            c.main();
        }
    });

It is then passed through an Customer.class to be saved within their respective fields.

public class Customer {

private String name;
private String Email;
private String Address;
private String Phone;


public Customer(String name, String email, String address, String phone) {
    super();
    this.name = name;
    Email = email;
    Address = address;
    Phone = phone;


}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}...

then the data is passed into an excel file using this class which is sopposed to retrieve the data from Customer.class.

public class C {
public static void main() {

    try {
        File file = new File("C:\\Users\\Sumuel\\Desktop\\Query.xls");

        HSSFWorkbook workbook =  new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("C.info");
        //Create Heading
        Row rowHeading = sheet.createRow(0);
        rowHeading.createCell(0).setCellValue("Name");
        rowHeading.createCell(1).setCellValue("Email");
        rowHeading.createCell(2).setCellValue("Address");
        rowHeading.createCell(3).setCellValue("Phone");

        for (int i = 0; i < 4; i++){
            CellStyle styleHeading = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setBold(true);
            styleHeading.setFont(font);
            rowHeading.getCell(i).setCellStyle(styleHeading);
        }

        for(int i = 0; i<4; i++){
            sheet.autoSizeColumn(i);
        }

        Customer c1;

        Row custInfo = sheet.createRow(1);
        custInfo.createCell(0).setCellValue(c1.getName());
        custInfo.createCell(1).setCellValue(c1.getEmail());
        custInfo.createCell(0).setCellValue(c1.getAddress());
        custInfo.createCell(1).setCellValue(c1.getPhone());

        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Sumuel\\Desktop\\Query.xls"));
        workbook.write(out);
        out.close();
        workbook.close();
        System.out.println("file written");

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

}

My main issue is passing the data correctly from one class to the excel sheet and calling/running that class when complete. Any help is greatly appreciated.

Move your code that creates an Excel file and writes the customers to it to a function public void createWorkbook(File filename, Collection<Customer> customers)

Then on your actionPerformed method, create a Customer object using the input fields, as you have done, and add that Customer object to a Collection of Customers. Make this collection accessible to the JFrame.

Then add an Export to Excel button on your JFrame and call createWorkbook(File, Collection<Customer>) in the actionPerformed method on that button.

If I assumed incorrectly that you wanted to save multiple customers to an Excel file, then disregard the Collection and 2nd button business.

However, if you are creating a file with multiple customers, you'll want to accumulate the data to write to an Excel file.and the write it all in one go. This avoids unnecessary I/Of and having to write code to detect where the first blank row is to write to, worrying about the file being modified between your application using it, etc.

What's stopping you from doing

c = new Connect();
c.createWorkbook(filename, customer);

In lieu of where you call c.main() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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