0
C#的新手並試圖從網站上的數據表中獲取數據並將其保存到csv文件中。到目前爲止,我已經設法將數據導入到csv文件中,但是每個記錄都附加到A列中的新單元格(在Excel中查看)。即..將UI表格數據寫入CSV文件
A B C
1 A_record1
2 A_record2
3 A_record3
4 B_record1
5 B_record2
6 B_record3
而我想的數據是在格式csv文件...
A B C
1 A_record1 A_record2 A_record3
2 B_record1 B_record2 B_record3
的代碼我有填充CSV在第一個例子是.. 。
//divided xpath In three parts to pass Row_count and Col_count values.
String firstPart = "//div[@id='lwDataGrid']/table/tbody/tr[";
String secondPart = "]/td[";
String thirdPart = "]";
//Row and Column counts
int rowCount = driver.FindElements(By.XPath("//div[@id='lwDataGrid']/table/tbody/tr[*]")).Count - 3;
int colCount = driver.FindElements(By.XPath("//div[@id='lwDataGrid']/table/tbody/tr[1]/td")).Count;
System.Diagnostics.Debug.WriteLine(rowCount + ": This is the number of rows in the table");
System.Diagnostics.Debug.WriteLine(colCount + ": This is the number of columns in the table");
string path = @"C:\...\my.csv";
for (int j = 2; j <= colCount; j++)
{
//Used for loop for number of columns.
for (int i = 4; i <= rowCount; i++)
{
//Prepared final xpath of specific cell as per values of i and j.
String finalXpath = firstPart + i + secondPart + j + thirdPart;
//Will retrieve value from located cell and print It.
String tableData = driver.FindElement(By.XPath(finalXpath)).Text;
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(tableData);
}
}
}
的最終目標是到這個CSV比較另一個「期望的結果」 CSV檢查,在UI表中的數據如預期。如果有比比較兩個文件更有效的方法,即使用數組來比較結果csv,我願意提供建議。
任何幫助將不勝感激。
輝煌,謝謝!儘管我現在需要處理不同的列值,因爲第3列數據有一個逗號分隔的數字(即51,100.00),它分成兩列。 – alex
實際上,只是試圖用引號包裝tabledata,它的工作原理即line = line + string.Format(「{0},」,「\」「+ tableData +」\「」); – alex
除了用逗號分隔的文件,您可以使用管道符號或任何未用作數據一部分的符號。然後,在導入文件時,您可以將該符號指定爲分隔符。 –