title: How to get the data in excel file (using NPOI)
body: <p>I made this function to embed the data when sending an email. So I can call this function in the body of the email.</p>
  
  <pre><code>private static string getHTML(DataTable dt)
  {
      StringBuilder myBuilder = new StringBuilder();
  
      myBuilder.AppendLine("see attached for the updated list.");
      myBuilder.AppendLine("");
  
      myBuilder.Append("&lt;table border='1px' cellpadding='5' cellspacing='0' ");
      myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'&gt;");
  
      myBuilder.Append("&lt;tr align='left' valign='top'&gt;");
  
  
      foreach (DataColumn myColumn in dt.Columns)
      {
          myBuilder.Append("&lt;td align='left' valign='top'&gt;");
          myBuilder.Append(myColumn.ColumnName);
          myBuilder.Append("&lt;/td&gt;");
      }
  
      myBuilder.Append("&lt;/tr&gt;");
  
      int a = 1;
  
      foreach (DataRow myRow in dt.Rows)
      {
          myBuilder.Append("&lt;tr align='left' valign='top'&gt;");
  
          foreach (DataColumn myColumn in dt.Columns)
          {
              myBuilder.Append("&lt;td align='left' valign='top'&gt;");
              myBuilder.Append(myRow[myColumn.ColumnName].ToString());
              myBuilder.Append("&lt;/td&gt;");
          }
  
          myBuilder.Append("&lt;/tr&gt;");
          a++;
  
          if (a &gt; 4)
              break;
      }
  
      myBuilder.Append("&lt;/table&gt;");
  
      return myBuilder.ToString();
  }
  </code></pre>
  
  <p>But when I try to embed the excel file made with NPOI library (instead of Datatable), I can't find the appropriate function or anything to get the data in file.</p>
  
  <p>How can I embed the sheet1 data in the email?</p>
  
  <p>This is the code that I create sheet:</p>
  
  <pre><code>private void Email()
  {
      //get the data from database
      DataTable data = GetData();
      IWorkbook workbook;
      workbook = new HSSFWorkbook();
  
      ISheet sheet1 = workbook.CreateSheet("Sheet 1");
  
      // [...]
  }
  </code></pre>
  
  <p>And this is the code of sending an email:</p>
  
  <pre><code>private void email()
  {
      // [...]        
  
      using (var ms = new MemoryStream())
      {
          workbook.Write(ms);
          ms.Position = 0;
  
          using (MailMessage mm = new MailMessage())
          {
              mm.From = new MailAddress("abcd@gmail.com");
              mm.Bcc.Add("abcd@gmail.com");
  
              SmtpClient smtp = new SmtpClient();
              mm.Subject = "Task List";
  
              StringBuilder sb = new StringBuilder();
              mm.Body = getHTML(data);
              mm.Attachments.Add(new Attachment(ms, "Task.xls", "application/vnd.ms-excel"));
              mm.IsBodyHtml = true;
              smtp.Host = "smtp.gmail.com";
              smtp.EnableSsl = true;
              System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
              credentials.UserName = "abc@gmail.com";
              credentials.Password = "1234";
              smtp.UseDefaultCredentials = true;
              smtp.Credentials = credentials;
              smtp.Port = 587;
              smtp.Send(mm);
          }
      }
  }
  
  workbook.RemoveSheetAt(0);
  }
  </code></pre>