我有一个GUI程序,我将人员的个人详细信息写入保存为CSV文件的Excel电子表格中。我可以读它写得很好,但是,我必须能够从文件中删除人。我目前的代码如下:
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);
当我运行程序并单击删除按钮时,出现以下错误:
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)
答案 0 :(得分:0)
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();
}
}
});