Introduction
In this article, we will learn how can we send mail with an attachment. In this article I am going to send excel files through SendGrid and save the excel file in folder let’s see step by step process.
Step 1
First, create a simple ASP.NET project. Here, I am not showing how to create ASP.NET project. I am creating my project with the name ExcelAttechment
Step 2
In this step, we need to install Microsoft.Office.Interop.Excel package through NuGet.
Step 3
In this step, we can add an entity ADO.NET entity Data Model in our project.
I am creating a table with the name Student.

Afterwards, I will fetch the data from the table with the help of an entity. I am using buttonClick event to get the data.
Let’s see the code on how can we get the data from the database. In the code given below, I am getting the data in list format after which I am converting the list into DataTable format.
- using ExcelAttechment.Models;
- using SendGrid;
- using SendGrid.Helpers.Mail;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace ExcelAttechment
- {
- public partial class _Default : Page
- {
- string savelocation = @"D:\Excel\";
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- AtulTestEntities1 at = new Models.AtulTestEntities1();
- // var data = new DALExcel().GetDataForExcel();
- var data = at.Students.ToList();
- DataTable dt = ToDataTable(data.ToList());
- excelimport(dt, savelocation).Wait();
- }
- public DataTable ToDataTable<T>(List<T> items)
- {
- DataTable dataTable = new DataTable(typeof(T).Name);
- //Get all the properties
- PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
- foreach (PropertyInfo prop in Props)
- {
- //Setting column names as Property names
- dataTable.Columns.Add(prop.Name);
- }
- foreach (T item in items)
- {
- var values = new object[Props.Length];
- for (int i = 0; i < Props.Length; i++)
- {
- //inserting property values to datatable rows
- values[i] = Props[i].GetValue(item, null);
- }
- dataTable.Rows.Add(values);
- }
- //put a breakpoint here and check datatable
- return dataTable;
- }
- public async Task excelimport(DataTable Tbl, string savelocation)
- {
- simpletest st = new simpletest();
- string name = Tbl.TableName;
- int rowcount = Tbl.Rows.Count + 1;
- Microsoft.Office.Interop.Excel.Range excelCellrange;
- Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
- excelApp.Workbooks.Add();
- // single worksheet
- Microsoft.Office.Interop.Excel._Worksheet workSheet = excelApp.ActiveSheet;
- workSheet.Name = Tbl.TableName;
- //column headings
- for (int i = 0; i < Tbl.Columns.Count; i++)
- {
- workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName.ToUpper();
- }
- // rows
- for (int i = 0; i < Tbl.Rows.Count; i++)
- {
- // to do: format datetime values before printing
- for (int j = 0; j < Tbl.Columns.Count; j++)
- {
- workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
- }
- }
- excelCellrange = workSheet.Range[workSheet.Cells[1, 1], workSheet.Cells[rowcount, Tbl.Columns.Count]];
- excelCellrange.EntireColumn.AutoFit();
- Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
- border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
- workSheet.get_Range("A1", "G1").Font.Bold = true;
- border.Weight = 2d;
- if (savelocation != null && savelocation != "savelocation")
- {
- try
- {
- workSheet.SaveAs(savelocation + workSheet.Name + ".xls");
- excelApp.Quit();
- string email = "[email protected]";
- string filepath = savelocation + name + ".xls";
- //send mai with attachment.
- await st.sendemail(email, filepath);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else // no filepath is given
- {
- excelApp.Visible = true;
- }
- }
- }
- }



Shankar KarmalkarPosted Feb 20, 2021, 10:58 PM
Great work done
Former memberPosted Mar 27, 2017, 4:56 AM
Thanks for your sharing
Satyaprakash SamantarayPosted Mar 23, 2017, 11:15 AM
Thanks for sharing