如何使用Java删除excel(csv)表中的行?

时间:2017-07-30 12:03:53

标签: java excel eclipse file csv

我正在编写一个程序,我可以在其中将人员的详细信息添加到Excel工作表中。我还必须能够编辑这些细节,因此我想要做的是删除旧的细节,然后将新的细节写入文件。每个人的所有细节都存储在一行中。我希望能够通过使用正在更改的人的电子邮件来定位每一行。我怎样才能做到这一点?我已经尝试了其他人解决方案,但我们找不到他们的方案。 Belo是我的程序的基本版本,可以帮助您理解:

以下代码是我将人员的详细信息写入文件的位置。一切正常。

JButton addClientButton = new JButton("Add");
    addClientButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            PrintWriter pw = null;
            Client newClient = new Client(firstNameTextField.getText(), lastNameTextField.getText(), emailTextField.getText(), phoneTextField.getText(), weightTextField.getText(), heightTextField.getText(), ageSpinner.getValue(), activityLevelComboBox.getSelectedItem());
            try {

                pw = new PrintWriter(new FileOutputStream(new File("Clients.csv"), true)); 
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            StringBuilder sb = new StringBuilder();
            sb.append(newClient.getFirst());
            sb.append(",");
            sb.append(newClient.getLast());
            sb.append(",");
            sb.append(newClient.getEmail());
            sb.append(",");
            sb.append(newClient.getPhone());
            sb.append(",");
            sb.append(newClient.getWeight());
            sb.append(",");
            sb.append(newClient.getHeight());
            sb.append(",");
            sb.append(newClient.getClientAge());
            sb.append(",");
            sb.append(newClient.getClientActivity());
            sb.append("\n");

            pw.write(sb.toString());
            pw.close();
        }
    });

下面是代码的开头,我必须能够使用存储在“editClient”对象中的新编辑细节来替换旧细节。

JButton btnSave1 = new JButton("Save");
        btnSave1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Client editClient = new Client(firstNameField1.getText(), lastNameField1.getText(), emailField1.getText(), phoneField1.getText(), weightField1.getText(), heightField1.getText(), ageSpinner1.getValue(), activityComboBox1.getSelectedItem());     
            }
        });

2 个答案:

答案 0 :(得分:2)

您可以使用Apache POI lib,您可以下载https://git-scm.com/docs/git-config#git-config-receiveadvertisePushOptions

    public JsonResult FetchTblData()
    {

        string MyTableName = Convert.ToString(Request.Form["TblName"]);

        DbContext result;

        try
        {
            Type tableType = typeof(CourseDesc);
            switch (MyTableName)
            {
                case "CourseTbl":
                    tableType = typeof(CourseTbl);
                    break;
                case "CourseDescTbl":
                    tableType = typeof(CourseDesc);
                    break;
            }


            using (EBContext db = new EBContext())
            {
                result = db.Set(tableType);
            }

            return new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
         }
        catch (Exception ex)
        {
            string innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : "";
             return new JsonResult { Data = "Not Found", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }

here另一个有用的话题。

答案 1 :(得分:1)

是。您可以使用Apache POI或JXL编辑excel或csv fiels。