2

以下では、一連のオブジェクトを 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 type) で\r\n Microsoft.Office.Interop.Excel._Worksheet.get_Range(Object Cell1, Object Cell2) で\r\n WorkbookTemplateManager.EditTemplateForm.AddName(String name, Object value) in C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:line 143\r\n at WorkbookTemplateManager. System.Windows の C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:line 124\r\n の EditTemplateForm.SaveTemplate(Object sender, EventArgs args)。フォーム。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 ボタン, Int32 クリック)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ButtonBase.WndProc( Message& m)\r\n at System.Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.Control.ControlNativeWindow.OnMessage(Message& m)\r\n at 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 mevent)\r\n at System.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 at System.Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& m)\r\n at 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 mevent)\r\n at System.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 at System.Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) "Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) )\r\n at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) )\r\n at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"

4

2 に答える 2

1

周りを見回してみると、文字列自体にequalsがあり、Names.Addメソッドに明示的な参照がある例がいくつか見つかりました。何かのようなもの:

 jresheet.Names.Add("next", "=jreTemplates!$A$1",...
于 2010-05-19T17:34:17.443 に答える
0

私も苦労しました。

追加しようとしていた名前はCEB04でした==>これは、右側の列で遠く離れた既存の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);
于 2013-03-21T14:23:42.517 に答える