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

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

下面的代码是我写人的细节的文件。 这一切工作正常。

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”对象中存储的新编辑的细节来replace旧的细节。

 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,你可以在这里下载。

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

这里另一个有用的话题

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