Uploading Multiple Records Via Excel Upload in Database Using EntityFramework 6.0 in ASP.Net MVC4

This article explains how to insert multiple records into a database using an Excel upload. When working on a project there was a requirement for me to upload an Excel file, let's say with the number of records up to 50000 per sheet. But while uploading the Excel file there were certain validations that needs to be done and if any of the records fail then the error message needs to be displayed for that respective row of the Excel on the page and none of the records needs to be saved inside the database table.

Validations could be in any sense, for example:

  1. The age of all employees needs to be >= 18 and <= 60
  2. The age column should not have a non-numeric value
  3. There were certain columns that were mandatory, if the value is not filled in that column of the Excel then the user should be notified of the error. Like name of the Employee should not be null.
  4. Checking thar a date value is in the format of DD/MM/YYYY
  5. and so on

When uploading this Excel file, if the preceding validations are successfully satisfied then the data needs to be saved in the database against a specific batch id that is basically a unique number to identify the records inserted within that specific batch and when they all were inserted. Also a special attention needs to be provided on the Excel column headings that we provided to the user for data entry purposes. Let's say there are 10 columns in the Excel and if the user changes the order of the Excel column heading or if they delete some column or changes the name of the column, this validation also needs to be performed against the uploaded file. For performing this validation we have made use of an ExcelStructure Table that will have Excel fields/columns stored with their respective sizes, types, parameters and whether these fields/columns are mandatory or not. The following is the structure of the ExcelStructure Table. Remember we are using EntityFramework with the Code First Approach. But just for demo/looking purposes our SQL table will have the following query. 

  1. CREATE TABLE [dbo].[ExcelStructure]  
  2. (  
  3.     [Row_ID]          INT            IDENTITY (1, 1) NOT NULL,  
  4.     [File_ID]         INT            NOT NULL,  
  5.     [File_Name]       NVARCHAR (MAXNULL,  
  6.     [Field_Name]      NVARCHAR (MAXNULL,  
  7.     [Field_Data]      NVARCHAR (MAXNULL,  
  8.     [Active_Flag]     INT            NOT NULL,  
  9.     [Row_No]          INT            NOT NULL,  
  10.     [Field_Delimeter] NVARCHAR (MAXNULL,  
  11.     CONSTRAINT [PK_dbo.ExcelStructure] PRIMARY KEY CLUSTERED ([Row_ID] ASC)  
  12. );  

Now let's move to the code. Here the technology that we are using is ASP.NET MVC4 and EntityFramework 6.0. I'm naming my solution name ImportFromExcel that will have two projects, one would be of type Class Library with the name ImportFromExcel.Domain and the other would be our MVC4 project with the name ImportFromExcel.WebUI with basic as its type. Our ImportFromExcel.Domain project will have the entire model and DbContext classes we are following here the separation of concerns between model and WebApplication. Install the EntityFramework Package inside the ImportFromExcel.Domain and ImportFromExcel.WebUI Project using the Package Manager Console by typing the command as:

Install-package EntityFramework

Note: Make sure you install it in both the projects otherwise a version issue might occur.

We need to add the reference to System.ComponentModel.DataAnnotations namespace. We have added creation folders to our Domain class library project and these folders contain several classes and interface that we'll require to query our database or manipulate the database. Here is the hierarchy of the ImportFromExcel.Domain project application:

Solution Explorer

Here are the definitions of the files.

Employee.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.ComponentModel.DataAnnotations.Schema;  
  5. namespace ImportFromExcel.Domain.Entities  
  6. {  
  7.     [Table("Employee")]  
  8.     public class Employee  
  9.     {  
  10.         [Key]  
  11.         public int EmployeeID { getset; }//mvc convention by default it will make it as primary key with autogenerated feature turned on  
  12.         [Required(ErrorMessage = "Name is Required")]  
  13.         public string Name { getset; }  
  14.         [Required(ErrorMessage = "Address is Required")]  
  15.         public string Address { getset; }  
  16.         public string DOB { getset; }  
  17.         public int Age { getset; }  
  18.         public string Email { getset; }  
  19.     }  
  20. }  

ExcelStructure.cs

  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3. using System.ComponentModel.DataAnnotations.Schema;  
  4. namespace ImportFromExcel.Domain.Entities  
  5. {  
  6.     [Table("ExcelStructure")]  
  7.     public class ExcelStructure  
  8.     {  
  9.         [Key]  
  10.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
  11.         public int Row_ID { getset; }  
  12.         public int File_ID { getset; }  
  13.         public string File_Name { getset; }  
  14.         public string Field_Name { getset; }  
  15.         public string Field_Data { getset; }  
  16.         public int Active_Flag { getset; }  
  17.         public int Row_No { getset; }  
  18.         public string Field_Delimeter { getset; }  
  19.     }  
  20. } 

In order to reduce the dependency between higher end modules and lower end modules we implemented an interface that will help us to do loose coupling between the various components.

IEmployeeRepository.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using ImportFromExcel.Domain.Entities;  
  6. namespace ImportFromExcel.Domain.Abstract  
  7. {  
  8.     public interface IEmployeeRepository  
  9.     {  
  10.         IQueryable<Employee> Employee { get; }  
  11.         IQueryable<ExcelStructure> ExcelStructure { get; }  
  12.         void SaveEmployee(IList<Employee> lstEmployees);  
  13.     }  
  14. }  

EFDBContext.cs: This is our DbContext class

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using ImportFromExcel.Domain.Entities;  
  6. namespace ImportFromExcel.Domain.Abstract  
  7. {  
  8.     public interface IEmployeeRepository  
  9.     {  
  10.         IQueryable<Employee> Employee { get; }  
  11.         IQueryable<ExcelStructure> ExcelStructure { get; }  
  12.         void SaveEmployee(IList<Employee> lstEmployees);  
  13.     }  
  14. }

EmployeeRepository.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using ImportFromExcel.Domain.Abstract;  
  5. namespace ImportFromExcel.Domain.Concrete  
  6. {  
  7.     public class EmployeeRepository : IEmployeeRepository  
  8.     {  
  9.         EFDbContext context = new EFDbContext();  
  10.         public IQueryable<Entities.Employee> Emp  
  11.         {  
  12.             get { return context.Employee; }  
  13.         }  
  14.         public IQueryable<Entities.ExcelStructure> ExcelStructure  
  15.         {  
  16.             get { return context.ExcelStructure; }  
  17.         }  
  18.         public void SaveEmployee(IList<Entities.Employee> lstEmployees)  
  19.         {  
  20.             try  
  21.             {  
  22.                 context.Employee.AddRange(lstEmployees);  
  23.                 context.SaveChanges();  
  24.             }  
  25.             catch (Exception ex)  
  26.             {  
  27.                 throw ex;  
  28.             }  
  29.         }  
  30.     }  
  31. } 

Inside the Concrete folder we have a class named ExcelStructureInitializer.cs that basically contains the code for the records that needs to be stored in the Database ExcelStructure Table when we run the application for the first time or even when the model changes or the database is dropped and recreated by the entityFramework. This class inherits from DropCreateDatabaseIfModelChanges class in order to maintain the synchronization between the Model and the EntityFramework Database. The following is the definition of the class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using ImportFromExcel.Domain.Entities;  
  7. using System.Data.Entity;  
  8. namespace ImportFromExcel.Domain.Concrete  
  9. {  
  10.     public class ExcelStructureInitializer : DropCreateDatabaseIfModelChanges<EFDbContext>  
  11.     {  
  12.         protected override void Seed(EFDbContext context)  
  13.         {  
  14.             //base.Seed(context);  
  15.             var excelStructure = new List<ExcelStructure>()  
  16.             {  
  17.                 new ExcelStructure(){ File_ID=1, Field_Name="Name", Field_Data="Name|Address|DOB|Age|Email", Active_Flag=1, Row_No=1, File_Name="EmployeeExcel", Field_Delimeter="|"},  
  18.                 new ExcelStructure(){ File_ID=1, Field_Name="DataType", Field_Data="S|S|D|I|S", Active_Flag=1, Row_No=2, File_Name="EmployeeExcel", Field_Delimeter="|"},  
  19.                 new ExcelStructure(){ File_ID=1, Field_Name="Size", Field_Data="50|100|20|10|100", Active_Flag=1, Row_No=3, File_Name="EmployeeExcel", Field_Delimeter="|"},  
  20.                 new ExcelStructure(){ File_ID=1, Field_Name="Mandatory", Field_Data="Y|Y|N|N|N", Active_Flag=1, Row_No=4, File_Name="EmployeeExcel", Field_Delimeter="|"},  
  21.                 new ExcelStructure(){ File_ID=1, Field_Name="Param", Field_Data="@name|@add|@dob|@age|@email", Active_Flag=1, Row_No=5, File_Name="EmployeeExcel", Field_Delimeter="|"},  
  22.             };  
  23.             context.ExcelStructure.AddRange(excelStructure);  
  24.             context.SaveChanges();  
  25.         }  
  26.     }  
  27. }  

The one highlighted in the yellow color in the code above will be the Excel Column heading with their Type, size, and mandatory or not. And the one highlighted in the light blue color are the name through that we can refer to these fields in the code file. Every value is separated using the “|” character.

All these records will be added to your Excel structure table when you run the application. Here in this table we specified the Excel column hierarchy and the maximum value that the column can contain, also whether they are mandatory or not and their datatype. For Datatype here I'm using the convention of S for String, I for integers, D for DateTime, DO for Double and so on but you can follow your own conventions.

And we are setting the reference of this class inside the Global.asax file that is within the ImportFromExcel.WebUI application. Here is the line that we set up in the Global.asax file.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.Entity;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Http;  
  7. using System.Web.Mvc;  
  8. using System.Web.Optimization;  
  9. using System.Web.Routing;  
  10. using ImportFromExcel.Domain.Concrete;  
  11. using ImportFromExcel.WebUI.Infrastructure;  
  12. namespace ImportFromExcel.WebUI  
  13. {  
  14.     // Note: For instructions on enabling IIS6 or IIS7 classic mode,  
  15.     // visit http://go.microsoft.com/?LinkId=9394801  
  16.     public class MvcApplication : System.Web.HttpApplication  
  17.     {  
  18.         protected void Application_Start()  
  19.         {  
  20.             AreaRegistration.RegisterAllAreas();  
  21.             Database.SetInitializer<EFDbContext>(new ImportFromExcel.Domain.Concrete.ExcelStructureInitializer());  
  22.             WebApiConfig.Register(GlobalConfiguration.Configuration);  
  23.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  24.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  25.             ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());  
  26.             BundleConfig.RegisterBundles(BundleTable.Bundles);  
  27.         }  
  28.     }  
  29. } 

For the code of the preceding files kindly download the article.

Now we'll be moving towards creating our front end, in other words the ImportFromExcel.WebUI application where we'll create a view and controllers that will communicate with the preceding domain model that we created above.

Note:

  1. Here for Dependency Injection we used Ninject. Also for the full explanation of Dependency Injection you can refer to the following URL:

    ASP.NET MVC 4 Dependency Injection

    We injected the Ninject controller using the Global.asax file. The one marked with light blue as background in the Global.asax file.
     
  2. For Excel reading and writing I'm using Bytescout.Spreadsheet.dll
  3. Also add the reference of the ImportFromExcel.Domain.dll to the ImportFromExcel.WebUI project. So that we can refer to the classes.

Here is the hierarchy of the ImportFromExcel.WebUI

ImportFromExcel

Add a connection string to the Web.Config file of the ImportFromExcel.WebUI like the following:

  1. <connectionStrings>  
  2.     <!--<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ImportFromExcel.WebUI-20140408112057;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ImportFromExcel.WebUI-  20140408112057.mdf" />-->  
  3.     <add name="EFDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=(localDb)\v11.0;Initial Catalog=MemberUpload;Integrated Security=true;"/>  
  4. </connectionStrings>

Now to create a helper file that will allow us to validate and upload the Excel content. We'll specify our validation rules inside this file. Create a class with the name of APIHelper.cs inside the AppHelper folder of your WebUI project.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Reflection;  
  6. using System.Web;  
  7. using Bytescout.Spreadsheet;  
  8. using ImportFromExcel.Domain.Abstract;  
  9. using ImportFromExcel.Domain.Entities;  
  10. namespace ImportFromExcel.WebUI.AppHelper  
  11. {  
  12.     public class APIHelper  
  13.     {  
  14.         private IEmployeeRepository repository;  
  15.         public APIHelper(IEmployeeRepository repository)  
  16.         {  
  17.             this.repository = repository;  
  18.         }  
  19.         /// <summary>  
  20.         /// Read the Excel File  
  21.         /// </summary>  
  22.         /// <param name="filePath">File Path</param>  
  23.         /// <param name="isHDR">Header File</param>  
  24.         /// <returns>returns DataSet</returns>  
  25.         public DataSet ReadExcelFile(string filePath, out string msg, string isHDR = "Yes")  
  26.         {  
  27.             string details = string.Empty;  
  28.             //retrieve the Excel structure for a particular File. in our case EmployeeStructure was the file name whose data needs to copied  
  29.             List<ExcelStructure> lstEntityTable = repository.ExcelStructure.Where(  
  30.                          x => x.File_Name.Equals("EmployeeExcel", StringComparison.InvariantCultureIgnoreCase)).  
  31.                 OrderBy(x => x.Row_No).ToList();  
  32.             List<string> lstFieldType = new List<string>();  
  33.             //getting the type of all field  
  34.             lstFieldType.AddRange(lstEntityTable[1].Field_Data.ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));  
  35.             //Get the ExcelStructure Stored in Database  
  36.             DataTable dt = CreateDataTableFromList(lstEntityTable);  
  37.             //Get the Records Stored in ExcelFile Uploaded by user  
  38.             DataSet ds = GetDataFromMultipleSheets(filePath, lstFieldType);  
  39.             string fileName = string.Empty;  
  40.             //perform validation on every dataTable inside DataSet  
  41.             for (byte i = 0; i < ds.Tables.Count; i++)  
  42.             {  
  43.                 if (ds.Tables[i].Rows.Count > 0)  
  44.                 {  
  45.                     details = ValidateExcelHeadings(dt, ds.Tables[i]);  
  46.                     ds.DataSetName = filePath.Substring(filePath.LastIndexOf("\\") + 1);  
  47.                 }  
  48.             }  
  49.             msg = details;  
  50.             return ds;  
  51.         }  
  52.         /// <summary>  
  53.         /// This function is used for copying the List Excel Structure  Data  to DataTable  
  54.         /// </summary>  
  55.         /// <param name="lstExcel">List<ExcelStructure></param>  
  56.         /// <returns>returns DataTable</returns>  
  57.         private DataTable CreateDataTableFromList(List<ExcelStructure> lstExcel)  
  58.         {  
  59.             DataTable dt = new DataTable();  
  60.             PropertyInfo[] props = typeof(ExcelStructure).GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  61.             foreach (PropertyInfo prop in props)  
  62.             {  
  63.                 if (!prop.Name.Equals("Row_ID"))  
  64.                     dt.Columns.Add(prop.Name);  
  65.             }  
  66.             for (int i = 0; i < lstExcel.Count; i++)  
  67.             {  
  68.                 DataRow dr = dt.NewRow();  
  69.                 dr[0] = lstExcel[i].File_ID;  
  70.                 dr[1] = lstExcel[i].File_Name;  
  71.                 dr[2] = lstExcel[i].Field_Name;  
  72.                 dr[3] = lstExcel[i].Field_Data;  
  73.                 dr[4] = lstExcel[i].Active_Flag;  
  74.                 dr[5] = lstExcel[i].Row_No;  
  75.                 dr[6] = lstExcel[i].Field_Delimeter;  
  76.                 dt.Rows.Add(dr);  
  77.             }  
  78.             return dt;  
  79.         }  
  80.         /// <summary>  
  81.         /// This function is used for reading Data From Multiple sheet and adding those worksheet data as  
  82.         /// DataTable inside a DataSet  
  83.         /// </summary>  
  84.         /// <param name="filePath">FilePath</param>  
  85.         /// <param name="lstDateType">DataType of the Fields</param>  
  86.         /// <returns>returns DataSet</returns>  
  87.         private DataSet GetDataFromMultipleSheets(string filePath, List<string> lstDateType)  
  88.         {  
  89.             DataSet ds = new DataSet();  
  90.             Spreadsheet document = new Spreadsheet(filePath);  
  91.             int totalColumn = 0;  
  92.             try  
  93.             {  
  94.                 //iterate over the workbook worksheets and add those to the DataSet Tables.  
  95.                 for (byte i = 0; i < document.Workbook.Worksheets.Count; i++)  
  96.                 {  
  97.                     Worksheet sheet = document.Workbook.Worksheets[i];  
  98.                     totalColumn = sheet.NotEmptyColumnMax + 1;  
  99.                     //set the DataTable Name to the SheetName specified  
  100.                     DataTable dtTable = ds.Tables.Add(sheet.Name);  
  101.                     //create the dataTable with the specified column of uploaded Excel  
  102.                     for (int col = 0; col < totalColumn; col++)  
  103.                     {  
  104.                         try  
  105.                         {  
  106.                             //Add the columns to the DataTable  
  107.                             dtTable.Columns.Add(sheet.Cell(0, col).Value.ToString().Trim());  
  108.                         }  
  109.                         catch (Exception ex)  
  110.                         {  
  111.                             throw ex;  
  112.                         }  
  113.                     }  
  114.                     //iterate over the worksheet records until the last used rows  
  115.                     //here we are iterating from row 1 because we are assuming row 0 is an header row  
  116.                     for (int row = 1; row <= sheet.UsedRangeRowMax; row++)  
  117.                     {  
  118.                         object[] data = new object[totalColumn];  
  119.                         for (int column = 0; column < totalColumn; column++)  
  120.                         {  
  121.                             try  
  122.                             {  
  123.                                 //check whether the value is of DateTime  
  124.                                 if (lstDateType[column].Equals("D"))  
  125.                                 {  
  126.                                     try  
  127.                                     {  
  128.                                         data[column] = sheet.Cell(row, column).ValueAsDateTime.ToShortDateString().Trim();  
  129.                                     }  
  130.                                     catch (Exception)  
  131.                                     {  
  132.                                         data[column] = sheet.Cell(row, column).Value.ToString().Trim();  
  133.                                     }  
  134.                                 }  
  135.                                 else  
  136.                                 {  
  137.                                     data[column] = sheet.Cell(row, column).Value.ToString().Trim();  
  138.                                 }  
  139.                             }  
  140.                             catch (Exception ex)  
  141.                             {  
  142.                                 data[column] = sheet.Cell(row, column).Value;  
  143.                             }  
  144.                         }  
  145.                         dtTable.Rows.Add(data);  
  146.                     }  
  147.                 }  
  148.                 document.Close();  
  149.             }  
  150.             catch (Exception ex)  
  151.             {  
  152.                 throw ex;  
  153.             }  
  154.             return ds;  
  155.         }  
  156.         /// <summary>  
  157.         /// This function is used for validating the Excel headings against those stored in the Database.  
  158.         /// </summary>  
  159.         /// <param name="dtUploadedExcel">Uploaded Excel Converted as DataTable</param>  
  160.         /// <param name="dtExcelStructure">Excel Structure Converted as DataTable</param>  
  161.         ///  /// <returns>returns string message</returns>  
  162.         private string ValidateExcelHeadings(DataTable dtExcelStructure, DataTable dtUploadedExcel)  
  163.         {  
  164.             string error = "";  
  165.             int number = 0;  
  166.             try  
  167.             {  
  168.                 //read the TableHeadings from the ExcelStructure  
  169.                 string strFieldValue = (string)dtExcelStructure.AsEnumerable().Where(x => x["Field_Name"].Equals("Name")).  
  170.                         Select(x => x["Field_Data"]).First();  
  171.                 string[] strFieldHeadings = strFieldValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);  
  172.                 string[] strFieldType = ((string)dtExcelStructure.AsEnumerable().Where(x => x["Field_Name"].Equals("DataType")).  
  173.                     Select(x => x["Field_Data"]).First()).Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);  
  174.                 string[] strFieldSize = ((string)dtExcelStructure.AsEnumerable().Where(x => x["Field_Name"].Equals("Size")).  
  175.                     Select(x => x["Field_Data"]).First()).Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);  
  176.                 string[] strFieldParam = ((string)dtExcelStructure.AsEnumerable().Where(x => x["Field_Name"].Equals("Param")).  
  177.                     Select(x => x["Field_Data"]).First()).Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);  
  178.                 string[] strFieldMandatory = ((string)dtExcelStructure.AsEnumerable().Where(x => x["Field_Name"].Equals("Mandatory")).  
  179.                     Select(x => x["Field_Data"]).First()).Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);  
  180.                 //Checking the upload Excel column count with that of the ExcelStructure Table Column Count  
  181.                 if (strFieldHeadings.Length == dtUploadedExcel.Columns.Count)  
  182.                 {  
  183.                     for (int i = 0; i < strFieldHeadings.Length; i++)  
  184.                     {  
  185.                         //Validate Excel Heading  
  186.                         if (strFieldHeadings[i].ToLower() != dtUploadedExcel.Columns[i].ToString().ToLower())  
  187.                         {  
  188.                             error += dtUploadedExcel.Columns[i].ToString() + " Mismach Column, it has to be " + strFieldHeadings[i] + "\\n";  
  189.                         }  
  190.                     }  
  191.                     if (error != "")  
  192.                         return error;  
  193.                 }  
  194.                 else  
  195.                 {  
  196.                     error = "Mismach Count of Excel Column";  
  197.                     return error;  
  198.                 }  
  199.                 if (error == "")  
  200.                 {  
  201.                     //add an extra column named Error inside the DataTable which contains the excelUploadedData  
  202.                     dtUploadedExcel.Columns.Add("Error");  
  203.                     for (int i = 0; i < dtUploadedExcel.Rows.Count; i++)  
  204.                     {  
  205.                         error = "";  
  206.                         for (int j = 0; j < strFieldHeadings.Length; j++)  
  207.                         {  
  208.                             //Validate Mandetory Fields/Columns  
  209.                             if (strFieldMandatory[j].ToUpper().Trim().Equals("Y", StringComparison.InvariantCultureIgnoreCase))  
  210.                             {  
  211.                                 if (dtUploadedExcel.Rows[i][j].ToString() == null || dtUploadedExcel.Rows[i][j].ToString() == "")  
  212.                                 {  
  213.                                     error += dtUploadedExcel.Columns[j].ToString() + " Should not be Null in line " + (i + 1) + ";";  
  214.                                 }  
  215.                             }  
  216.                             //Adding custom validations like lets say the age of employee should be >18 less than or equal to 60 years  
  217.                             if (strFieldHeadings[j].ToUpper().Trim().Equals("Age", StringComparison.InvariantCultureIgnoreCase))  
  218.                             {  
  219.                                 if (dtUploadedExcel.Rows[i][j].ToString() != null || dtUploadedExcel.Rows[i][j].ToString() != "")  
  220.                                 {  
  221.                                     if (!int.TryParse(dtUploadedExcel.Rows[i][j].ToString(), out number))  
  222.                                     {  
  223.                                         error += "Age value should be numeric in row: " + (i + 1) + ";";  
  224.                                     }  
  225.                                     else  
  226.                                     {  
  227.                                         if (number < 18 || number > 60)  
  228.                                         {  
  229.                                             error += "Age value should be between 18 and 60 in row: " + (i + 1) + ";";  
  230.                                         }  
  231.                                     }  
  232.                                 }  
  233.                             }  
  234.                             //Check for those column whose datatype is integer specified in ExcelStructure  
  235.                             if (strFieldType[j].ToUpper().Trim().Equals("I") && dtUploadedExcel.Rows[i][j].ToString() != "" &&  
  236.                                 !strFieldHeadings[j].ToUpper().Trim().Equals("Age", StringComparison.InvariantCultureIgnoreCase))  
  237.                             {  
  238.                                 if (!int.TryParse(dtUploadedExcel.Rows[i][j].ToString(), out number))  
  239.                                 {  
  240.                                     error += dtUploadedExcel.Columns[j].ColumnName + " Should be Numeric value in line " + (i + 1) + ";";  
  241.                                 }  
  242.                             }  
  243.                             //Checking the type of the Column if it is String then check the length of the value specified in the Excel File against the max value specified  
  244.                             //inside the ExcelStructure Table using the | operator  
  245.                             if (strFieldType[j].ToUpper().Trim().Equals("S") && dtUploadedExcel.Rows[i][j].ToString() != "")  
  246.                             {  
  247.                                 //Check every field length compared with what we specified in the ExcelStructure Length For String Type  
  248.                                 if (Convert.ToInt32(strFieldSize[j].Trim()) < dtUploadedExcel.Rows[i][j].ToString().Length)  
  249.                                 {  
  250.                                     error += string.Format("Value Length: {0}. Reached Max Length Of {1}", dtUploadedExcel.Rows[i][j].ToString().Length, strFieldSize[j].Trim()) + ";";  
  251.                                 }  
  252.                             }  
  253.                         }  
  254.                         //adding the error details to the Error coulmn of the DataTable which can be used for displaying the error details on the Page  
  255.                         dtUploadedExcel.Rows[i]["Error"] = error;  
  256.                     }  
  257.                 }  
  258.                 return "Success";  
  259.             }  
  260.             catch (Exception ex)  
  261.             {  
  262.                 throw ex;  
  263.             }  
  264.         }  
  265.     }  
  266. } 

Add a new controller with the name HomeController inside the controller folder of your application. Here is the code for that.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.IO;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8. using ImportFromExcel.Domain.Abstract;  
  9.   
  10. using ImportFromExcel.Domain.Entities;  
  11. using ImportFromExcel.WebUI.AppHelper;  
  12. namespace ImportFromExcel.WebUI.Controllers  
  13. {  
  14.     public class HomeController : Controller  
  15.     {  
  16.         private IEmployeeRepository repository;  
  17.   
  18.         public HomeController(IEmployeeRepository repository)  
  19.         {  
  20.             this.repository = repository;  
  21.         }  
  22.   
  23.         public ActionResult Index()  
  24.         {  
  25.             return View();  
  26.         }  
  27.   
  28.         /// <summary>  
  29.        /// This function will be only called when there is a postback using Httppost method from the view. This function accepts an arguement  
  30.         /// named file which is basically the name of the input file control specified in the view. Since MVC4 follows the conventional approach by default.  
  31.         /// </summary>  
  32.         /// <param name="file">HttpPostedFileBase object</param>  
  33.         /// <returns>returns ActionResult</returns>  
  34.   
  35.         [HttpPost]  
  36.         public ActionResult Index(HttpPostedFileBase file)  
  37.         {  
  38.             if (file != null)  
  39.             {  
  40.                 string extension = Path.GetExtension(file.FileName);  
  41.                 if (extension.Equals(".xls") || extension.Equals(".xlsx"))  
  42.                 {  
  43.                     DateTime dt = DateTime.Now;  
  44.                     string filePath = "EmployeeData";  
  45.                     string format = dt.ToString();  
  46.                     format = format.Replace('/''_');  
  47.                     format = format.Replace(':''_');  
  48.                     format = format.Replace(' ''_');  
  49.                     filePath += "_" + format + ".xlsx";  
  50.                     string finalFilePath = Server.MapPath("~/Uploads/" + filePath);  
  51.                     file.SaveAs(finalFilePath);  
  52.                     APIHelper objHelper = new APIHelper(repository);  
  53.                     string errorDetails = string.Empty;  
  54.                     DataSet ds = objHelper.ReadExcelFile(finalFilePath, out errorDetails);  
  55.                     if (errorDetails.Equals("success", StringComparison.InvariantCultureIgnoreCase))  
  56.                     {  
  57.                         int errorCount = ds.Tables[0].AsEnumerable().Where(x => x["Error"].ToString() != "").Count();  
  58.                         if (errorCount > 0)  
  59.                             ViewBag.UploadMsg = "Error Uploading Data";  
  60.                         else  
  61.                         {  
  62.                             ListConverter<Employee> converterObj = new ListConverter<Employee>(ds);  
  63.                             List<Employee> lstData = converterObj.ConvertDataSetToList();  
  64.                             repository.SaveEmployee(lstData);  
  65.                             ViewBag.UploadMsg = "Data Saved Successfully...";  
  66.                         }  
  67.                         return View("Index", ds);  
  68.                     }  
  69.                     else  
  70.                     {  
  71.                         ViewBag.UploadMsg = errorDetails;  
  72.   
  73.                         return View("Index"null);  
  74.                     }  
  75.                 }  
  76.                 ViewBag.Error = "Invalid File Format " + extension + " Valid File Formats allowed are .xls or .xlsx";  
  77.                 return View("Index");  
  78.             }  
  79.             ViewBag.Error = "Kindly Select an Excel File to upload";  
  80.             return View("Index");  
  81.             //to display alter box from Controller you can make use of the following line  
  82.             // return Content("<script type='text/javascript'>alert('Error Uploading Details');</script>");  
  83.         }  
  84.     }  
  85. }   

Now create a strongly typed view from the controller. Here is the code for that.

  1. @model System.Data.DataSet  
  2. @using System.Data;  
  3. @{  
  4.     ViewBag.Title = "Index";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7. <h3>Upload Excel File</h3>  
  8. @if (ViewBag.Error != null)  
  9. {  
  10.     <script type="text/javascript">  
  11.         alert("@ViewBag.Error.ToString()");  
  12.     </script>  
  13. }  
  14. @using (Html.BeginForm("Index""Home", FormMethod.Post, new { enctype = "multipart/form-data" }))  
  15. {  
  16.     <div>  
  17.         <label for="file">Filename:</label>  
  18.         <input type="file" name="file" id="file" />  
  19.         <input type="submit" value="Upload File" />  
  20.     </div>  
  21. }  
  22. <p>  
  23.     @if (ViewBag.UploadMsg != null)  
  24.     {  
  25.         if (ViewBag.UploadMsg == "Data Saved Successfully...")  
  26.         {  
  27.         <script type="text/javascript">  
  28.             alert("@ViewBag.UploadMsg.ToString()");  
  29.         </script>  
  30.         }  
  31.         else if (ViewBag.UploadMsg != "" && Model == null)  
  32.         {  
  33.         <script type="text/javascript">  
  34.             alert("@ViewBag.UploadMsg.ToString()");  
  35.         </script>  
  36.         }  
  37.         else  
  38.         {  
  39.             Html.RenderPartial("~/Views/Shared/_UploadResult.cshtml", Model);  
  40.         }  
  41.     }  
  42. </p> 

PartialView: _UploadResult.cshtml

  1. @model System.Data.DataSet  
  2. @using System.Data;  
  3. @{  
  4.     int counter = 1;  
  5. }  
  6. <div>  
  7.     <h3>Upload Errors.</h3>  
  8. </div>  
  9. <div class="CSSTableGenerator">  
  10.     <table>  
  11.         <tr>  
  12.             <td>Sr.No</td>  
  13.             <td>Sheet Name</td>  
  14.             <td>Emp Name</td>  
  15.             <td>Error Message</td>  
  16.         </tr>  
  17.         @foreach (DataTable table in Model.Tables)  
  18.         {  
  19.             if (table.Rows.Count > 0)  
  20.             {  
  21.                 for (int i = 0; i < table.Rows.Count; i++)  
  22.                 {  
  23.                     if (table.Rows[i]["Error"] != "")  
  24.                     {  
  25.                         <tr>  
  26.                             <td>@counter</td>  
  27.                             <td>@table.TableName</td>  
  28.                             <td>@table.Rows[i]["Name"].ToString()</td>  
  29.                             <td>@table.Rows[i]["Error"].ToString()</td>  
  30.                         </tr>  
  31.                         counter++;  
  32.                     }  
  33.                 }  
  34.             }  
  35.         }  
  36.     </table>  
  37. </div> 

Note: You need to download the application for the smooth functioning

  1. For NinjectControllerFactory that we created in WebUI
  2. For App_Helper Files that are used for uploading and reading the Excel files content
  3. Before running, kindly create an Excel file with some records that you can make use of to upload it.
  4. The following is the dummy ExcelFile:

    ExcelFile

Here we explicitly did not specify the name of the Employee and age > 60 and non numeric value in the age column.

Now we can run the application and try to upload the Excel file. Here is the final output, since there are validation errors our screen will look like this:

uploaded the excel file
Now let's try to update the Excel and remove all the validation errors.

validation errors

Stop the application and open ServerExplorer and refersh your database and right-click on the Employee Table and select the option "Show Table Data".

ServerExplorer

You'll find that our data is stored that we uploaded using Excel.

excel

Here within our application there is the small issue that if you continue to upload new files it will continue to increment the EmployeeID since it takes it as an auto-increment column and also it's a primary key as per EntityFramework Conventions and we didn't add a field named Batch_no so we won't be able to recognize the upload details. But nevertheless since we are learning it, for learning purposes our task has been done. Small and Simple Logic anyone can implement for adding the Batch_no as the primary key for the employee Table and making it an auto-generated column. You can also add the date and time or when the upload was made.


Similar Articles