2014-04-01 158 views
0

我在做這一切的C#行跨度和列跨度在HTML

我創建了一個表像下面

Company Name Gender 

    cmc sindhu f 
    cmc shilpa f 
    cmc srikar m 

但我需要的是....我想「CMC」只出現一次所有的名字。

Company Name Gender 

    cmc  sindhu f 
      shilpa f 
      srikar m 

如何使用這個rowspancolspan?我的代碼如下:

public class sample 

{ 
    static void Main(string[] args) 
    { 
     DataTable table=new DataTable(); 
     table.Columns.Add("Company", typeof(string)); 
     table.Columns.Add("Name", typeof(string)); 
     table.Columns.Add("Gender", typeof(string)); 

     table.Rows.Add("cmc", "sindhu", "f"); 
     table.Rows.Add("cmc", "shilpa", "f"); 
     table.Rows.Add("cmc", "srikar", "m"); 


     string htmltable; 
     string htmlstring; 
     htmltable=ConvertDataTableToHtml(table); 
     string filename = "HTML File.html"; 
     StreamWriter swXLS = new StreamWriter("c:\\"+ filename); 
     swXLS.Write(htmltable.ToString()); 
     swXLS.Close(); 

    } 
      public static string ConvertDataTableToHtml(DataTable targetTable) 
      { 
       string htmlString = ""; 

       if (targetTable == null) 
       { 
       throw new System.ArgumentNullException("targetTable"); 
       } 

       StringBuilder htmlBuilder = new StringBuilder(); 

       //Create Top Portion of HTML Document 
       htmlBuilder.AppendLine("<html>"); 
       htmlBuilder.AppendLine("<head>"); 
       htmlBuilder.AppendLine("</head>"); 
       htmlBuilder.AppendLine("<body>"); 
       htmlBuilder.AppendLine("<table border='1px' cellpadding='5' cellspacing='0'>"); 

       //Create Header Row 
       htmlBuilder.Append("<span style='font-size: small;font-weight:bold;'></span>\n"); 
       htmlBuilder.AppendLine("\n<tr align='left' valign='top'>"); 

       foreach (DataColumn targetColumn in targetTable.Columns) 
       { 

       htmlBuilder.Append("<td align='left' valign='top'>"); 
       htmlBuilder.Append("<b>"); 
       htmlBuilder.Append(targetColumn.ColumnName); 
       htmlBuilder.Append("</b>"); 
       htmlBuilder.Append("</td>\n"); 
       } 

       htmlBuilder.AppendLine("</tr>"); 

       //Create Data Rows 
       foreach (DataRow myRow in targetTable.Rows) 
       { 
       htmlBuilder.AppendLine("\n<tr align='left' valign='top'>"); 

       foreach (DataColumn targetColumn in targetTable.Columns) 
       { 
       htmlBuilder.Append("<td align='left' valign='top'>"); 
       htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString()); 
       htmlBuilder.Append("</td>\n"); 
       } 
       //htmlBuilder.Append("</td>"); 
       htmlBuilder.AppendLine("</tr>\n"); 
       } 

       //Create Bottom Portion of HTML Document 
       htmlBuilder.AppendLine("</table>"); 
       htmlBuilder.AppendLine("</body>"); 
       htmlBuilder.AppendLine("</html>"); 

       //Create String to be Returned 
       htmlString = htmlBuilder.ToString(); 

       return htmlString; 
      } 

    } 

回答

1
 //create Data Row 
     int count = 0; 
     int rowsinserted = 0; 

     foreach (DataRow myRow in targetTable.Rows) 
     { 
      htmlBuilder.AppendLine("\n<tr align='left' valign='top'>"); 

      foreach (DataColumn targetColumn in targetTable.Columns) 
      { 

       if (targetColumn == targetTable.Columns[0]) 
       { 
        if (count == 0) 
        { 
         htmlBuilder.Append("<td rowspan='" + targetTable.Rows.Count + "' 'align='left' valign='top'>"); 
         htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString()); 
         htmlBuilder.Append("</td>\n"); 
        } 

       } 
       else 

       { 
        int columncount = 1; 
        for (int i = 1; i < targetTable.Columns.Count; i++) 
        { 

         if (targetColumn == targetTable.Columns[columncount]) 
         { 

          htmlBuilder.Append("<td 'align='left' valign='top'>"); 
          htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString()); 
          htmlBuilder.Append("</td>\n"); 


         } 
         columncount = columncount + 1; 

         count = count + 1; 
        } 
      } 
      } 
      rowsinserted = rowsinserted + 1; 
      htmlBuilder.AppendLine("</tr>\n"); 
     } 
0

我認爲下面應該工作,我還沒有測試它。基本上,我試圖確定何時在第一列中首次出現值,並添加一個與該值出現的行數相同的單元格。這僅在行按第一列排序時纔有效。

//Create Data Rows 
foreach (DataRow myRow in targetTable.Rows) 
{ 
    htmlBuilder.AppendLine("\n<tr align='left' valign='top'>"); 
    foreach (DataColumn targetColumn in targetTable.Columns) 
    { 
     // if its the first column then only add a cell when its the first time 
     // value appears, else just add the cell 
     if(targetColumn == targetTable.Columns.First()) 
     { 
      // if its the first time the value has appeared in the first column 
      // then add cell with rowspan set to the number of appearances 
      if(myRow == targetTable.Rows.First(r => r[targetColumn.ColumnName].ToString() == myRow[targetColumn.ColumnName].ToString())) 
      { 
       rowspan = targetTable.Rows.Count(r => r[targetColumn.ColumnName].ToString() == myRow[targetColumn.ColumnName].ToString()); 

       htmlBuilder.Append("<td rowspan='"+rowspan+"' align='left' valign='top'>"); 
       htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString()); 
       htmlBuilder.Append("</td>\n"); 
      } 
     } 
     else 
     {    
      htmlBuilder.Append("<td align='left' valign='top'>"); 
      htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString()); 
      htmlBuilder.Append("</td>\n"); 
     } 
    } 

    htmlBuilder.AppendLine("</tr>\n"); 
} 

編輯:假設你沒有使用DataTable

這工作,我測試過它,你可以查看輸出here。我假定你不必使用DataTable,因爲使用ListDictionary這樣的枚舉更容易。理想情況下,您可能想創建自己的班級,而不是使用Dictionary,但我認爲它可以用於演示目的。

var Data = new List<Dictionary<string, string>>(); 

Data.Add(new Dictionary<string, string>() { {"Company", "Company 1"}, {"Name", "Jim"}, {"Gender", "m"} }); 
Data.Add(new Dictionary<string, string>() { {"Company", "Company 1"}, {"Name", "Bob"}, {"Gender", "m"} }); 
Data.Add(new Dictionary<string, string>() { {"Company", "Company 1"}, {"Name", "Dawn"}, {"Gender", "f"} }); 
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Dave"}, {"Gender", "m"} }); 
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Ben"}, {"Gender", "m"} }); 
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Tina"}, {"Gender", "f"} }); 
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Sarah"}, {"Gender", "f"} }); 

string html = "<table><tr>"; 

foreach (var Column in Data.First().Select(col => col.Key)) 
{ 
    html += string.Format("<th>{0}</th>", Column); 
} 

html += "</tr>"; 

foreach (var Row in Data) 
{ 
    html += "<tr>"; 

    foreach (string Column in Row.Select(col => col.Key)) 
    { 
     if(Column == Row.First().Key) 
     { 
      if(Row == Data.First(row => row.First(col => col.Key == Column).Value == Row.First(col => col.Key == Column).Value)) 
      { 
       int Row_Span = Data.Count(row => row.First(col => col.Key == Column).Value == Row.First(col => col.Key == Column).Value); 

       html += string.Format("<td rowspan='{0}'>{1}</td>", Row_Span, Row.First(col => col.Key == Column).Value); 
      } 
     } 
     else 
     { 
      html += string.Format("<td>{0}</td>", Row.First(col => col.Key == Column).Value); 
     } 
    } 

    html += "</tr>"; 
} 
+0

「System.Data.DataRowCollection」不包含關於「排序依據」的定義和沒有擴展方法「的OrderBy」接受類型「System.Data.DataRowCollection」的第一個參數可以發現(是你錯過了使用指令或程序集引用?)----這個錯誤是coming.i也嘗試添加System.Data.DataRowCollection.but顯示相同的錯誤。 –

+0

不,對不起,我只是假設它是某種類型的枚舉。如果您在將數據添加到「DataTable」之前按第一列對數據進行排序,則應該可以轉儲OrderBy。我會相應地更新我的答案 – Ben

+0

現在我得到了像ORDERBY一樣的錯誤和COUNT –