c# - 错误! (使用来自 C# 的 Excel 命名范围)

标签 c# .net excel office-interop

在下文中,我试图在 Excel 工作表中保留一组对象。每次调用该函数来存储一个值时,它应该分配 A 列的下一个单元格来存储该对象。

但是,Interop 库在第一次调用 get_Range() 时抛出异常。 (就在 catch block 之后)

有人知道我做错了什么吗?

private void AddName(string name, object value)
        {
            Excel.Worksheet jresheet;
            try
            {
                jresheet = (Excel.Worksheet)_app.ActiveWorkbook.Sheets["jreTemplates"];
            }
            catch
            {
                jresheet = (Excel.Worksheet)_app.ActiveWorkbook.Sheets.Add(Type.Missing, Type.Missing, 
                                                                       Type.Missing, Type.Missing);
                jresheet.Visible = Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVeryHidden;
                jresheet.Name = "jreTemplates";
                jresheet.Names.Add("next", "A1", true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
            Excel.Range cell = jresheet.get_Range("next", Type.Missing);
            cell.Value2 = value;
            string address = ((Excel.Name)cell.Name).Name;
            _app.ActiveWorkbook.Names.Add(name, address, false,
                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            cell = cell.get_Offset(1, 0);
            jresheet.Names.Add("next", ((Excel.Name)cell.Name).Name, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }

由于异常是从 COM 库中抛出的,嵌入的信息似乎不是很有用。然而,这里是:

"Exception from HRESULT: 0x800A03EC"

"\r\n服务器堆栈跟踪:\r\n\r\n\r\n在 [0] 处重新抛出异常:\r\n 在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n 在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 类型)\r\n 在 Microsoft.Office.Interop.Excel._Worksheet.get_Range(Object Cell1, Object Cell2)\r\n 在 C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs 中的 WorkbookTemplateManager.EditTemplateForm.AddName(字符串名称,对象值):第 143 行\r\n在 C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:line 124\r\n 在系统.Windows.Forms.Control.OnClick(EventArgs e)\r\n 在 System.Windows.Forms.Button.OnClick(EventArgs e)\r\n 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n 在 System.Windows.Forms.Control.WndProc(Message& m)\r\n 在 System.Windows.Forms .ButtonBase.WndProc(消息& m)\r\n 在 System.Windows.Forms.Button.WndProc(消息& m)\r\n 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m)\r\n在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"

最佳答案

通过环顾四周,我发现了一些示例,其中字符串本身有一个等号和对 Names.Add 方法的显式引用。像这样的东西:

 jresheet.Names.Add("next", "=jreTemplates!$A$1",...

关于c# - 错误! (使用来自 C# 的 Excel 命名范围),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2860832/

相关文章:

excel - 粘贴到Excel文件时如何保留前导零?

c# - WCF Soap 客户端无法解析签名中的 URI 以计算摘要

c# - 为什么 '+' + 短转换为 44

C# 单元测试 NSubstitute 无法在 ObjectCache 中设置值

c# - 带字符串键的 GetHashCode()

excel - 多个模块和子程序之间的错误处理

c# - 通过 AssociatedControlID 从文本框访问标签

.net - 文件有多个属性时如何判断文件是否隐藏

c# - 运行 .NET 3.5 进程时,.NET 4 和 .NET 3.5 之间是否存在差异

excel - 复制事件行并在下面插入,即使使用事件过滤器