所以我正在努力为我的工作做点什么,但每次我使用这种方法(我在互联网上找到)它会创建一个新的CSV文件,但我想每次添加数据,而不删除以前的数据那里。
public static void generateCsvFile(String sFileName) {
try {
NummerS = Nummer.getText();
LocatiesS = Locatie.getText();
LoginS = Login.getText();
Nummer5S = Nummer5.getText();
Nummer1S = Nummer1.getText();
DatumS = Datum.getText();
FileWriter writer = new FileWriter(sFileName);
writer.append(NummerS);
writer.append(';');
writer.append(LocatiesS);
writer.append(';');
writer.append(LoginS);
writer.append(';');
writer.append(DatumS);
writer.append(';');
writer.append(Nummer1S);
writer.append(';');
writer.append(Nummer5S);
writer.append(';');
if(listt.getSelectedValue() != null)
{
writer.append(listt.getSelectedValue().toString());
}
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File("F:\\test.xls"));
} catch (BiffException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
writer.append(';');
Sheet sheet = workbook.getSheet(0);
for(Cell c : sheet.getColumn(1)){
if(listt.getSelectedValue() != null)
{
if(c.getContents().contains(listt.getSelectedValue().toString())){
System.out.println(c.getColumn() + " , " + c.getRow());
writer.append(sheet.getCell(c.getColumn()+1,c.getRow()).getContents());
} else {
}
}
}
// generate whatever data you want
writer.append("\n");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这就是我称之为方法的地方。
if(e.getSource().equals(save)){
generateCsvFile("f:\\testss.csv");
JOptionPane.showMessageDialog(null, "Saved!");
}
答案 0 :(得分:1)