Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaApachePOI将数据从JFrame写入excel_Java_Excel_Jframe_Apache Poi - Fatal编程技术网

使用JavaApachePOI将数据从JFrame写入excel

使用JavaApachePOI将数据从JFrame写入excel,java,excel,jframe,apache-poi,Java,Excel,Jframe,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 JTextF

在这里,数据是通过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());
    }

}

}
公共C类{
公共静态void main(){
试一试{
File File=新文件(“C:\\Users\\Sumuel\\Desktop\\Query.xls”);
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet sheet=workbook.createSheet(“C.info”);
//创建标题
行标题=sheet.createRow(0);
rowHeading.createCell(0.setCellValue(“名称”);
行标题.createCell(1).setCellValue(“电子邮件”);
行标题.createCell(2).setCellValue(“地址”);
行标题.createCell(3).setCellValue(“电话”);
对于(int i=0;i<4;i++){
CellStyleHeading=workbook.createCellStyle();
Font=workbook.createFont();
font.setBold(true);
styleHeading.setFont(字体);
rowHeading.getCell(i).setCellStyle(styleHeading);
}

对于(int i=0;i移动创建Excel文件的代码,并将客户写入函数
public void create工作簿(文件名,集合客户)

然后在actionPerformed方法上,使用输入字段创建一个Customer对象,如您所做的,并将该Customer对象添加到Customer集合中。使JFrame可以访问此集合

然后在
JFrame
上添加一个导出到Excel按钮,并在该按钮上的
actionPerformed
方法中调用
create工作簿(文件、集合)

如果我错误地认为您希望将多个客户保存到一个Excel文件中,则忽略收集和第二按钮业务

但是,如果您要创建一个包含多个客户的文件,则需要累积数据以写入Excel文件,并一次性写入。这避免了不必要的I/Of和必须编写代码以检测第一个空行要写入的位置,以及担心在应用程序使用文件之间修改文件等

是什么阻止了你

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

代替调用
c.main()的位置

您必须尝试运行您的代码,如果有问题,请在此询问您使用的是
Customer c1;
没有c1的init值,然后您使用它获取值,===>空指针异常,您需要在某个函数中将Customer类型的值传递给此
c1
@TuyenNguyen,对不起,您可以更具体一点吗(我还是新手)你试着运行你的代码了吗?你的代码有很多问题,我不知道这是你的代码还是你从某处得到的,但你对此一无所知。在第一部分,最后你使用:
Connect c=new Connect();c.main()
但是3块代码,你定义你的类是C?那么什么是
Connect
类?你不应该定义一个名为
main
的函数,因为
main
通常用于程序的启动方法。建议你调用
main
以数据形式编写excel
Customer
,然后你就可以了应将其传递给
main
。示例:
c.main(c);