简体   繁体   English

打开具有c#完全访问权限的Excel工作表

[英]Opening Excel sheet with full access in c#

I am writing an application that has to write a value to a cell in an excel spreadsheet. 我正在编写一个应用程序,必须将值写入Excel电子表格中的单元格。 I am able to open the workbook, but it always opens in read-only mode. 我可以打开工作簿,但它始终以只读模式打开。 My code is 我的代码是

myExcelApp = new Excel.ApplicationClass();
myExcelApp.Visible = false;
Excel.Workbook myExcelWorkbooks = myExcelApp.Workbooks.Open(fileName, misValue, **false**, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);

Third argument is read-only which I have assigned as false. 第三个参数是只读的,我已将其指定为false。 But still myExcelWorkbooks.readOnly shows only True . 但仍然myExcelWorkbooks.readOnly只显示True

Can someone help me out of this? 有人可以帮我解决这个问题吗?

If you are on C# 4 or above, just do this: 如果您使用的是C#4或更高版本,请执行以下操作:

var myExcelApp = new Excel.Application()
    {
        Visible=false
    };

Excel.Workbook myExcelWorkbooks = myExcelApp.Workbooks.Open(Filename: @"F:\oo\bar.xlsx", ReadOnly: false);

Once you are done, make sure you do this: 完成后,请确保执行此操作:

myExcelWorkbooks.Save();
myExcelWorkbooks.Close();

myExcelApp.Quit();

Marshal.ReleaseComObject(myExcelApp);

If you haven't been doing the second part, open task manager and kill every instance of EXCEL.EXE (or restart your machine), and try the first part again. 如果您还没有执行第二部分,请打开任务管理器并终止EXCEL.EXE的每个实例(或重新启动计算机),然后再次尝试第一部分。

Excel will always open a workbook in read only mode if the Excel file is locked by another process. 如果Excel文件被另一个进程锁定,Excel将始终以只读模式打开工作簿。 If you have been messing about with interop, and not killing the Excel process afterwards, this could explain the behavior you are seeing. 如果你一直在搞乱互操作,而不是之后杀死Excel过程,这可以解释你所看到的行为。 Therefore your code and UAC settings may be fine, so I would get rid of any Excel instances running before changing your code further, or playing about with UAC. 因此,您的代码和UAC设置可能没问题,因此我会在进一步更改代码或使用UAC之前删除任何运行的Excel实例。

UAC may be interfering with it somehow. UAC可能会以某种方式干扰它。 Try launching either your app or Visual Studio as an administrator (from the context menu) 尝试以管理员身份启动您的应用或Visual Studio(从上下文菜单中)

Try use OpenXml, on codeplex there are a wrapper for simply iteracion. 尝试使用OpenXml,在codeplex上有一个简单迭代的包装器。

http://closedxml.codeplex.com/ http://closedxml.codeplex.com/

 xl = new Excel.Application();
 Excel.Workbooks books = xl.Workbooks;
 Excel.Workbook book = books.Open(filepath);

This always works for me, and it will be invisible by default and opened for read/write. 这对我来说总是有效,默认情况下它是不可见的,并且可以读/写。 You should use Excel.Application() rather than Excel.ApplicationClass() apparently but I doubt that is the route of your problem (I also can't find the article where I read that I'm afraid). 您应该使用Excel.Application()而不是Excel.ApplicationClass()但我怀疑这是您的问题的路径(我也找不到我读的文章,我害怕)。 Also notice how I split declaring the Workbooks and the Workbook object, again apparently that is best practice otherwise the process you launch won't always get closed. 还要注意我如何拆分声明工作簿和工作簿对象,再次显然这是最佳实践,否则您启动的过程将不会总是关闭。

I did a large project using Interop.excel. 我使用Interop.excel做了一个大型项目。 The pitfalls are horrible and when you start doing more advanced functions or processing large data sets it is extremely slow. 陷阱是可怕的,当你开始做更高级的功能或处理大型数据集时,它是非常缓慢的。

Try out OpenXML/ClosedXML, this will make your life a lot easier and your program architecture more scalable. 试用OpenXML / ClosedXML,这将使您的生活更轻松,您的程序架构更具可扩展性。

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

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