Excel“外部表格不是预期的格式." [英] Excel "External table is not in the expected format."
问题描述
我正在尝试使用下面显示的代码读取 Excel (xlsx) 文件.我收到外部表不是预期的格式".错误,除非我已经在 Excel 中打开了文件.换句话说,我必须先在 Excel 中打开文件,然后才能从我的 C# 程序中读取 if.xlsx 文件位于我们网络上的共享位置.如何在不必先打开文件的情况下读取文件?谢谢
I'm trying to read an Excel (xlsx) file using the code shown below. I get an "External table is not in the expected format." error unless I have the file already open in Excel. In other words, I have to open the file in Excel first before I can read if from my C# program. The xlsx file is on a share on our network. How can I read the file without having to open it first? Thanks
string sql = "SELECT * FROM [Sheet1$]";
string excelConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathname + ";Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"";
using (OleDbDataAdapter adaptor = new OleDbDataAdapter(sql, excelConnection)) {
DataSet ds = new DataSet();
adaptor.Fill(ds);
}
推荐答案
外部表不是预期的格式."通常在尝试使用带有以下连接字符串的 Excel 2007 文件时发生:Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0
"External table is not in the expected format." typically occurs when trying to use an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0
使用以下连接字符串似乎可以解决大多数问题.
Using the following connection string seems to fix most problems.
public static string path = @"C:srcRedirectApplicationRedirectApplication301s.xlsx";
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
这篇关于Excel“外部表格不是预期的格式."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!