简体   繁体   English

如何在不锁定的情况下打开Excel文件?

[英]How can I open an Excel file without locking it?

I have a process that builds an Excel report, then opens it for the user. 我有一个构建Excel报告的过程,然后为用户打开它。 The problem is if someone leaves the file open, it stays locked and nobody else can build the report until the first person exits the excel file. 问题是如果有人将文件保持打开状态,它会保持锁定状态,并且在第一个人退出excel文件之前,没有其他人可以构建报告。

Is there a way to open an Excel file without locking it, using either Process.Start or Microsoft's Interop.Excel library? 有没有办法打开Excel文件而不使用Process.Start或Microsoft的Interop.Excel库锁定它?

I use the Interop library to build the file each time the report is run, and save it as a static file name in a shared network folder where this application is run from 我每次运行报表时都使用Interop库来构建文件,并将其作为静态文件名保存在运行此应用程序的共享网络文件夹中

using Excel = Microsoft.Office.Interop.Excel;

...

xlsBook.SaveAs(newFileName, Excel.XlFileFormat.xlWorkbookNormal);

And open the file using Process.Start 并使用Process.Start打开该文件

Process.Start(newFileName);

You can try to open the file in read-only mode: 您可以尝试以只读模式打开文件:

var app = new Microsoft.Office.Interop.Excel.Application();
var workbook = app.Workbooks.Open(filename, ReadOnly: true);

Or you can try to save it in shared mode: 或者您可以尝试以共享模式保存它:

workbook.SaveAs(filename, AccessMode: XlSaveAsAccessMode.xlShared);

If the end user only has to read the file instead of also modifying it, you could create a shadow copy and then open that copy. 如果最终用户只需要读取文件而不是修改它,您可以创建卷影副本然后打开该副本。

Simply copy the original file to a temporary location and open it from there. 只需将原始文件复制到临时位置并从那里打开即可。 The original file remains untouched and can thus be opened by others. 原始文件保持不变,因此可以由其他人打开。

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

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