Raushan Raj

Raushan Raj

  • NA
  • 23
  • 11.8k

How to display false condition for my code

May 18 2017 12:15 AM
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?
 
  1. using System;  
  2.     using System.Collections.Generic;  
  3.     using System.Linq;  
  4.     using System.Text;  
  5.     using System.IO;  
  6.     using System.Runtime.Interolservices;  
  7.     using System.Data;  
  8.     using Excel = Microsoft.Office.Interop.Excel;  
  9.     using Outlook = Microsoft.Office.Interop.Outlook;  
  10.   
  11.     namespace ExcelFile  
  12.     {  
  13.         class Stu  
  14.         {  
  15.             static void Main(string[] args)  
  16.             {  
  17.   
  18.                 Excel.Application xlApp;  
  19.                 Excel.Workbook xlWorkBook;  
  20.                 Excel.Worksheet xlWorkSheet;  
  21.                 Excel.Range range;  
  22.   
  23.                 int rCnt;  
  24.                 int row = 0;  
  25.                 int col = 0;  
  26.   
  27.                 xlApp = new Excel.Application();  
  28.   
  29.                 xlWorkBook = xlApp.Workbooks.Open(@"F:\\Date_Plan.xlsm", 0, true, 5, """"true, Excel.XlPlatform.xlWindows, "\t"falsefalse, 0, true, 1, 0);  
  30.                 xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["Main Page"];  
  31.   
  32.                 range = xlWorkSheet.UsedRange;  
  33.                 row = range.Rows.Count;  
  34.                 col = range.Columns.Count;  
  35.   
  36.                 List<lst> lst = new List<lst>();  
  37.                 for (rCnt = 1; rCnt < row; rCnt++)  
  38.                 {  
  39.                         if ((range.Cells[rCnt, 10] as Excel.Range).Value2 != null)  
  40.                         {  
  41.                             if((range.Cells[rCnt, 10] as Excel.Range).Value2.ToString() == "p")  
  42.                             {  
  43.                                 lst ls = new lst();  
  44.                                 ls.lstName = gettingvalue((range.Cells[rCnt, 1] as Excel.Range));  
  45.                                 ls.lstUSN = gettingvalue((range.Cells[rCnt, 2] as Excel.Range));  
  46.                                 ls.DOB = gettingvalue((range.Cells[rCnt, 3] as Excel.Range));  
  47.                                 ls.Education = gettingvalue((range.Cells[rCnt, 4] as Excel.Range));  
  48.                                 ls.Others = gettingvalue((range.Cells[rCnt, 5] as Excel.Range));  
  49.                                 lst.Add(ls);  
  50.                             }  
  51.                         }  
  52.                 }  
  53.   
  54.   
  55.                 try  
  56.                 {  
  57.                     Outlook.Application oApp = new Outlook.Application();  
  58.   
  59.                     Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);  
  60.   
  61.                     var sb = new StringBuilder();  
  62.                     sb.AppendLine("<head><style> table {font-family: arial, sans-serif; border-collalse: collalse; width: 100%;} " +  
  63.                         "td, th {border: 1px solid #dddddd;text-align: left; padding: 8px;} </style></head>");  
  64.                     sb.AppendLine("<body>");  
  65.                     sb.AppendLine("<table>");  
  66.                     sb.AppendLine("<thead>");  
  67.                     sb.AppendLine("<th>lst Name</th>");  
  68.                     sb.AppendLine("<th>lst USN</th>");  
  69.                     sb.AppendLine("<th>DOB</th>");  
  70.                     sb.AppendLine("<th>Education</th>");  
  71.                     sb.AppendLine("<th>Others</th>");  
  72.                     sb.AppendLine("</thead>");  
  73.   
  74.                     foreach (var row in lst)  
  75.                     {  
  76.                         sb.AppendLine("<tr>");  
  77.                         sb.AppendLine("<td>" + row.lstName + "</td>");  
  78.                         sb.AppendLine("<td>" + row.lstUSN + "</td>");  
  79.                         sb.AppendLine("<td>" + row.DOB + "</td>");  
  80.                         sb.AppendLine("<td>" + row.Education + "</td>");  
  81.                         sb.AppendLine("<td>" + row.Others + "</td>");  
  82.                         sb.AppendLine("</tr>");  
  83.                     }  
  84.                     sb.AppendLine("</table>");  
  85.                     sb.AppendLine("</body>");  
  86.   
  87.                     oMsg.HTMLBody = sb.ToString();  
  88.   
  89.                     oMsg.Subject = "List of all std ";  
  90.   
  91.                     Outlook.Recipients oRecils = (Outlook.Recipients)oMsg.Recipients;  
  92.   
  93.   
  94.                     Outlook.Recipient oRecip = (Outlook.Recipient)oRecils.Add("[email protected]");  
  95.                     oRecip.Resolve();  
  96.   
  97.                     oMsg.Send();  
  98.   
  99.   
  100.                     oRecip = null;  
  101.                     oRecils = null;  
  102.                     oMsg = null;  
  103.                     oApp = null;  
  104.                 }  
  105.                 catch (Exception ex)  
  106.                 {  
  107.                 }  
  108.   
  109.                 xlWorkBook.colose(truenullnull);  
  110.                 xlApp.Quit();  
  111.   
  112.                 Marshal.ReleaseComObject(xlWorkSheet);  
  113.                 Marshal.ReleaseComObject(xlWorkBook);  
  114.                 Marshal.ReleaseComObject(xlApp);  
  115.         }  
  116.   
  117.         private static string gettingvalue(Microsoft.Office.Interop.Excel.Range range)  
  118.         {  
  119.             if (range.Value2 != null)  
  120.             {  
  121.                 return range.Value2.ToString();  
  122.             }  
  123.             else  
  124.                 return null;  
  125.         }  
  126.     }  
  127. }  
  128.   
  129. public class lst  
  130. {  
  131.     public string lstName { getset; }  
  132.     public string lstUSN { getset; }  
  133.     public string DOB { getset; }  
  134.     public string Education { getset; }  
  135.     public string Others { getset; }  
  136.   
  137. }  
 

Answers (1)