Monday, 11 February 2013
How to fetch data from Excel file to Data Set in ASP .Net
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GenerateExcelData();
}
}
private void GenerateExcelData()
{
try
{
OleDbConnection oledbConn;
string path = System.IO.Path.GetFullPath(@"C:\Users\user\Desktop\PriceAnalysis.xls");
oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet2$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "Table");
//return ds.Tables[0];
}
catch
{
//return null;
}
}
}
Note : where Sheet2 is your sheet name.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment