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

如何使用Java删除excel(csv)工作表中的行?,java,excel,eclipse,file,csv,Java,Excel,Eclipse,File,Csv,我正在编写一个程序,在其中我可以将人员的详细信息添加到excel工作表中。我还必须能够编辑这些细节,因此我想做的是删除旧的细节,然后只写新的文件。每个人的所有详细信息都存储在一行中。我希望能够通过使用被修改人的电子邮件来定位每一行。我该怎么做?我已经尝试过其他人的解决方案,我已经找到了,但他们不为我的计划工作。以下是我的程序的基本版本,帮助您了解: 下面的代码是我将人员的详细信息写入文件的地方。这一切都很好 JButton addClientButton = new JButton("Add")

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

下面的代码是我将人员的详细信息写入文件的地方。这一切都很好

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

您可以使用ApachePOI库,您可以下载

import org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.*;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Iterator;
公营业务{
私有静态最终字符串文件\u NAME=“MyFirstExcel.xls”;
公共静态void main(字符串[]args){
//ArrayList电子邮件=新建ArrayList();
//int cellIndex=0;
试一试{
FileInputStream excelFile=newfileinputstream(新文件(文件名));
工作簿=新的HSSF工作簿(Excel文件);
工作表数据类型工作表=工作簿。getSheetAt(0);
迭代器迭代器=datatypeSheet.Iterator();
//用于(字符串电子邮件:电子邮件){
while(iterator.hasNext()){
Row currentRow=iterator.next();
迭代器cellIterator=currentRow.Iterator();
//while(cellIterator.hasNext()){
if(currentRow.getCell(2.getCellTypeEnum()==CellType.STRING){
//控制你的电子邮件,我想电子邮件会留在第三个手机里
如果(currentRow.getCell(2.getStringCellValue().compareTo(editClient.getEmail())!=0){
继续;
}
}
//Cell currentCell=cellIterator.next();
//你的代码在这里。。
currentRow.getCell(0.setCellValue(editClient.getFirst());
currentRow.getCell(1.setCellValue(editClient.getLast());
//.. 
//currentCell.setCellValue(editClient.getFirst());
//}
}
//}
//更新
//刷新数据类型表对象后,应将其写回文件
FileOutputStream outputStream=新的FileOutputStream(文件名);
workbook.write(outputStream);
workbook.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}   
}

另一个有用的主题。

是的。您可以使用Apache POI或JXL编辑excel或csv文件。

为什么这些解决方案不起作用?也许您可以共享一些代码?是excel还是csv?
解决方案不起作用
对于编程来说不是一个合适的术语。合适的术语是
我在执行时出现异常/意外行为使用以下代码
非常感谢,我现在将尝试此解决方案,并让您知道我的进展情况。在您的代码中,您将“outputStream”写入文件。为什么要这样做?我在哪里写入客户端新编辑的详细信息?您正在编辑datatypeSheet,它是一个工作表对象,而此工作表是工作簿的工作表。“workbook.write(outputStream)”将工作簿项导出到实际的excel文件中。我也进行了编辑。您应该逐单元格添加单元格索引。我相信这就是您需要的。
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();
    }   

}