简体   繁体   English

C#-未处理COMexception-无法访问只读文档?

[英]C# - COMexception unhandled - Cannot access read-only document?

I'm new to C# and trying to accomplish some simple excel manipulation through the interop library. 我是C#的新手,正在尝试通过互操作库完成一些简单的excel操作。

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. 删除行后,出现错误“未处理COMexception-无法访问只读文档-test1.xlsx”,并且我尝试关闭工作簿。

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. 编辑-我最终添加了一个Workbook.Save()函数,并使用了Application对象的Quit()函数。 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();

        }

I ended up adding a Workbook.Save() function and using the Application object's Quit() function. 我最终添加了一个Workbook.Save()函数,并使用了Application对象的Quit()函数。 This question was also very informative ( similar question ). 这个问题也很有参考价值( 类似的问题 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM