在 Visual Studio C# 中写入现有 CSV 或 Excel 文件
Writing to existing CSV or Excel File in Visual Studio C#
我希望能够持续更新 CSV 或 Excel xlsx 文件,可能真的使用 NPOI 或其他任何东西。我只需要完成以下。
我的代码示例如下,我正在捕获时间。该代码正在使用硒。每次测试 运行,我都需要将这些时间导出到现有的 excel/csv 文件,我将存储在我的 C: 驱动器上。我不想覆盖现有的单元格,而是每次都想在下一个空白行中添加新的时间。
我现有的 excel 文件只有 3 header 列。这些将被命名为 "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 for Google to load is {0}", sw1.Elapsed);
var sw2 = Stopwatch.StartNew();
driver.Navigate().GoToUrl("http://www.facebook.com");
sw2.Stop();
Console.WriteLine("Time for Facebook to load is {0}", sw2.Elapsed);
/*TODO code to write the Elapsed time into 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));
}
使用 CSV 文件:
using (StreamWriter sw = new StreamWriter("pippo.csv", true))
{
sw.WriteLine(string.Format("{0};{1};{2}", DateTime.Now, "Google Perf Test", ws1.Elapsed));
sw.WriteLine(string.Format("{0};{1};{2}", DateTime.Now, "Facebook Perf Test", ws2.Elapsed));
}
类似的东西会起作用:
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);
}
}
您也可以使用文件 Class
这是一个可以使用的小片段
var csv = new StringBuilder();
var newLine = string.Format("{0},{1},{2}", firstValue, secondValue, thirdValue);
csv.AppendLine(newLine);
File.AppendAllText(fileUrl, csv.ToString());
我希望这对你有用。
我希望能够持续更新 CSV 或 Excel xlsx 文件,可能真的使用 NPOI 或其他任何东西。我只需要完成以下。
我的代码示例如下,我正在捕获时间。该代码正在使用硒。每次测试 运行,我都需要将这些时间导出到现有的 excel/csv 文件,我将存储在我的 C: 驱动器上。我不想覆盖现有的单元格,而是每次都想在下一个空白行中添加新的时间。
我现有的 excel 文件只有 3 header 列。这些将被命名为 "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 for Google to load is {0}", sw1.Elapsed);
var sw2 = Stopwatch.StartNew();
driver.Navigate().GoToUrl("http://www.facebook.com");
sw2.Stop();
Console.WriteLine("Time for Facebook to load is {0}", sw2.Elapsed);
/*TODO code to write the Elapsed time into 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));
}
使用 CSV 文件:
using (StreamWriter sw = new StreamWriter("pippo.csv", true))
{
sw.WriteLine(string.Format("{0};{1};{2}", DateTime.Now, "Google Perf Test", ws1.Elapsed));
sw.WriteLine(string.Format("{0};{1};{2}", DateTime.Now, "Facebook Perf Test", ws2.Elapsed));
}
类似的东西会起作用:
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);
}
}
您也可以使用文件 Class 这是一个可以使用的小片段
var csv = new StringBuilder();
var newLine = string.Format("{0},{1},{2}", firstValue, secondValue, thirdValue);
csv.AppendLine(newLine);
File.AppendAllText(fileUrl, csv.ToString());
我希望这对你有用。