Excel 2007连接string(xlsx)

你好,我想从一个Excel的xlsx文件拉,但我不断收到一个错误“外部表格不是所需的格式”。 下面我用我的连接string:


String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 12.0;'", filePath); using (OleDbConnection excelConnection = new OleDbConnection(excelConnString)) { excelConnection.Open(); 

filepath – 只是一个string,其中包含我的Excel Spreadsheet的文件path,它是从表单上传控件中提取的。

我到了我想要打开连接的代码点,它只是出错。 但是,这里的踢球….我试图在一个.XLS文件相同的连接string,并连接好,甚至跑了一个查询,如预期。 当我谷歌的连接string,我发现我使用相同的连接作为其他人只是我不会工作。 任何帮助表示赞赏。

尝试这个

 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 12.0 Xml; HDR=YES'" 

或这个

 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 12.0; HDR=YES'" 

试试这个macros启用连接string:

 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsm; Extended Properties="Excel 12.0 Macro;HDR=YES";