2

I'm trying to write a code to read the excel to get row count and data. I'm getting an exception that the excel is encrypted or on read-only mode.

wb = xlApp.Workbooks.Open(srcFile, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",true, false, 0, true, false, false);

could anyone please provide some help in here.

Ajay2707
  • 5,690
  • 6
  • 40
  • 58

3 Answers3

0

Use Using method to open file, which also take care of dispose object when its work complete.

May be the file is open or not, still you open file as readmode via 'FileShare.Read' as

using (FileStream fs = File.Open(<file-path>, FileMode.Open, FileAccess.Read, FileShare.Read))

check this link

Open file ReadOnly

Exception in opening a file that is already open

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
0

Use

    var workbook = 
      excelApp.Workbooks.Open(_mirrorFileName,Type.Missing,Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

      var worksheet = (Worksheet)workbook.Worksheets[1];
        var rowCount = worksheet.UsedRange.Rows.Count;
faux
  • 91
  • 1
  • 10
0

just try to look parameter values and compare with your parameter value

Its work for me

 xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@"E:\LIQ.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Hiren Patel
  • 125
  • 11