2014-07-10 30 views
2

我嘗試使用下面的代碼數據表作爲郵件在C#控制檯應用程序

public static string getHtml(DataTable dataSet) 
    { 
     try 
     { 
      string messageBody = "<font>The following are the records: </font><br><br>"; 

      if (dataSet.Rows.Count == 0) 
       return messageBody; 
      string htmlTableStart = "<table style=\"float:left; border-collapse:collapse; text-align:center;\" >"; 
      string htmlTableEnd = "</table>"; 
      string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">"; 
      string htmlHeaderRowEnd = "</tr>"; 
      string htmlTrStart = "<tr style =\"color:#555555;\">"; 
      string htmlTrEnd = "</tr>"; 
      string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">"; 
      string htmlTdEnd = "</td>"; 


      foreach (DataColumn dc in dataSet.Columns) 
      { 

       messageBody += htmlTableStart; 
       messageBody += htmlHeaderRowStart; 
       messageBody += htmlTdStart + dc + htmlTdEnd; 
       messageBody += htmlHeaderRowEnd; 

       foreach (DataRow Row in dataSet.Rows) 
       { 
        messageBody = messageBody + htmlTrStart; 
        messageBody = messageBody + htmlTdStart + Row["" + dc + ""] + htmlTdEnd; 
        messageBody = messageBody + htmlTrEnd; 
       } 
      } 
      messageBody = messageBody + htmlTableEnd; 


      return messageBody; 
     } 
     catch (Exception ex) 
     { 
      return null; 
     } 
    } 

enter image description here

我想要的方式, 所有不同的表應該進來使創建從我的數據集的html水平。我試圖浮動:左但是當我使用這個網站在郵件被髮送,浮動左不工作

能否東西代碼進行更改,以便表進來hozizontally作爲enter image description here

+0

是否要爲數據庫中的每一行添加新表? – sylwester

+0

試着給表格一個「寬度」。 'style = \「... width:600px ... \」' –

+1

請使用StringBuilder! – DaniCE

回答

1

我完成了我的答案。這裏是將數據集轉換爲html的解決方案

public static string DataTableToHTML(DataTable dataSet) 
     { 
      mLogger.Error("DataTableToHTML -------> Start"); 
      try 
      { 
       string messageBody = "<font>Following corporate actions are not updated in Security Master: </font><br><br>"; 

       if (dataSet.Rows.Count == 0) 
        return messageBody; 
       string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >"; 
       string htmlTableEnd = "</table>"; 
       string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">"; 
       string htmlHeaderRowEnd = "</tr>"; 
       string htmlTrStart = "<tr style =\"color:#555555;\">"; 
       string htmlTrEnd = "</tr>"; 
       string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">"; 
       string htmlTdEnd = "</td>"; 

       messageBody += htmlTableStart; 
       messageBody += htmlHeaderRowStart; 
       foreach (DataColumn dc in dataSet.Columns) 
       { 
        messageBody += htmlTdStart + dc + htmlTdEnd; 

       } messageBody += htmlHeaderRowEnd; 

       foreach (DataRow dr in dataSet.Rows) 
       { 
        messageBody = messageBody + htmlTrStart; 
        foreach (DataColumn dc in dataSet.Columns) 
        { 

         messageBody = messageBody + htmlTdStart + dr["" + dc + ""] + htmlTdEnd; 
        } 
        messageBody = messageBody + htmlTrEnd; 
       } 

       messageBody = messageBody + htmlTableEnd; 

       mLogger.Error("DataTableToHTML -------> End"); 
       return messageBody; 
      } 
      catch (Exception ex) 
      { 
       mLogger.Error("Exception in DatatableToHTML" + ex); 
       return null; 
      } 
     }