2014-07-11 2 views
0

HTML 표를 CSV 파일로 구문 분석하지만 원하는 수의 colspan 및 rowpspan을 유지하려고합니다.HTML 표를 CSV 파일 (colspan 및 rowspan)로 구문 분석

나는 을 사용하고 있습니다. ";" 구분 기호 셀입니다. 따라서, 예를 들어 2 개 열의 컬럼 스팬이있을 때 ";"는 2를 갖습니다.

tr 인디케이터가 끝나는 곳에서 테이블 내용을 추출하고 줄 바꿈을 할 수 있습니다. 하지만 치료 방법을 모르겠다 colspanrowspan.

HtmlNodeCollection rows = tables[0].SelectNodes("tr"); 

// Aux vars 
int i; 
// ncolspan 

// For each row... 
for (i = 0; i < rows.Count; ++i) 
{ 
    // For each cell in the col... 
    foreach (HtmlNode cell in rows[i].SelectNodes("th|td")) 
    { 
     /* Unsuccessful attempt to treat colspan 
     foreach (HtmlNode n_cell in rows[i].SelectNodes("//td[@colspan]")) 
     { 
      ncolspan = n_cell.Attributes["colspan"].Value; 
     } 
     */ 

     text.Write(System.Text.RegularExpressions.Regex.Replace(cell.InnerText, @"\s\s+", "")); 
     text.Write(";"); 
     /* 
     for (int x = 0; x <= int.Parse(ncolspan); x++) 
     { 
      text.Write(";"); 
     } 
     */ 
    } 
    text.WriteLine(); 
    ncolspan = "0"; 
} 

도움이 필요하십니까? 고맙습니다!

UPDATE : 여기 간단한 예제 테이블 사용하기 :

<table id="T123" border="1"> 
    <tr> 
     <td colspan="3"><center><font color="red">Title</font></center></td> 
    </tr> 
    <tr> 
     <th>R1 C1</th> 
     <th>R1 C2</th> 
     <th>R1 C3</th> 
    </tr> 
    <tr> 
     <td>R2 C1</td> 
     <td>R2 C2</td> 
     <td>R2 C3</td> 
    </tr> 
    <tr> 
     <td colspan="2">R3 C1 e C2 with "</td> 
     <td>R3 C3</td> 
    </tr> 
    <tr> 
     <td>R4 C1</td> 
     <td colspan=2>R4 C2 e C3 without "</td> 
    </tr> 
    <tr> 
     <td>R5 C1</td> 
     <td>R5 C2</td> 
     <td>R5 C3</td> 
    </tr> 
    <tr> 
     <td rowspan ="2">R6/R7 C1: Two lines rowspan. Must leave the second line blank.</td> 
     <td>R6 C2</td> 
     <td>R6 C3</td> 
    </tr> 
    <tr> 
     <td>R7 C2</td> 
     <td>R7 C3</td> 
    </tr> 
    <tr> 
     <td>End</td> 
    </tr> 
</table> 

답변

1

CSV는 ROWSPAN 또는 열 병합 값을 처리하지 않습니다 - 그것은 구분 기호이고 넘어 열 또는 행의 개념이없는 매우 간단한 형식이다 줄의 끝 문자.

rowspan 및 colspan을 유지하려면 모델을 CSV로 내보내기 전에 셀의 특정 내용과 위치를 저장하는 데 사용할 수있는 중간 개체 모델을 사용해야합니다. 그런 다음 CSV 형식은 원하는대로 Excel 스프레드 시트처럼 colspan 및 rowspan을 유지하지 않습니다.

0

당신이 CSV 형식으로 ROWSPAN 또는 열 병합을 넣을 수 없다는 사실이다, 무엇 나를 위해 일한 것은 스팬 그것은 최고의 옵션이 아닙니다

존재한다 공백을 넣어하지만, 심미적 그것은 본다 유사

"";SEPTIEMBRE;;OCTUBRE;;NOVIEMBRE;;TOTAL; 
PRODUCTOS;cantidad;monto;cantidad;monto;cantidad;monto;cantidad;monto