简体   繁体   English

在 C# 中以只读方式打开 Excel 文件,同时在 Excel 中打开文件

[英]Open an Excel file in C# as read-only whilst file is open in Excel

I'm writing a application which converts data from an excel file into an XML file for another application.我正在编写一个应用程序,它将数据从 excel 文件转换为 XML 文件用于另一个应用程序。 You make some changes to the excel file, save them, load them in my C# forms application as readonly and then save the resultant xml.您对 excel 文件进行了一些更改,保存它们,将它们加载到我的 C# forms 应用程序为只读,然后保存结果 Z4F635D81E01238 Only problem is, this is a very iterative process and at the moment my application throws an exception if the file is open in excel.唯一的问题是,这是一个非常迭代的过程,目前如果文件在 excel 中打开,我的应用程序会引发异常。 So to get it to work, you have to shut down excel (or close file), run application, then reopen excel.所以要让它工作,你必须关闭 excel(或关闭文件),运行应用程序,然后重新打开 excel。

My code is this;我的代码是这样的;

using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))

which causes a IO Exception.这会导致 IO 异常。

Oh, just found the answer;哦,刚刚找到答案; Change the last argument to FileShare.ReadWrite将最后一个参数更改为 FileShare.ReadWrite

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

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