Today one of my team members asked me a very simple question though very important question,
"How could we fetch Excel Records using ADO.Net? Could you give me code snippet of same? "
I replied him; it is pretty possible using oledDbConnection
I told him to add below reference
I gave him below straight forward code snippet. This function is
- Returning DataTable
- Reading XLS file from called YourFile.xls from F Drive.
- Reading Sheet1
01 | public static DataTable GetItemsFromExcel1() |
04 | List<Items> lstItems = new List<Items>(); |
06 | DataTable dt = new DataTable(); |
08 | OleDbConnection excelConnection = new OleDbConnection ( @"Provider=Microsoft.Jet.OLEDB.4.0;" |
09 | + @"Data Source=F:\YourFile.xls;" |
10 | + @"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;""" ); |
12 | excelConnection.Open(); |
15 | OleDbDataAdapter dbAdapter = |
17 | ( "SELECT * FROM [Sheet1$]" , excelConnection); |
22 | excelConnection.Close(); |
After using this function in his code , he was very happy and paid by coffee bill
No comments:
Post a Comment