在下文中,我试图在 excel 工作表中保留一组对象。每次调用该函数来存储一个值时,它应该分配A
列的下一个单元格来存储该对象。
但是,Interop 库在第一次调用get_Range()
. (就在 catch 块之后)
有谁知道我做错了什么?
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 在WorkbookTemplateManager.EditTemplateForm.AddName(String name, Object value) 在 C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:WorkbookTemplateManager 的第 143 行\r\n。 EditTemplateForm.SaveTemplate(Object sender, EventArgs args) 位于 System.Windows 的 C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:line 124\r\n 中。形式。Control.OnClick(EventArgs e)\r\n 在 System.Windows.Forms.Button.OnClick(EventArgs e)\r\n 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件)\r\n 在系统。 Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ButtonBase.WndProc( Message& m)\r\n 在 System.Windows.Forms.Button.WndProc(Message& m)\r\n 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n 在 System.Windows。 Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"OnMouseUp(MouseEventArgs 事件)\r\n 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons 按钮, Int32 点击)\r\n 在 System.Windows.Forms.Control.WndProc(Message& m)\r\n n 在 System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n 在 System.Windows.Forms.Button.WndProc(Message& m)\r\n 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& 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) "OnMouseUp(MouseEventArgs 事件)\r\n 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons 按钮, Int32 点击)\r\n 在 System.Windows.Forms.Control.WndProc(Message& m)\r\n n 在 System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n 在 System.Windows.Forms.Button.WndProc(Message& m)\r\n 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& 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) "Windows.Forms.Button.WndProc(Message& m)\r\n 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& 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)"Windows.Forms.Button.WndProc(Message& m)\r\n 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& 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)"