Let the excel sheet contains the above data along with the header. Now in the application, take a fileupload control, a button and a gridview. In the botton click write the below code :
try
{DataSet ds = new DataSet();//file upload pathstring path = FileUpload1.PostedFile.FileName;try
//Create connection string to Excel work bookstring excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from ExcelOleDbCommand cmd = new OleDbCommand("Select [EID],[LOB],[Resource First Name],[Resource Middle Initial],[Resource Last Name],[Primary Role],[Input Type Code],[Financial Location],[Currency Code],[Actual Payable Hours] from [Sheet1$]", excelConnection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);da.Fill(ds);
excelConnection.Open();
OleDbDataReader dReader;dReader = cmd.ExecuteReader();
//SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);////Give your Destination table name//sqlBulk.DestinationTableName = "Excel_table";//sqlBulk.WriteToServer(dReader);excelConnection.Close();GridView1.DataSource = ds;
GridView1.DataBind();catch (Exception ex){
Response.Write(ex.StackTrace);
}
The Result will look as below :
Now since we got the data in dataset from the excel, we can easily do anything we need with the data(modify) and insert it in the database table.
No comments:
Post a Comment