Steps To Generate Excel Using EPPlus

My work is to create an Excel file, which will contain three fields: Address, Latitude and Longitude. The user will upload an Address in an Excel file and after reading the addresses from an Excel file, I have to retrieve Latitude and Longitude, using Bing MAP API and create another Excel file, which will contain Addresses along with the Latitude and Longitude of that address and download the new Excel file to the user's end.

Step 1

Create a MVC Application and add EPPLUS from NuGet package manager to your solution.

Step 2

Add the code to your .cshtml page.

  1. <h2>Upload File</h2>  
  2.   
  3. sing (Html.BeginForm("Upload""Home"null, FormMethod.Post, new { enctype = "multipart/form-data" }))  
  4.   {  
  5.       @Html.AntiForgeryToken()  
  6.       @Html.ValidationSummary()  
  7.   
  8.       <div class="form-group">  
  9.           <input type="file" id="dataFile" name="upload" />  
  10.       </div>  
  11.   
  12.       <div class="form-group">  
  13.           <input type="submit" value="Upload" class="btn btn-default" />  
  14.       </div>  
  15.   
  16.         
  17.   }  
Step 3

Now, add the code, mentioned below to your controller.
  1. [HttpPost]  
  2.         public ActionResult Upload(HttpPostedFileBase upload)  
  3.         {  
  4.             if (ModelState.IsValid)  
  5.             {  
  6.                 if (Path.GetExtension(upload.FileName) == ".xlsx")  
  7.                 {  
  8.                     ExcelPackage package = new ExcelPackage(upload.InputStream);  
  9.             //From This part we will read excel file  
  10.                     DataTable dt = ExcelPackageExtensions.ToDataTable(package);  
  11.   
  12.   
  13.                     DataTable dtExcel = new DataTable();  
  14.                     dtExcel.Columns.Add("Address"typeof(String));  
  15.                     dtExcel.Columns.Add("LAT"typeof(Double));  
  16.                     dtExcel.Columns.Add("LONG"typeof(Double));  
  17.   
  18.                     List<Coordinates> lstCor = new List<Coordinates>();  
  19.                     for (int i = 0; i < dt.Rows.Count; i++)  
  20.                     {  
  21.                         //Fill the new data Table to generate new excel file  
  22.                     }  
  23.             //This method will generate new excel and download the same  
  24.                     generateExcel(dtExcel);                   
  25.                 }  
  26.             }  
  27.             return View();  
  28.         }  
Step 4

“ExcelPackageExtensions.ToDataTable(package)” for this create a new class with the name ExcelPackageExtensions and create a static method with the name “ToDataTable()”. The code is mentioned below.
  1. public static class ExcelPackageExtensions  
  2.     {  
  3.         public static DataTable ToDataTable(this ExcelPackage package)  
  4.         {  
  5.             ExcelWorksheet workSheet = package.Workbook.Worksheets.First();  
  6.             DataTable table = new DataTable();  
  7.             foreach (var firstRowCell in workSheet.Cells[1, 1, 1, workSheet.Dimension.End.Column])  
  8.             {  
  9.                 table.Columns.Add(firstRowCell.Text);  
  10.             }  
  11.   
  12.             for (var rowNumber = 2; rowNumber <= workSheet.Dimension.End.Row; rowNumber++)  
  13.             {  
  14.                 var row = workSheet.Cells[rowNumber, 1, rowNumber, workSheet.Dimension.End.Column];  
  15.                 var newRow = table.NewRow();  
  16.                 foreach (var cell in row)  
  17.                 {  
  18.                     newRow[cell.Start.Column - 1] = cell.Text;  
  19.                 }  
  20.                 table.Rows.Add(newRow);  
  21.             }  
  22.             return table;  
  23.         }  
  24.     }  
Step 5

Now, add the code part to generate and download an Excel file.
  1. [NonAction]  
  2.         public void generateExcel(DataTable Dtvalue)  
  3.         {  
  4.             string excelpath = "";  
  5.             excelpath = @"C:\Excels\abc.xlsx";//Server.MapPath("~/UploadExcel/" + DateTime.UtcNow.Date.ToString() + ".xlsx");  
  6.             FileInfo finame = new FileInfo(excelpath);  
  7.             if (System.IO.File.Exists(excelpath))  
  8.             {  
  9.                 System.IO.File.Delete(excelpath);  
  10.             }  
  11.             if (!System.IO.File.Exists(excelpath))  
  12.             {  
  13.                 ExcelPackage excel = new ExcelPackage(finame);  
  14.                 var sheetcreate = excel.Workbook.Worksheets.Add(DateTime.UtcNow.Date.ToString());  
  15.                 if (Dtvalue.Rows.Count > 0)  
  16.                 {  
  17.                     for (int i = 0; i < Dtvalue.Rows.Count; )  
  18.                     {  
  19.                         sheetcreate.Cells[i + 1, 1].Value = Dtvalue.Rows[i][0].ToString();  
  20.                         sheetcreate.Cells[i + 1, 2].Value = Dtvalue.Rows[i][1].ToString();  
  21.                         sheetcreate.Cells[i + 1, 3].Value = Dtvalue.Rows[i][2].ToString();  
  22.                         i++;  
  23.                     }  
  24.                 }  
  25.                 //sheetcreate.Cells[1, 1, 1, 25].Style.Font.Bold = true;  
  26.                 //excel.Save();  
  27.   
  28.                 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");  
  29.                 //workSheet.Cells[1, 1].LoadFromCollection(data, true);  
  30.                 using (var memoryStream = new MemoryStream())  
  31.                 {  
  32.                     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";  
  33.                     Response.AddHeader("content-disposition""attachment;  filename=Contact.xlsx");  
  34.                     excel.SaveAs(memoryStream);  
  35.                     memoryStream.WriteTo(Response.OutputStream);  
  36.                     Response.Flush();  
  37.                     Response.End();  
  38.                 }  
  39.             }  
  40.   
  41.         }  
**Upload format of Excel is mentioned below.



**Download format of Excel is given below.

 

Ebook Download
View all
Learn
View all