0

I am inserting data into Sql table from excel sheet. oledbConnection.GetSchema("Table_Name") getting excel sheet names in sorted order. I dont want them to be sorted coz i want to get the first sheet in order to perform some operations on it. Here is my code snippet.

string con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel +";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            OleDbConnection cn = new OleDbConnection(con);
            cn.Open();

            DataTable sheetTable = cn.GetSchema("Tables");
            string strSHeetName = Convert.ToString(sheetTable.Rows[0]["TABLE_NAME"]);

any help would be highly appreciated.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Mujeeb
  • 83
  • 4
  • 14
  • You can do this... Hope it will help... http://stackoverflow.com/questions/1164698/using-excel-oledb-to-get-sheet-names-in-sheet-order – Developer May 17 '13 at 07:19

1 Answers1

0

Try this code, this worked for me...

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelBook = xlApp.Workbooks.Open(filePath); 
string[] excelSheets = new string[excelBook.Worksheets.Count];
int i = 0;
foreach(Microsoft.Office.Interop.Excel.Worksheet wSheet in excelBook.Worksheets)    
{
  excelSheets[i] = wSheet.Name;
  i++;
}

*Replace "filePath" with your file location path...