![](/img/trans.png)
[英]How to export data to Excel sheet using Selenium webdriver in C#
[英]How to write data into an Excel file using Selenium WebDriver with C#?
如何在C#中使用Selenium WebDriver將數據寫入excel文件。 我正在使用配置文件讀取所有Excel數據。
您能否分享造成問題的代碼部分。 以下是示例代碼,可能會幫助您-
從數據表到csv-
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = resultstable.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in resultstable.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field =>
string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText("C:\\path\\Test.csv", sb.ToString());
要將其直接寫入csv,請使用system.io中的File函數
File.AppendAllText(@"C:\\path\\test.csv","data to write"+ Environment.NewLine);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace WriteExcelData
{
class Program
{
static void Main(string[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
List MyInfo = new List { "CLASS NAME", "RESULT", "ReadExcelData", "Success", "WriteExcelData", "Success" };
int index = 0;
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 2; j++)
{
xlWorkSheet.Cells[i, j] = MyInfo[index];
index++;
}
}
var fileLocation = "C:\\Users\\user\\Desktop\\Selenium Learing\\WriteExcelData\\TestResultFile\\MyExcelFile.xls";
xlWorkBook.SaveAs(fileLocation, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.