如何使用 Java 从 Excel (csv) 文件中删除一行? [英] How to remove a row from an Excel (csv) file using Java?
本文介绍了如何使用 Java 从 Excel (csv) 文件中删除一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 GUI 程序,我在其中将人员的个人详细信息写入保存为 CSV 文件的 Excel 电子表格.我可以很好地读写它,但是,我必须能够从文件中删除人员.我当前的代码如下:
I have a GUI program in which I write the personal details of people to an Excel spreadsheet saved as a CSV file. I can read and write to it just fine, however, I must be able to remove people from the file. My current code is down below:
JButton btnDeleteClient = new JButton("Delete Client");
btnDeleteClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Client deleteClient = null;
try {
String line = "";
br = new BufferedReader(new FileReader("Clients.csv"));
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
String clientName = data[0];
if(data.length > 1) {
clientName = clientName + " " + data[1];
if(clientName.equals(comboBox_1.getSelectedItem())){
deleteClient = new Client(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
}
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
try {
FileInputStream excelFile = new FileInputStream(new File("Clients.csv"));
Workbook workbook = new HSSFWorkbook(excelFile);
org.apache.poi.ss.usermodel.Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
if (currentRow.getCell(2).getCellTypeEnum() == CellType.STRING) {
if (currentRow.getCell(2).getStringCellValue().compareTo(deleteClient.getEmail()) != 0) {
continue;
}
}
currentRow.getCell(0).setCellValue("");
currentRow.getCell(1).setCellValue("");
currentRow.getCell(2).setCellValue("");
currentRow.getCell(3).setCellValue("");
currentRow.getCell(4).setCellValue("");
currentRow.getCell(5).setCellValue("");
currentRow.getCell(6).setCellValue("");
currentRow.getCell(7).setCellValue("");
}
FileOutputStream outputStream = new FileOutputStream("Clients.csv");
workbook.write(outputStream);
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnDeleteClient.setBounds(463, 348, 120, 23);
existingClients.add(btnDeleteClient);
当我运行程序并单击删除按钮时,出现以下错误:
When I run the program and click the delete button, I get the following error:
org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x6C6F432C79657254, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:181)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:302)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:413)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:394)
at IA$77.actionPerformed(IA.java:1837)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
推荐答案
JButton btnDeleteClient = new JButton("Delete Client");
btnDeleteClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
List<String[]> myEntries = new ArrayList<>();
CSVReader reader = new CSVReader(new FileReader("Clients.csv"),',');
myEntries = reader.readAll();
List<String[]> toRemove = new ArrayList<>();
for (String[] row: myEntries){
if(comboBox_1.getSelectedItem().equals(row[0]+" "+row[1])){
toRemove.add(row);
}
}
myEntries.removeAll(toRemove);
CSVWriter writer = new CSVWriter(new FileWriter("Clients.csv"), ',',CSVWriter.NO_QUOTE_CHARACTER);
for(String[]row : myEntries){
writer.writeNext(row);
}
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
这篇关于如何使用 Java 从 Excel (csv) 文件中删除一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文