Wednesday, January 27, 2010

For using excel file as database

6:11 AM Posted by Unknown , No comments
For reading Excel files and getting the data
string FilePath = Server.MapPath("exmaple.xls");

DataSet myDataset = new DataSet();
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable DTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//here the code will read sheet from spreadsheet with any name --generic
OleDbDataAdapter myData = new OleDbDataAdapter
(@"SELECT * FROM [" +
DTable.Rows[0]["TABLE_NAME"].ToString() + "]", conn);
//
myData.Fill(myDataset);

0 comments:

Post a Comment