This program is looking for "p" in 10th column of Excel Sheet. If "p" is found, then required information is extracted and sent to the desired email id. What to do if "p" is not present in the worksheet? It should display message that "p" is not present in the worksheet. How to do this?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Runtime.Interolservices;
- using System.Data;
- using Excel = Microsoft.Office.Interop.Excel;
- using Outlook = Microsoft.Office.Interop.Outlook;
-
- namespace ExcelFile
- {
- class Stu
- {
- static void Main(string[] args)
- {
-
- Excel.Application xlApp;
- Excel.Workbook xlWorkBook;
- Excel.Worksheet xlWorkSheet;
- Excel.Range range;
-
- int rCnt;
- int row = 0;
- int col = 0;
-
- xlApp = new Excel.Application();
-
- xlWorkBook = xlApp.Workbooks.Open(@"F:\\Date_Plan.xlsm", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
- xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["Main Page"];
-
- range = xlWorkSheet.UsedRange;
- row = range.Rows.Count;
- col = range.Columns.Count;
-
- List<lst> lst = new List<lst>();
- for (rCnt = 1; rCnt < row; rCnt++)
- {
- if ((range.Cells[rCnt, 10] as Excel.Range).Value2 != null)
- {
- if((range.Cells[rCnt, 10] as Excel.Range).Value2.ToString() == "p")
- {
- lst ls = new lst();
- ls.lstName = gettingvalue((range.Cells[rCnt, 1] as Excel.Range));
- ls.lstUSN = gettingvalue((range.Cells[rCnt, 2] as Excel.Range));
- ls.DOB = gettingvalue((range.Cells[rCnt, 3] as Excel.Range));
- ls.Education = gettingvalue((range.Cells[rCnt, 4] as Excel.Range));
- ls.Others = gettingvalue((range.Cells[rCnt, 5] as Excel.Range));
- lst.Add(ls);
- }
- }
- }
-
-
- try
- {
- Outlook.Application oApp = new Outlook.Application();
-
- Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
-
- var sb = new StringBuilder();
- sb.AppendLine("<head><style> table {font-family: arial, sans-serif; border-collalse: collalse; width: 100%;} " +
- "td, th {border: 1px solid #dddddd;text-align: left; padding: 8px;} </style></head>");
- sb.AppendLine("<body>");
- sb.AppendLine("<table>");
- sb.AppendLine("<thead>");
- sb.AppendLine("<th>lst Name</th>");
- sb.AppendLine("<th>lst USN</th>");
- sb.AppendLine("<th>DOB</th>");
- sb.AppendLine("<th>Education</th>");
- sb.AppendLine("<th>Others</th>");
- sb.AppendLine("</thead>");
-
- foreach (var row in lst)
- {
- sb.AppendLine("<tr>");
- sb.AppendLine("<td>" + row.lstName + "</td>");
- sb.AppendLine("<td>" + row.lstUSN + "</td>");
- sb.AppendLine("<td>" + row.DOB + "</td>");
- sb.AppendLine("<td>" + row.Education + "</td>");
- sb.AppendLine("<td>" + row.Others + "</td>");
- sb.AppendLine("</tr>");
- }
- sb.AppendLine("</table>");
- sb.AppendLine("</body>");
-
- oMsg.HTMLBody = sb.ToString();
-
- oMsg.Subject = "List of all std ";
-
- Outlook.Recipients oRecils = (Outlook.Recipients)oMsg.Recipients;
-
-
- Outlook.Recipient oRecip = (Outlook.Recipient)oRecils.Add("[email protected]");
- oRecip.Resolve();
-
- oMsg.Send();
-
-
- oRecip = null;
- oRecils = null;
- oMsg = null;
- oApp = null;
- }
- catch (Exception ex)
- {
- }
-
- xlWorkBook.colose(true, null, null);
- xlApp.Quit();
-
- Marshal.ReleaseComObject(xlWorkSheet);
- Marshal.ReleaseComObject(xlWorkBook);
- Marshal.ReleaseComObject(xlApp);
- }
-
- private static string gettingvalue(Microsoft.Office.Interop.Excel.Range range)
- {
- if (range.Value2 != null)
- {
- return range.Value2.ToString();
- }
- else
- return null;
- }
- }
- }
-
- public class lst
- {
- public string lstName { get; set; }
- public string lstUSN { get; set; }
- public string DOB { get; set; }
- public string Education { get; set; }
- public string Others { get; set; }
-
- }