2015-03-25 25 views
0

这里是场景: 我从多个数据库检索数据并将其保存在列表和字典中。然后我必须在html文件的主体中创建一个表格,这个表格将作为一个自动生成的电子邮件,用于每个收件人的表中不同的数据集。我的问题是如何在c sharp文件的主循环中添加单元格到表格中。 这里的升C代码片段:将数据从c sharp移动到html表格到电子邮件

foreach(string str in strList) 
{ 
    columnOneData = someDict[Key]; 
    columnTwoData = str; 
    columnThreeData = otherDict[Key]; 
    columnFourData = thirdDict[Key]; 
} 
SmtpClient SmtpServer = new SmtpClient("some.server.com "); 
string Body = System.IO.File.ReadAllText(@"C:path\HTMLPage.htm"); 
Body=Body.Replace("firstColData",columnOneData); 
Body=Body.Replace("secondColData",columnTwoData); 
Body=Body.Replace("thirdColData",columnThreeData); 
Body=Body.Replace("fourthColData",columnFourData); 

message.Body = Body; 
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); 
SmtpServer.Send(message); 

这里就是我要寻找的想法,以保持填充表,而不是只结束了在HTML文件中

<table border="1" cellpadding="5" cellspacing="5" style="font-family:Calibri;color:Black;"> 
     <tr> 
      <th>Column One Header</th> 
      <th>Column Two Header</th> 
      <th>Column Three Header</th> 
      <th>Column Four Header</th> 
     </tr> 
     <tr> 
      <td>#firstColDataa#</td> 
      <td>#secondColData#</td> 
      <td>#thirdColData#</td> 
      <td>#fourthColData#</td> 
     </tr> 
    </table> 

因此,代码来自上次迭代的数据。

在此先感谢

+0

做什么? https://github.com/Antaris/RazorEngine效果很好。 – adt 2015-03-25 10:03:31

+0

为什么不使用现成的模板解决方案?例如,T4模板集成到Visual Studio和.NET中。只需创建一个运行时模板,您可以非常轻松地填写数据。 – Luaan 2015-03-25 10:03:38

+0

好吧,也许我可以提到我在视觉工作室是个不错的选择,我不太明白模板 – PrOjEkTeD 2015-03-25 10:12:32

回答

0

我想通了,这样做的快捷方式,放在#tablestring#变量中的HTML文件,然后在CS圈我只是串联起来每次迭代这样的字符串:

foreach(string str in strList) 
{ 
columnOneData = someDict[Key]; 
columnTwoData = str; 
columnThreeData = otherDict[Key]; 
columnFourData = thirdDict[Key]; 
tablestring += "<tr><td>"+columnOneData+"</td><td>"+columnTwoData....+"</td></tr>"; 
} 
SmtpClient SmtpServer = new SmtpClient("some.server.com "); 
string Body = System.IO.File.ReadAllText(@"C:path\HTMLPage.htm"); 
Body=Body.Replace("tablestring",tablestring); 

而在HTML文件时,要考虑使用一些模板引擎

<table border="1" cellpadding="5" cellspacing="5" style="font-family:Calibri;color:Black;"> 
    <tr> 
     <th>Column One Header</th> 
     <th>Column Two Header</th> 
     <th>Column Three Header</th> 
     <th>Column Four Header</th> 
    </tr> 
    #tablestring# 
</table> 
相关问题