Hi Andrea & dharmendr! Thanks a ton for the response... I'm nearly done with the code but I can't get the gridview to display while the page default2.aspx is loaded.
This the code-behind file for Default.aspx. On Button Click, the page gets redirected to Default2.aspx and the gridview is displayed on page_load.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Pages_Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void upFile_Click(object sender, EventArgs e)
{
try
{
if (excelPath.HasFile)
{
string fileName = Path.GetFileName(excelPath.PostedFile.FileName);
string fileExtension = Path.GetExtension(excelPath.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
excelPath.SaveAs(fileLocation);
Session["filepath"] = excelPath.PostedFile.FileName;
if (fileExtension == ".xls")
{
//excelPath.SaveAs(Server.MapPath("~/"));
string f = Server.MapPath("~/App_data/");
Session["filepath"] = f;
Response.Redirect("Default2.aspx");
}
else if (fileExtension == ".xlsx")
{
//excelPath.SaveAs(Server.MapPath("~/"));
string f = Server.MapPath("~/App_data/");
Session["filepath"] = f;
Response.Redirect("Default2.aspx");
}
else
{
string resp = "<script type = 'text/javascript'> var resp = window.alert('Only Excel Files Allowed!'); </script>";
Response.Write(resp);
}
}
else
{
string resp = "<script type = 'text/javascript'> window.alert('Upload an Excel File'); </script>";
Response.Write(resp);
}
}
catch(Exception ex)
{
Response.Write(ex);
}
}
}
This is the code-behind file for Default2.aspx:
I'm using session variable as I append columns to the generated gridview and assign values dynamically.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Pages_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string fpath = Session["filepath"].ToString();
if(fpath!="")
{
if(fpath.EndsWith("xls"))
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fpath + ";Extended Properties=Excel 8.0;");
con.Open();
//Get the list of sheet available in excel sheet
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//Get first sheet name
string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();
//Select rows from first sheet in excel sheet and fill into dataset
OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT * FROM [" + getExcelSheetName + @"]", con);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataSet ExcelDataSet = new DataSet();
ExcelAdapter.Fill(ExcelDataSet);
con.Close();
//Bind the dataset into gridview to display excel contents
ExcelGrid.DataSource = ExcelDataSet;
ExcelGrid.DataBind();
}
else if (fpath.EndsWith("xlsx"))
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fpath + ";Extended Properties=Excel 12.0;");
con.Open();
//Get the list of sheet available in excel sheet
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//Get first sheet name
string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();
//Select rows from first sheet in excel sheet and fill into dataset
OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT * FROM [" + getExcelSheetName + @"]",con);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataSet ExcelDataSet = new DataSet();
ExcelAdapter.Fill(ExcelDataSet);
con.Close();
//Bind the dataset into gridview to display excel contents
ExcelGrid.DataSource = ExcelDataSet;
ExcelGrid.DataBind();
}
}
}
}
Kindly help...