3

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?

djblois
  • 963
  • 1
  • 17
  • 52

2 Answers2

6

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);
    }

Update : I did not included the variable declarations because this method was inside a class. The 3 variables are class members.

NickB
  • 349
  • 4
  • 15
  • Beautifully simple workaround. Thanks! Of course, we have to specify the data type while declaring and initializing each of the 3 variables, which we can do by simply adding `var` at the beginning of each of the 3 lines inside the curly braces. – Abel Wenning Jun 22 '21 at 17:04
  • @AbelWenning In my example, the method was inside a class. That's the reason I did not include the declarations. – NickB Jun 23 '21 at 15:04
0

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.
Francois Botha
  • 4,520
  • 1
  • 34
  • 46