| Author: chandra sekhar 25 Jul 2006 | Member Level: Gold | Rating: Points: 2 |
hi by using OleDbConnection, we can connect to Excel sheet. in this case, excel sheet act as SQL Server. that means we can write select, update query
|
| Author: Pritam Baldota 25 Jul 2006 | Member Level: Gold | Rating: Points: 2 |
you can use oledb connection and excel act as database.
|
| Author: BRUCE DHARA 01 Aug 2006 | Member Level: Bronze | Rating: Points: 2 |
Hey Anny,
I understand your urgency, well I am sending you the quick & dirty way of doing it. Ofcourse there are nerds out there who will post 10 different responses to point out deficiencies in my code but here it is :
Import these namespaces:
using System.Data; using System.Data.OleDb; using Microsoft.Office.Interop.Excel;
Insert this code bit into Load even of your form :
try { // Put user code to initialize the page here string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Server.MapPath("\\projectfolder\\YourEXCEL.xls")+";Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES;\"";
OleDbConnection objConn=new OleDbConnection(sConnectionString); OleDbCommand oCmd=new OleDbCommand("SELECT * FROM [Sheet1$]", objConn); //Replace Sheet1 with your excel form's sheet name, append the $ the sign in the end.
objConn.Open(); //ddgXls is the name of the datagrid ddgXls.DataSource=oCmd.ExecuteReader(); ddgXls.DataBind(); objConn.Close(); }
catch(Exception ex) { Response.Write("ERROR:" +ex.Message.ToString()); Response.Write(ex.StackTrace.ToString()); } } //Make sure you add your assembly reference to Microsoft Office 8/0/11, leave the version in the connection string as Excel 8.0 else you will get ISAM installer not found error which will take you to a whole new world of debugging w.r.to registry keys etc.
If you get an ISAM error, dont panic. Just make sure your connection string is as described & if the problem still persists check msdn for ISAM Installer Error.
Hope this helps..
Bruce M Dhara
|