yashu v

yashu v

  • NA
  • 98
  • 15.1k

upload excel sheet files

Jul 31 2017 3:51 AM
Pls help me,
 
 
 upload excel files show gridview using OLEDB dataReader with pagination asp.net, C#
 
protected void PopulateGrid(object sender, EventArgs e)
{
//First Create the instance of Stopwatch Class
Stopwatch sw = new Stopwatch();
// Start The StopWatch ...From 000
sw.Start();
// CHECK IF A FILE HAS BEEN SELECTED.
if ((FileUpload.HasFile))
{
if (!Convert.IsDBNull(FileUpload.PostedFile) &
FileUpload.PostedFile.ContentLength > 0)
{
// SAVE THE SELECTED FILE IN THE ROOT DIRECTORY.
FileUpload.SaveAs(Server.MapPath(".") + "\\" + FileUpload.FileName);
// SET A CONNECTION WITH THE EXCEL FILE.
OleDbConnection myExcelConn = new OleDbConnection
("Provider=Microsoft.ACE.OLEDB.12.0; " +
"Data Source=" + Server.MapPath(".") + "\\" + FileUpload.FileName +
";Extended Properties=Excel 12.0;");
try
{
myExcelConn.Open();
// GET DATA FROM EXCEL SHEET.
OleDbCommand objOleDB =
new OleDbCommand("SELECT *FROM [Sheet1$]", myExcelConn);
// READ THE DATA EXTRACTED FROM THE EXCEL FILE.
OleDbDataReader objBulkReader = null;
objBulkReader = objOleDB.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(objBulkReader);
// FINALLY, BIND THE EXTRACTED DATA TO THE GRIDVIEW.
GridView1.DataSource = dt;
GridView1.DataBind();
sw.Stop();
//Writing Execution Time in label
string ExecutionTimeTaken = string.Format("Minutes :{0}\nSeconds :{1}\n Mili seconds :{2}", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.TotalMilliseconds);
label1.Text = ExecutionTimeTaken;
lblConfirm.Text = "DATA IMPORTED TO THE GRID, SUCCESSFULLY.";
lblConfirm.Attributes.Add("style", "color:green");
}
catch (Exception ex)
{
// SHOW ERROR MESSAGE, IF ANY.
lblConfirm.Text = ex.Message;
lblConfirm.Attributes.Add("style", "color:red");
}
finally
{
// CLEAR.
myExcelConn.Close(); myExcelConn = null;
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
// PopulateGrid();
}
}
 

Answers (1)