Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
Read and excel file into a workbook while open in excel
posted

I have followed the examples and am successfully reading an excel file into a workbook.  However, it errors out if the file is currently open in excel.  This "feature" is not going to go over well with my end users.  I cannot find any settings that act appropriately.

Workbook wb;
Worksheet ss;

wb = Workbook.Load(excelFileName);

Receive the following error:

"Error while processing work sheet:  The process cannot access the file 'c:\pathname\combined.xlsx' because it is being used by another process."

Parents
No Data
Reply
  • 44743
    posted

    You can manually open the file using the FileStream constructor that takes a FileShare value of ReadWrite. This allows you to share the file with other programs reading and writing it, which MS Excel states it wants to do when it has the file open:

    Workbook wb;

    using (var stream = new FileStream(excelFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

    wb = Workbook.Load(stream);

Children
No Data