I am trying to find solution for below case, Can any one suggest me solutions/ ideas.....
I have Excel Template like below shared link
I need to Import Excel Sheet data and i need to make validate all headers, If any errors I would like to show error message with particular Number and Header to User
Example to validate fields :
Name : Required,ValidString
Date_of_Birth : ValidDate
Age : ValidInteger
Debit : ValidFloat
Amount : ValidCurrency,Required
I have a solution which reads excel file and convert it to Data table...
Created using Visual Studio 2013
public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(HttpPostedFileBase fileName) { OleDbConnection con; string connectionString; string[] workSheetNames; connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName.FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\""; con = new OleDbConnection(connectionString); con.Open(); //get all the available sheets System.Data.DataTable dataSet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //get the number of sheets in the file workSheetNames = new String[dataSet.Rows.Count]; int i = 0; foreach (DataRow row in dataSet.Rows) { //insert the sheet's name in the current element of the array //and remove the $ sign at the end workSheetNames[i] = row["TABLE_NAME"].ToString().Trim(new[] { '$' }); i++; } OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [" + workSheetNames[0] + "$]", con); DataSet excelDataSet = new DataSet(); cmd.Fill(excelDataSet); con.Close(); if (con != null) { con.Close(); con.Dispose(); } if (dataSet != null) dataSet.Dispose(); string isSucess = ValidateDataTable(excelDataSet.Tables[0]); return new EmptyResult(); } private string ValidateDataTable(DataTable dataTable) { throw new NotImplementedException(); } }
Thanks for your help