外部 table 不是预期格式 OLEDB 12.0 with Excel 2007
External table is not in the expected format OLEDB 12.0 with Excel 2007
我开发了一个 WEB API 服务,它会在用户上传时从 excel 文件中读取数据。
我使用 OLEDB 如下:
if (Path.GetExtension(filePath).ToUpper() == ".XLS")
{
oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\"");
}
else
{
oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
}
oledbConn.Open(); //Exception thrown at here
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
cmd.Connection = oledbConn;
cmd.CommandText = "SELECT DISTINCT(["+mo_field+"]),["+model_field+"],["+content_filed+"] FROM ["+ecn_field+"] WHERE ["+mo_field+"] IS NOT NULL AND ["+active_field+"] ='1'";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds,"NewMO");
但抛出异常:
External table is not in the expected format
我的服务器安装了 Window Server 2012 RC2 64 位所以我尝试安装 Microsoft 数据库引擎 2010 再分发 32 位/64 位。和 Microsoft 数据库引擎 2007 32 位。但它仍然不起作用。我搜索了 3 天,每个帖子都说安装 Microsoft 数据库引擎将修复错误。此代码适用于 Office 2010/2013。
非常感谢您的帮助!
var ds = new DataSet();
var da = new OleDbDataAdapter("SELECT DISTINCT(["+mo_field+"]),["+model_field+"],["+content_filed+"] FROM ["+ecn_field+"] WHERE ["+mo_field+"] IS NOT NULL AND ["+active_field+"] ='1'",
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'");
da.Fill(ds,"NewMO");
并确保文件确实是一个 Excel 文件,方法是在 Excel 中打开它。
更新
这里还有一些来自 https://www.connectionstrings.com/excel/ 的连接字符串可以尝试
foreach (var cs in new string[] {
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=YES';",
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0; HDR = Yes; IMEX = 1';" })
try { using (var con = new System.Data.OleDb.OleDbConnection(cs)) con.Open(); MessageBox.Show(cs + " worked bro!!"); } catch { }
foreach (var cs in new string[] {
"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + filePath + ";",
"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + filePath + ";",
"Driver={Microsoft Excel Driver (*.xls)};Dbq=" + filePath + ";ReadOnly=0;"})
try { using (var con = new System.Data.Odbc.OdbcConnection(cs)) con.Open(); MessageBox.Show(cs + " worked bro!!"); } catch { }
我开发了一个 WEB API 服务,它会在用户上传时从 excel 文件中读取数据。
我使用 OLEDB 如下:
if (Path.GetExtension(filePath).ToUpper() == ".XLS")
{
oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\"");
}
else
{
oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
}
oledbConn.Open(); //Exception thrown at here
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
cmd.Connection = oledbConn;
cmd.CommandText = "SELECT DISTINCT(["+mo_field+"]),["+model_field+"],["+content_filed+"] FROM ["+ecn_field+"] WHERE ["+mo_field+"] IS NOT NULL AND ["+active_field+"] ='1'";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds,"NewMO");
但抛出异常:
External table is not in the expected format
我的服务器安装了 Window Server 2012 RC2 64 位所以我尝试安装 Microsoft 数据库引擎 2010 再分发 32 位/64 位。和 Microsoft 数据库引擎 2007 32 位。但它仍然不起作用。我搜索了 3 天,每个帖子都说安装 Microsoft 数据库引擎将修复错误。此代码适用于 Office 2010/2013。
非常感谢您的帮助!
var ds = new DataSet();
var da = new OleDbDataAdapter("SELECT DISTINCT(["+mo_field+"]),["+model_field+"],["+content_filed+"] FROM ["+ecn_field+"] WHERE ["+mo_field+"] IS NOT NULL AND ["+active_field+"] ='1'",
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'");
da.Fill(ds,"NewMO");
并确保文件确实是一个 Excel 文件,方法是在 Excel 中打开它。
更新
这里还有一些来自 https://www.connectionstrings.com/excel/ 的连接字符串可以尝试
foreach (var cs in new string[] {
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=YES';",
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0; HDR = Yes; IMEX = 1';" })
try { using (var con = new System.Data.OleDb.OleDbConnection(cs)) con.Open(); MessageBox.Show(cs + " worked bro!!"); } catch { }
foreach (var cs in new string[] {
"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + filePath + ";",
"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + filePath + ";",
"Driver={Microsoft Excel Driver (*.xls)};Dbq=" + filePath + ";ReadOnly=0;"})
try { using (var con = new System.Data.Odbc.OdbcConnection(cs)) con.Open(); MessageBox.Show(cs + " worked bro!!"); } catch { }