C# - 如何在不知道工作表名称的情况下导入Excel文件

时间:2015-06-05 21:12:50

标签: c# excel winforms vba

当工作表名称为" Sheet1"时,我的代码将读取excel文件。我想知道是否有办法阅读任何工作表名称?例如:(TaxForm或EmployeeList)

 private DataTable readExcelFile(string path)
    {

        string excelConn = @"Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + path + "; Extended Properties=Excel 8.0;";
        System.Data.OleDb.OleDbConnection oleConn = new System.Data.OleDb.OleDbConnection(excelConn);
        DataTable xlsDs = new DataTable();
        try
        {
            if (!string.IsNullOrEmpty(path))
            {
                string strSQL = "SELECT * from [Sheet1$]";
                System.Data.OleDb.OleDbDataAdapter oleDa = new System.Data.OleDb.OleDbDataAdapter(strSQL, oleConn);
                oleDa.Fill(xlsDs);


            }

}

0 个答案:

没有答案