Writing to existing CSV or Excel File in Visual Studio C# -
i want able continually update csv or excel xlsx file, maybe using npoi or else really. need accomplish below.
example of code below, capturing timings. code using selenium. each time test run, need timings exported existing excel/csv file have stored on c: drive. dont want overwrite existing cells, instead want add new timings on next blank row each time.
my existing excel file, have 3 header columns. these titled "date of test", "name of test", "elapsed time"
[test] public void timeforwebpagestoload() { iwebdriver driver = new firefoxdriver(); datetime currentdatetime = datetime.now; var datetime = currentdate.tostring(); var sw1 = stopwatch.startnew(); driver.navigate().gotourl("http://www.google.com"); sw1.stop(); console.writeline("time google load {0}", sw1.elapsed); var sw2 = stopwatch.startnew(); driver.navigate().gotourl("http://www.facebook.com"); sw2.stop(); console.writeline("time facebook load {0}", sw2.elapsed); /*todo code write elapsed time excel writetoexcelmethod(datetime, "google perf test", sw1.elapsed) writetoexcelmethod(datetime, "facebook perf test", sw2.elapsed) */ assert.istrue(sw1.elapsed < timespan.fromseconds(10)); assert.istrue(sw2.elapsed < timespan.fromseconds(10)); }
something work:
public static void writetoexcelmethod(datetime dt, string str, timespan ts) { string path = @"c:\temp\mytest.csv"; string line = string.format(@"""{0}"",""{1}"",""{2}""", dt, str, ts); using (streamwriter sw = file.appendtext(path)) { sw.writeline(line); } }
Comments
Post a Comment