C#如何获取EXCEL中TextBox的值

saptechnique 2009-09-23 10:51:06
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
...全文
571 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
美人心计1999 2009-09-24
  • 打赏
  • 举报
回复
学习学习
tengye19840704 2009-09-23
  • 打赏
  • 举报
回复
顶下.学习/
wuyq11 2009-09-23
  • 打赏
  • 举报
回复
一般
public void WriteCell(Excel._Worksheet Sheet,object objCell, object objValue)
{
Sheet.get_Range(objCell, mValue).Value2 = objValue;
}
wuyq11 2009-09-23
  • 打赏
  • 举报
回复
Excel.Application app = new Excel.ApplicationClass();
Excel.Workbook workBook = app.Workbooks._Open(templetFile, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);
sheet.Name = "";

Excel.TextBox txt = (Excel.TextBox)sheet.TextBoxes("txtD");
txt.Text = "";

ArithmeticUnit 2009-09-23
  • 打赏
  • 举报
回复
学习了!
saptechnique 2009-09-23
  • 打赏
  • 举报
回复
顶起,大家帮忙!

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧