2016-09-30 2 views
0

C#을 처음 사용하면서 웹 사이트의 데이터 테이블에서 데이터를 가져 와서 csv 파일에 저장하려고합니다. 지금까지 나는 데이터를 CSV 파일로 가져 왔지만 각 레코드는 Excel에서 볼 때 A 열의 새 셀에 추가됩니다. 즉 .. UI 테이블 데이터를 CSV 파일에 쓰기

A   B   C 
1 A_record1 
2 A_record2 
3 A_record3 
4 B_record1 
5 B_record2 
6 B_record3 

I의 형식 CSV 파일로 데이터를 원하는 반면

...

A   B   C 
1 A_record1 A_record2 A_record3 
2 B_record1 B_record2 B_record3 

인 제 1 실시 예에 CSV 채워 I 가지고있는 코드 .. .

//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); 
     } 
    } 
} 

최종 목표는 UI 테이블의 데이터가 예상대로 있는지 확인하기 위해 다른 "예상 결과"CSV로이 CSV를 비교하는 것입니다. 두 파일을 비교하는 것보다 효율적인 방법이 있다면 즉, 배열을 사용하여 결과 CSV와 비교하는 것입니다. 제안을 할 수 있습니다.

도움을 주시면 감사하겠습니다.

답변

0

에서 코드를 변경 - 아래에

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); 
     } 
    } 
} 

문제를

for (int j = 2; j <= colCount; j++) 
     { 
      using (StreamWriter sw = File.AppendText(path)) 
      { 
       string line = string.Empty; 
       //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; 
        line = line + string.Format("{0},",tableData); 
       } 
       sw.WriteLine(line); 
      } 
     } 
+0

브릴리언트 감사를 해결할 수 있습니다! 비록 3 열 데이터가 쉼표 (즉, 51,100.00)를 사용하여 2 열 이상으로 나뉘는 숫자를 가지므로 다른 열 값을 다르게 처리해야하지만 이제는 조금 더 나아갑니다. – alex

+0

사실, 그냥 인용 부호로 둘러싼 테 블라 타를 시도해 보았습니다. 즉, line = line + string.Format ("{0}", "\" "+ tableData +"\ ""); – alex

+0

쉼표로 구분 된 파일 대신 파이프 기호 나 데이터의 일부로 사용되지 않는 기호를 사용할 수 있습니다. 그런 다음 파일을 가져 오는 동안 해당 기호를 구분 기호로 지정할 수 있습니다. –