2
votes

I am using ClosedXML to make reports for people in Excel. There are 3 issues that I would like some help with.

1) I have this code here so I can access data from a workbook. It works correctly except for when someone has that workbook open. I don't care if it opens a read only copy, if possible, because all I am doing is accessing information from it.

var WorkbookCopyFrom = new XLWorkbook(WorksheetToCopy);

How can I open it even when someone has it open?

2) In my previous company, I used the Office Interop Nuget package to do something similar. In that package I was able to show the excel file while working on it using this code:

excelApp.Visible = true;

Is there something similar in ClosedXML?

2
It's bad style to post unrelated questions in one question on StackOverflow. Split them up in separate questions. However, I'll answer some of them below. - Francois Botha
Sorry I will remove Question 3 - djblois

2 Answers

3
votes

I stumbled on the same problem. A workaround could be opening a FileStream instead of the file directly.

    public void LoadDocumentReadOnly(string fn)
    {
        filename = fn;
        fileStream = new FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        Document = new XLWorkbook(fileStream);
    }
0
votes

ClosedXML is wrapper around OpenXML and is subject to the same constraints.

  1. OpenXML can't open files currently in use.
  2. OpenXML is designed to have no dependency on Excel being installed on the system. So this isn't possible. Office Interop is the only way, or maybe WINAPI.
  3. Not relevant to ClosedXML.