java - Don't create a new CSV file each time -
so trying make work each time use method (which found on internet) creates new csv file, want add data each time, without removing there.
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 want writer.append("\n"); writer.flush(); writer.close(); } catch (ioexception e) { e.printstacktrace(); } }
and call method.
if(e.getsource().equals(save)){ generatecsvfile("f:\\testss.csv"); joptionpane.showmessagedialog(null, "saved!"); }
you calling wrong constructor while creating filewriter. rewrite to:
new filewriter(sfilename, true);
that should preserve data. here direct link documentation.
Comments
Post a Comment