简体   繁体   English

使用FileHelpers ExcelStorage-无法打开Excel文件

[英]Using filehelpers ExcelStorage - Excel File not opening

I am using filehelpers ExcelStorage somewhat like this: 我正在使用像这样的filehelpers ExcelStorage:

ExcelStorage provider = new ExcelStorage(typeof(Img));
provider.StartRow = 2;
provider.StartColumn = 1;
provider.FileName = "Customers.xls";
provider.HeaderRows = 6;
provider.InsertRecords(imgs.ToArray()); // imgs was a list before

And when I am done inserting records, I would like to open the Excelfile I created (with my software still running). 当我完成记录插入后,我想打开我创建的Excelfile(我的软件仍在运行)。 But it seems that Excel is somehow locked. 但是似乎Excel已被锁定。 Ie there is an Excel instance running in process manager. 即,有一个Excel实例在流程管理器中运行。 When I kill all Excel instances I can open the file. 当我杀死所有Excel实例时,我可以打开文件。 Do I have to dispose the ExcelStorage in some sort of way? 我是否必须以某种方式处置ExcelStorage?

I've used FileHelpers, but not ExcelStorage. 我使用了FileHelpers,但没有使用ExcelStorage。 The link here suggests that you should probably be using FileHelpers.ExcelNPOIStorage instead. 这里的链接建议您可能应该改用FileHelpers.ExcelNPOIStorage。

Looking at the source code for ExcelStorage, there is no public dispose method. 查看ExcelStorage的源代码,没有公共处理方法。 There is a private CloseAndCleanup method which is called at the end of InsertRecords. 在InsertRecords的末尾有一个私有的CloseAndCleanup方法。 Therefore I don't think there's anything you are doing wrong. 因此,我认为您没有做错任何事情。

The usage of ExcelNPOIStorage looks very much the same, there is a call to GC.Collect() within the private cleanup method here, so I'd guess that there was a known issue with the cleanup of the prior version of the component. ExcelNPOIStorage的用法看起来非常相似,这里的私有清理方法中有一个对GC.Collect()的调用,因此我猜想该组件的先前版本的清理存在一个已知问题。

Your best bet is to grab a copy of HANDLE.EXE which you can use with an elevated command prompt to see what has a handle to the file in question. 最好的选择是获取HANDLE.EXE的副本,您可以将其与提升的命令提示符一起使用,以查看具有相关文件句柄的文件。 This may be your code, anti virus or excel (if open). 这可能是您的代码,防病毒软件或excel(如果已打开)。 Excel does keep a full lock on a file when open preventing ordinary notepad access etc. Excel会在打开时保留对文件的完全锁定,从而阻止普通记事本的访问等。

If the process owning the handle to the file is your own code, then see if the handle exists once you have exited back to the development environment. 如果拥有该文件句柄的进程是您自己的代码,那么一旦退出开发环境,请查看该句柄是否存在。 If that clears the handle, then you are not releasing the lock properly and that can be slightly trickier as it will depend on exactly what you have coded. 如果清除了该句柄,则说明您没有正确释放该锁,这可能会有些棘手,因为这将完全取决于您编写的代码。

The CloseAndCleanup function mentioned by @timbo is only called from a few places, the Sheets property and the ExtractRecords / InsertRecords functions. 通过提到的CloseAndCleanup功能@timbo仅由少数几个地方,Sheets属性和ExtractRecords / InsertRecords函数调用。 The only other thing to wonder is whether you are seeing any exceptions when it attempts to perform the CloseAndCleanup or the reference count the Excel application hasn't been properly released by the COM system. 唯一想知道的是,当它尝试执行CloseAndCleanup时是否看到任何异常,或者COM系统未正确释放Excel应用程序的引用计数。

If you can replicate this with a small sample app, I will be more than willing to give it a quick test and see what happens. 如果您可以使用一个小型示例应用程序来复制它,那么我将更愿意对其进行快速测试,看看会发生什么。

Note 1 , if you are running your code from within Visual Studio, it may be a process called <APPNAME>.VSHOST.EXE which is visual studio's development process, or if you've turned off Visual Studio hosting, just your <APP>.EXE. 注意1 ,如果您是从Visual Studio中运行代码,则可能是一个名为<APPNAME> .VSHOST.EXE的过程,这是Visual Studio的开发过程,或者如果您已关闭Visual Studio托管,则只需<APP> 。可执行程序。 If running within IIS for a web page or web service, you will more than likely have a w3p process. 如果在IIS中针对网页或Web服务运行,则很有可能会有w3p进程。

Note 2 , if you run handle without being elevated, it may or may not find the handle to the file in question. 注意2 ,如果在未提升手柄的情况下运行手柄,则可能会或可能不会找到相关文件的手柄。 Therefore, it is always recommended to run elevated to ensure results are accurate. 因此,始终建议将其抬高以确保结果准确。

Note 3 , the difference between ExcelStorage and ExcelNPOIStorage is that the former deals with .xls and the latter deals with .xlsx if I remember rightly. 注意3 ,如果我没记错的话,ExcelStorage和ExcelNPOIStorage之间的区别是前者处理.xls,而后者处理.xlsx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM