0

I'm new to C# and trying to accomplish some simple excel manipulation through the interop library.

I'd like to delete a text value which always appears as a separate line below the actual data table (with a blank row between the table and the text). It's a 'rows selected.' count which prints out in an automated report. It always appears in the first column.

The error "COMexception unhandled - Cannot access read-only document - test1.xlsx" appears after the row is deleted and I try to Close() the workbook.

I cannot seem to release the workbook, I've tried a lot of random garbage collection methods found on other forums, but has anyone seen this before?

Appreciate the help!

EDIT - So the file isn't read-only until I start running the program. Once I run the program it becomes locked and read-only. It isn't closing/releasing the workbook...

EDIT - I ended up adding a Workbook.Save() function and using the Application object's Quit() function. This question was also very informative (similar question).

        public void ExcelManip(string thisFileName)
        {
            Workbook workBook = _excelApp.Workbooks.Open(thisFileName,
                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);

            DeleteRowCount(ref workBook);

            workBook.Save();

            workBook.Close(false, Type.Missing, Type.Missing);
            _excelApp.Application.Quit();
            _excelApp.Quit();

            //workBook.Close(true, thisFileName);//ERROR;Cannot access read-only document
            //Marshal.ReleaseComObject(workBook);
            //GC.Collect();
            //GC.WaitForPendingFinalizers();

        }
Community
  • 1
  • 1
sm1
  • 13
  • 1
  • 5
  • Trying to delete rows from a read-only spreadsheet is a waste of your time, it isn't going to work. It is read-only. This should not mystify anybody. – Hans Passant Jun 01 '13 at 15:37
  • It isn't a read-only workbook until I start running the program. The program starts editing the workbook then isn't performing the Close() function properly. – sm1 Jun 01 '13 at 15:55

1 Answers1

1

I ended up adding a Workbook.Save() function and using the Application object's Quit() function. This question was also very informative (similar question).

Community
  • 1
  • 1
sm1
  • 13
  • 1
  • 5