【问题标题】:How do I delete a row in an excel (csv) sheet using Java?如何使用 Java 删除 excel (csv) 表中的一行?
【发布时间】:2018-01-06 01:33:29
【问题描述】:

我正在编写一个程序,我可以在其中将人员的详细信息添加到 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());     
            }
        });

【问题讨论】:

  • 为什么这些解决方案不起作用?也许你可以分享一些代码?是 Excel 还是 CSV?
  • solution not working 不是编程的合适术语。正确的术语是I have an exception/unexpected behaviour when I'm executing the following code

标签: java excel eclipse file csv


【解决方案1】:

可以使用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 另一个有用的话题。

【讨论】:

  • 非常感谢,我现在就试试这个解决方案,让你知道我的进展情况。
  • 在您的代码中,您将“outputStream”写入文件。你为什么这么做?我在哪里写客户的新编辑细节?
  • 您正在编辑 datatypeSheet ,它是一个 Sheet 对象,而此工作表是工作簿的工作表。 “ workbook.write(outputStream) ” 将工作簿项导出到实际的 excel 文件中。
  • 我也编辑过。您应该使用单元格索引逐个单元格添加。我相信这就是您所需要的。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多