C# 用C解析XLS文件时出现问题#

C# 用C解析XLS文件时出现问题#,c#,excel,C#,Excel,好吧,让我们看看我是否能理解这一点 我编写了一个程序,可以解析Excel文件,它工作得很好。我使用以下命令进入该文件: string FileToConvert = Server.MapPath(".") + "\\App_Data\\CP-ARFJN-FLAG.XLS"; string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileToConvert + ";Extended Propertie

好吧,让我们看看我是否能理解这一点

我编写了一个程序,可以解析Excel文件,它工作得很好。我使用以下命令进入该文件:

string FileToConvert = Server.MapPath(".") + "\\App_Data\\CP-ARFJN-FLAG.XLS";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileToConvert + ";Extended Properties=Excel 8.0;";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
//this next line assumes that the file is in default Excel format with Sheet1 as the first sheet name, adjust accordingly
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [CP-ARFJN-FLAG$]", connection);
这个很好用。但当我在实际文件上尝试它时(它是由另一个程序提供给我的),我得到以下错误:

System.Data.OleDb.OleDbException: External table is not in the expected format. at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at wetglobe.Page_Load(Object sender, EventArgs e)
但是,我认为这就是问题所在。如果我获取该文件,并将其保存到本地Excel中,首先会出现以下弹出窗口:

CP-ARFJN-FLAG.XLS可能包含功能 与文本(选项卡)不兼容的 分隔)。你想保留这张支票吗 这种格式的工作簿

  • 若要保留此格式,将 要删除任何不兼容的功能,请单击 对
  • 要保留这些功能,请单击“否”。 在最新的Excel中保存一份副本 格式
  • 要查看可能丢失的内容,请单击 救命啊
如果我单击“否”,然后将其保存为当前Excel格式,则程序将正常工作

所以我假设这是保存在一些疯狂的旧Excel格式

我想我的问题是:

  • 我如何知道Excel版本是什么 救了这个
  • 我如何在其当前版本中解析它 国家
  • -或者-我可以通过编程将其保存为新版本吗

我希望这是清楚的。。。谢谢。

听起来,您的第三方应用程序生成的XLS文件可能不是真正的Excel格式-它可能实际上是一个以制表符分隔的文本文件,扩展名为.XLS

尝试用文本编辑器打开它,然后查看


如果是制表符分隔的,您可以放弃OleDB适配器,将其作为标准文本文件打开/解析。

如果生成的文件的格式将来可能会更改(可能是在升级第三方应用程序时),您可能更喜欢使用。这些将加载Excel生成的任何版本或格式的文件。缺点是您需要在服务器上安装Office。

我解决了这个问题。Excel文件应由MS Excel 2003生成,而不是从MS Excel 2007“另存为97-2003”生成。 所以你必须从任何来源下载一个文件。然后手动将数据复制到工作表中。它对我起了作用。

错误

[OleDbException] External table is not in the expected format.   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OleDb.OleDbConnection.Open()
解决方案

我在服务器端应用程序中遇到了上述错误。当您同时访问多个excel文件时,可能会出现此错误。我的代码中缺少了
connection.close()
connection.dispose()
方法。添加此代码后,问题得到解决。
为了安全起见,您也可以在catch块中添加此代码
connection.open()
connection.dispose()
方法对于
Oledb
连接是必需的。

。。。另一个缺点是,Office的服务器端自动化不受支持,不可扩展,并且存在许多问题,包括这里描述的问题:谁说它是作为服务运行的?