Raushan Raj

Raushan Raj

  • NA
  • 23
  • 11.7k

How To Format Table In The Body Of Mail

Apr 15 2017 9:02 AM
I am using C# for my problem.
I have an Excel file which has many worksheets. From "Fisrt Sheet", I am looking for a character "x" which will be present in some cells of a specific column (occurence of "x" will be in one specific column only, in different cells of that column). I am looking for "x" and extracting the corresponding row's details in a generic list (with naming the headers of extracted field). Now, I have to send this generic list in "tabular format" in body of the mail via Outlook.
I am getting the output but not in proper format. My code is extracting the information but I can't see any table borders and also the table headers are missing under which the extracted data should be filled.
Table header information should be like this:
Column 1 - MemoName
Column 1 - Type
Column 1 - Ext
Column 1 - Seller
Column 1 - Warehouse
Also the header should be in black color and the extracted details in red.
Please help me with my problem
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using System.Data;  
  7. using Excel = Microsoft.Office.Interop.Excel;  
  8. using Outlook = Microsoft.Office.Interop.Outlook;  
  9.   
  10. namespace xlsm  
  11. {  
  12.     class New  
  13.     {  
  14.         static void Main(sting[] args)  
  15.         {  
  16.         sting st;   
  17.             long rCnt, cCnt;  
  18.             long rows = 0, columns = 0;  
  19.   
  20.         Excel.Application xlApp;  
  21.         Excel.Workbook xlWorkBook;  
  22.         Excel.Worksheet xlWorkSheet;  
  23.         Excel.Range rng;  
  24.   
  25.         xlApp = new Excel.Application();  
  26.         xlWorkBook = xlApp.Workbooks.Open(@"F:\Doc_Excel", 0, true, 5, """"true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t"falsefalse, 0, true, 1, 0);  
  27.         xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["First Sheet"];  
  28.   
  29.   
  30.         rng = xlWorkSheet.UsedRange;  
  31.         rows = rng.Rows.Count;  
  32.         columns = rng.Columns.Count;  
  33.   
  34.         List<Memo> lst = new List<Memo>();  
  35.   
  36.         for (rCnt = 1; rCnt < rows; rCnt++)  
  37.         {  
  38.             for (cCnt = 1; cCnt < columns; cCnt++)  
  39.             {  
  40.   
  41.                 if ((rng.Cells[rCnt, cCnt] as Excel.rng).Value2 != null)  
  42.                 {  
  43.                     st = (rng.Cells[rCnt, cCnt] as Excel.rng).Value2.Tosting();  
  44.                     if (st == "x")  
  45.                     {  
  46.                         Memo ms = new Memo();  
  47.   
  48.                         ms.MemoName = (rng.Cells[rCnt, 1] as Excel.rng).Value2.Tosting();  
  49.                         ms.Type = (rng.Cells[rCnt, 2] as Excel.rng).Value2.Tosting();  
  50.                         ms.Ext = (rng.Cells[rCnt, 3] as Excel.rng).Value2.Tosting();  
  51.                         ms.Seller = (rng.Cells[rCnt, 4] as Excel.rng).Value2.Tosting();  
  52.                         ms.Warehouse = (rng.Cells[rCnt, 5] as Excel.rng).Value2.Tosting();  
  53.                         lst.Add(ms);    
  54.   
  55.                     }  
  56.                 }  
  57.   
  58.             }  
  59.         }  
  60.   
  61.         try  
  62.         {  
  63.   
  64.             Outlook.Application oApp = new Outlook.Application();                 
  65.             Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);  
  66.             StringBuilder sb = new StringBuilder();  
  67.             sb.Append("<table>");  
  68.             foreach (var row in lst)  
  69.            {  
  70.             sb.Append("<tr>");  
  71.             sb.Append("<td>" + row.MemoName + "</td>");  
  72.             sb.Append("<td>" + row.Type + "</td>");  
  73.             sb.Append("<td>" + row.Ext + "</td>");  
  74.             sb.Append("<td>" + row.Seller + "</td>");  
  75.             sb.Append("<td>" + row.Warehouse + "</td>");  
  76.             sb.Append("</tr>");  
  77.            }  
  78.             sb.Append("</table>");  
  79.             oMsg.HTMLBody = sb.ToString();  
  80.             oMsg.Subject = "Memo contents as required.";                 
  81.             Outlook.Recipients oRecims = (Outlook.Recipients)oMsg.Recipients;                 
  82.             Outlook.Recipient oRecip = (Outlook.Recipient)oRecims.Add("[email protected]");  
  83.             oRecip.Resolve();                 
  84.             oMsg.Send();  
  85.   
  86.             oRecip = null;  
  87.             oRecims = null;  
  88.             oMsg = null;  
  89.             oApp = null;  
  90.         }  
  91.         catch (Exception ex)  
  92.         {  
  93.         }  
  94.         xlWorkBook.close(truenullnull);  
  95.         xlApp.Quit();  
  96.   
  97.         Marshal.ReleaseComObject(xlWorkSheet);  
  98.         Marshal.ReleaseComObject(xlWorkBook);  
  99.         Marshal.ReleaseComObject(xlApp);  
  100.     }          
  101.   
  102. }  
  103. public class Memo  
  104. {  
  105.     public string MemoName { getset; }  
  106.     public string Type { getset; }  
  107.     public string Ext { getset; }  
  108.     public string Seller { getset; }  
  109.     public string Warehouse { getset; }  
  110. }  
 

Answers (2)