C#如何获取EXCEL中TextBox的值
C#操作Excel是很方便的。可以很轻松地实现基于Cell的设置与读取的操作。
但是,在Excel中建立了TextBox,却不知道该如何操作了。
我在Excel中创建了一个TextBox1,然后输入一些值如MyTextBox。
在C#中使用如下代码:
Excel.TextBox tb=(Excel.TextBox)ws.TextBoxes("TextBox1");
MessageBox.Show(tb.Text);
但是在执行到第一行时出现错误:
未处理 System.Runtime.InteropServices.COMException
Message="异常来自 HRESULT:0x800A03EC"
Source="Microsoft.Office.Interop.Excel"
ErrorCode=-2146827284
不知道这是什么原因呢?
请高手帮忙看看是怎么回事吧,谢谢先!
附:错误的详细信息
StackTrace:
在 Microsoft.Office.Interop.Excel._Worksheet.TextBoxes(Object Index)
在 ExcelDataImport.Form3.button2_Click(Object sender, EventArgs e) 位置 E:\guofu\ExcelDataImport\ExcelDataImport\Form3.cs:行号 69
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 ExcelDataImport.Program.Main() 位置 E:\guofu\ExcelDataImport\ExcelDataImport\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/iamsyu/archive/2009/05/06/4154464.aspx