java - 如何使用 Java 删除 Excel (csv) 工作表中的一行?

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

最佳答案

可以使用Apache POI lib,可以下载here .

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

public class Operations {

private static final String FILE_NAME = "MyFirstExcel.xls";

public static void main(String[] args) {
    //ArrayList<String> eMails = new ArrayList<String>();
    //int cellIndex = 0;
    try {

        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        Workbook workbook = new HSSFWorkbook(excelFile);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = datatypeSheet.iterator();

        //for (String eMail : eMails) {
            while (iterator.hasNext()) {

                Row currentRow = iterator.next();

                Iterator<Cell> cellIterator = currentRow.iterator();

                //while (cellIterator.hasNext()) {

                    if (currentRow.getCell(2).getCellTypeEnum() == CellType.STRING) {
                        // control your email, I presume email stays in the 3rd cell
                        if (currentRow.getCell(2).getStringCellValue().compareTo(editClient.getEmail()) != 0) {
                            continue;
                        }
                    }
                    //Cell currentCell = cellIterator.next();

                    // your code here..
                   currentRow.getCell(0).setCellValue(editClient.getFirst());
                   currentRow.getCell(1).setCellValue(editClient.getLast());
                   //.. 
                   //currentCell.setCellValue(editClient.getFirst());


                //}

            }
        //}
        // update
        // after refreshin your datatypeSheet objet you should write back to file
        FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
        workbook.write(outputStream);
        workbook.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   

}

Here另一个有用的话题。

关于java - 如何使用 Java 删除 Excel (csv) 工作表中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45399608/

相关文章:

java - 防止重复 Activity

java - 无法启动 ActiveMQ JMS 消息代理

java - Apache POI 插入图像

java - 在 Eclipse 中开发 App Engine/Java 项目时是否需要重新启动服务器?

java - FragmentPagerAdapter - 无法解析父类(super class)

java - 为什么在填充包含 < 类型元素的集合时行为会有所不同?扩展 T>?

java - Spring Zuul - 网关超时

PHPExcel_IOFactory::load($target_file) 在本地主机上工作,但不在服务器上工作

excel - 从 Excel 中的公式返回空单元格

java - android studio 中的 Eclipse keystore