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.

4

1 回答 1

0

试试这个代码,这对我有用......

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++;
}

*将“filePath”替换为您的文件位置路径...

于 2014-11-06T04:12:29.343 回答