问题描述
我有一个Excel工作簿("test.xlsx"),其中有一个名为"gv_epxsize"的单元格.我的目标是打开excel工作簿,并在名称为"gv_epxsize"的单元格中写下字符串"101".
I have an Excel Workbook ("test.xlsx") that has a cell named "gv_epxsize". My goal is to open the excel workbook and write down the string "101" in the cell with the name "gv_epxsize".
问题是我的代码使文件保持只读状态,因此它不会将字符串"101"写入命名的单元格.我一直收到Windows提示,提示我已经使用Windows特权打开了文件,并询问是否要打开只读副本.问题是我没有打开文件,并且在启动代码之前没有运行EXCEL.EXE进程.
The problem is that my code keeps making the file Read Only, so it will not write the string "101" to the named cell. I keep receiving a windows prompt that I already have the file open with Windows privileges and asks if I want to open a Read Only copy. The problem is that I don't have the file open, and there is no EXCEL.EXE process running before I initiated the code.
我已经完成的事情:
- 我已将文件另存为其他文件夹中的文件-仍然没有运气.
- 该文件未保存在共享文件夹中.
- 在运行代码之前,我已经确保TaskManager已杀死所有EXCEL.EXE进程.
有人可以告诉我我在做什么错吗?
Can someone please show me what I'm doing wrong here?
这是C#的代码段
string filePath = "C:\\Users\\ussatdafa\\Desktop\\Work\\Projects\\test.xlsx";
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Excel has not been properly installed");
}
else
{
//string fileName = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), filePath);
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Workbooks wbs = excelApp.Workbooks;
Workbook wb = wbs.Open(filePath, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
wb.Names.Item("gv_epxsize").RefersToRange.Value = "101";
}
推荐答案
老实说,我不确定发生了什么,但是我有两个发现:
I'm honestly not sure what's going on, but I have two observations:
- 您要打开Excel两次-可以肯定.一个实例是
xlApp
,另一个实例是excelApp
.如果您在excelApp
的实例上打开任务管理器,我很确定您会找到两个正在运行的Excel实例.我不知道这是否有助于您解决问题 - 我总是发现在调试时使Excel可见很有帮助.您始终可以在部署之前注释掉该行,但是在调试过程中这很不错,因为在调试过程中暴露COM对象并不像本机.NET对象那么有用-这样,您就可以以本机形式查看它.
- You are opening Excel twice -- that appears to be certain. One instance is
xlApp
and the other isexcelApp
. If you open your task manager on the instantiation ofexcelApp
I'm pretty sure you will find two instances of Excel running. I have no idea if that's contributing to your issue or not - I always found it helpful, when debugging, to make Excel visible. You can always comment that line out before you deploy, but during debugging it's nice since exposing COM objects during debugging isn't as helpful as native .NET objects -- this way you can see it in its native form
我运行了具有这些更改的代码版本,并且没有更改命名范围"gv_epxsize"的单元格值的问题.
I ran a version of your code with these changes and had no issues changing the cell value of the named range "gv_epxsize."
string filePath = "C:\\Users\\ussatdafa\\Desktop\\Work\\Projects\\test.xlsx";
Microsoft.Office.Interop.Excel.Application excelApp =
new Microsoft.Office.Interop.Excel.Application();
if (excelApp == null)
{
MessageBox.Show("Excel has not been properly installed");
}
else
{
excelApp.Visible = true;
Workbook wb = excelApp.Workbooks.Open(filePath, 0, false, 5, "", "", false,
XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Worksheet ws = wb.Sheets[1];
wb.Names.Item("gv_epxsize").RefersToRange.Value = "101";
}
这对我来说是完美的.这是证明:
This worked flawlessly for me. Here is proof:
这使我想到了几种可能性:
So that leads me to several possibilities:
(Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
不是您想的那样.也许与我使用的是不一样的,
Isn't what you think it is. Or perhaps, it's not the same as what I used, which was:
Worksheet ws = wb.Sheets[1];
这将返回第一个工作表(在新工作簿上为"Sheet1").
Which will return the first worksheet ("Sheet1" on a new workbook).
当然,打开两个Excel可能会引起问题.
And of course, there is the possibility that having two Excels open is causing issues.
这篇关于C#Excel强制执行ReadOnly且没有Edit Priveledges的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!