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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Data;
- using Excel = Microsoft.Office.Interop.Excel;
- using Outlook = Microsoft.Office.Interop.Outlook;
- namespace xlsm
- {
- class New
- {
- static void Main(sting[] args)
- {
- sting st;
- long rCnt, cCnt;
- long rows = 0, columns = 0;
- Excel.Application xlApp;
- Excel.Workbook xlWorkBook;
- Excel.Worksheet xlWorkSheet;
- Excel.Range rng;
- xlApp = new Excel.Application();
- xlWorkBook = xlApp.Workbooks.Open(@"F:\Doc_Excel", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
- xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["First Sheet"];
- rng = xlWorkSheet.UsedRange;
- rows = rng.Rows.Count;
- columns = rng.Columns.Count;
- List
lst = new List (); - for (rCnt = 1; rCnt < rows; rCnt++)
- {
- for (cCnt = 1; cCnt < columns; cCnt++)
- {
- if ((rng.Cells[rCnt, cCnt] as Excel.rng).Value2 != null)
- {
- st = (rng.Cells[rCnt, cCnt] as Excel.rng).Value2.Tosting();
- if (st == "x")
- {
- Memo ms = new Memo();
- ms.MemoName = (rng.Cells[rCnt, 1] as Excel.rng).Value2.Tosting();
- ms.Type = (rng.Cells[rCnt, 2] as Excel.rng).Value2.Tosting();
- ms.Ext = (rng.Cells[rCnt, 3] as Excel.rng).Value2.Tosting();
- ms.Seller = (rng.Cells[rCnt, 4] as Excel.rng).Value2.Tosting();
- ms.Warehouse = (rng.Cells[rCnt, 5] as Excel.rng).Value2.Tosting();
- lst.Add(ms);
- }
- }
- }
- }
- try
- {
- Outlook.Application oApp = new Outlook.Application();
- Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
- StringBuilder sb = new StringBuilder();
- sb.Append("
");
");- foreach (var row in lst)
- {
- sb.Append("
"); ");- sb.Append("
" + row.MemoName + " ");- sb.Append("
" + row.Type + " ");- sb.Append("
" + row.Ext + " ");- sb.Append("
" + row.Seller + " ");- sb.Append("
" + row.Warehouse + " ");- sb.Append("
- }
- sb.Append("
- oMsg.HTMLBody = sb.ToString();
- oMsg.Subject = "Memo contents as required.";
- Outlook.Recipients oRecims = (Outlook.Recipients)oMsg.Recipients;
- Outlook.Recipient oRecip = (Outlook.Recipient)oRecims.Add("[email protected]");
- oRecip.Resolve();
- oMsg.Send();
- oRecip = null;
- oRecims = null;
- oMsg = null;
- oApp = null;
- }
- catch (Exception ex)
- {
- }
- xlWorkBook.close(true, null, null);
- xlApp.Quit();
- Marshal.ReleaseComObject(xlWorkSheet);
- Marshal.ReleaseComObject(xlWorkBook);
- Marshal.ReleaseComObject(xlApp);
- }
- }
- public class Memo
- {
- public string MemoName { get; set; }
- public string Type { get; set; }
- public string Ext { get; set; }
- public string Seller { get; set; }
- public string Warehouse { get; set; }
- }
Arunprasath GajapathiPosted Apr 16, 2017, 5:07 AM
Manoj BhoirPosted Apr 16, 2017, 3:56 AM
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
foreach (var row in lst)
{
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
sb.Append("
}
sb.Append("