简体   繁体   中英

Open an excel file in exclusive mode using interop in C#

In my application I want to be able to open an excel file that already exists, and add information to this file. I use the Microsoft Interop libraries. I have noticed that if you create a new spreadsheet using Add and then Saveas you can set the file to be in exclusive mode until you close the excel application.

However I haven't found a way to do the same when Opening an existing file. The reason I want to do that is because, if I open the excel file while my application is open, then I get an exception. I presume that when I am opening the excel file using excel, it is automatically opened in exclusive mode.

I though of opening the file and then using saveas, but it didn't work.

private object missing=Type.Missing;
workbook = workbooks.Open(excelfilename,missing, false, missing, missing, missing,missing,missing,missing,missing,false, missing,missing, missing, missing);
workbook.SaveAs((String)excelfilename, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges, missing, missing, missing, missing); 

I am getting the exception later in the code:

Excel.Range testrange = sheet.get_Range(rangetext);

The exception I am getting is a COMException, "Exception from HRESULT: 0x800401A8".

您可以转到本文以获取有关如何使用Microsoft互操作库打开Excel文件/添加新工作表/获取范围和设置属性的完整示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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