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

时间:2010-05-18 20:21:57

标签: c# .net excel office-interop

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

但是,第一次调用get_Range()时,Interop库会抛出异常。 (在拦截之后)

有谁知道我做错了什么?

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 \ n \ 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(String name,Object value):143行\ r \ n在C:\ Documents and Settings \ QueBITuser \ My Documents \ Visual Studio 2008 \ Projects \ JRE.WCF \ WorkbookTemplateManager \ EditTemplateForm.cs中的WorkbookTemplateManager.EditTemplateForm.SaveTemplate(Object sender,EventArgs args):第124行\ r \ n at系统.Windows.Forms.Control.OnClick(EventArgs e)\ r \ n在System.Windows.Forms.Button.OnClick(EventArgs e)\ r \ n在System.Windows.Forms.Button.OnMouseUp(MouseEve)在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32单击)\ r \ n在System.Windows.Forms.Control.WndProc(消息& m)\ r \ n在System.Windows.Forms.ButtonBase.WndProc(消息& m)\ r \ n在System.Windows.Fornd.WoutProc上的System.Windows.Forms.Button.WndProc(Message& m)\ r \ n,在System.Windows.Forms.Fortrol.Control.ControlNativeWindow上的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\ r \ n .WndProc(Message& m)\ r \ n在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)“

2 个答案:

答案 0 :(得分:1)

从一些环顾四周看,我发现了一些例子,其中字符串本身在Names.Add方法上有一个等号和一个显式引用。类似的东西:

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

答案 1 :(得分:0)

我也在挣扎。

我试图添加的名称是CEB04 ==>这指的是一个远离righ列的现有excel(2007)单元。因此,因为它是一个真正的地址,所以excel不允许它。

只需在“MyAppNames_”+“CEB04”之前添加一个前缀,就可以了。

顺便使用c#和.net4可以避免键入Type.Missing,因为它允许使用可选参数。

此代码段工作正常:

var name= "MyAppNames_" + "CEB04";
var address = "=" + Constants.ValidationSheetName + "!" + target.Address;
worksheet.Names.Add(name, address,true);