繁体   English   中英

使用Java Apache POI将数据从JFrame写入excel

[英]write Data from JFrame to excel using java apache poi

在这里,数据是通过JFrame-JText字段输入的,该字段应保存到Excel表中

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

然后,将其通过Customer.class传递,以保存在它们各自的字段中。

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

然后使用此类将数据传递到excel文件中,以便从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());
    }

}

}

我的主要问题是将数据正确地从一个类传递到excel工作表,并在完成后调用/运行该类。 任何帮助是极大的赞赏。

将创建Excel文件并将其客户写入代码的代码移动到一个函数public void createWorkbook(File filename, Collection<Customer> customers)

然后,在您的actionPerformed方法上,使用输入字段创建一个Customer对象,就像已经完成的那样,然后将该Customer对象添加到“客户集合”中。 使该集合可供JFrame访问。

然后在JFrame上添加一个Export to Excel按钮,并在该按钮上的actionPerformed方法中调用createWorkbook(File, Collection<Customer>)

如果我错误地认为您想将多个客户保存到一个Excel文件中,请不要理会Collection和2nd按钮业务。

但是,如果要创建一个由多个客户组成的文件,则需要累积数据以将其写入Excel文件,然后一次性全部写入。 这样可以避免不必要的I / Of,而不必编写代码来检测要写入第一个空白行的位置,而不必担心文件在使用它的应用程序之间被修改,等等。

是什么阻止了您

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

代替调用c.main()

暂无
暂无

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

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