在VB.NET中编写输出Word和Excel模块时候出错

tianshilei1992 2012-10-13 09:27:41
输出Word报表模块代码如下:

Private Sub 输出Word格式WToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 输出Word格式WToolStripMenuItem.Click
Dim wordApp As New Word.Application
Dim myDoc As Word.Document
Dim oTable As Word.Table
Dim rowIndex, colIndex As Integer
wordApp.Documents.Add()
myDoc = wordApp.ActiveDocument
rowIndex = 1
colIndex = 0
Dim Table As New DataTable
Table = objDataSetOutput.Tables(0)

oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, [End]:=0), NumColumns:=Table.Rows.Count + 1, NumRows:=Table.Columns.Count)

'将所得到的表的列明赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
Next

'将所得到的表所有行赋值给单元格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next

oTable.Borders.InsideLineStyle = 1
oTable.Borders.OutsideLineStyle = 1
wordApp.Visible = True

objDataSetOutput = Nothing
End Sub

当点击按钮时候,程序运行出错,错误提示:
未处理的COMException:oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)集合要求的成员不存在
调试窗口输出内容:


在 System.Runtime.InteropServices.COMException 中第一次偶然出现的“PhoneStore.exe”类型的异常
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>未处理的异常</Description><AppDomain>PhoneStore.vshost.exe</AppDomain><Exception><ExceptionType>System.Runtime.InteropServices.COMException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>集合所要求的成员不存在。</Message><StackTrace> 在 Microsoft.Office.Interop.Word.Table.Cell(Int32 Row, Int32 Column)
在 PhoneStore.MainWnd.输出Word格式WToolStripMenuItem_Click(Object sender, EventArgs e) 位置 E:\PhoneStore\GUI\VB.NET\PhoneStore\MainWnd.vb:行号 544
在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStrip.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStripDropDown.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr 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)
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
在 PhoneStore.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.Runtime.InteropServices.COMException (0x800A1735): 集合所要求的成员不存在。
在 Microsoft.Office.Interop.Word.Table.Cell(Int32 Row, Int32 Column)
在 PhoneStore.MainWnd.输出Word格式WToolStripMenuItem_Click(Object sender, EventArgs e) 位置 E:\PhoneStore\GUI\VB.NET\PhoneStore\MainWnd.vb:行号 544
在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStrip.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStripDropDown.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr 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)
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
在 PhoneStore.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord>

整个模块的代码是在书上抄的,不知道怎么到我的机器上就有问题……

上面代码中,objDataSetOutput是一个Public类型的DataSet对象,是我点击查询按钮以后将数据Fill到这个Set中去的,该不会是这个是NULL引起的吧……
...全文
312 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tom000443 2013-12-02
  • 打赏
  • 举报
回复
我也出现这个问题!! 无解~~资料太少了
tianshilei1992 2012-10-13
  • 打赏
  • 举报
回复
人工置顶啊……
Anycell Report(简称AC Report)是一款国式报表组件,是国内最早的基于表格,支持图文混排、公式和脚本的国式报表工具之一。就如Anycell Report的名称那样,灵活强大的表格功能一直是AC Report区别于其它软件或控件最显著的特征之一,AC Report 表格取消了传统表格概念“列”的概念,每一行上的单元格数量可以不等,且可以自由活动,调整某行上单元格的宽度时,不影响其它行的单元格。在制作复杂的国式报表时可以避免很多不必要的合并拆分操作,制作表格更加方便和随心所欲,并且省时省力。AC Report单元格支持多种丰富的形态,例如格式化文本、图片、图表、条码、OLE容器等。   AC Report的一些基本特点:   1.独具特色的表格,风格与Word表格相似,但可以做出比WordExcel更灵活的表格来。   2. 功能全面、专业的国式报表设计器,国用户更易于学习和接受。   3.支持多种单元格样式,可以打印图像、图表(直方图、折线图等)、Rich文本、条形码、式财务帐薄、支持在报表嵌入WordExcel文档等。   4. 强大的计算和合计功能。内置表达式解析系统和函数库。   5. 可扩充性,可以在应用程序给报表引擎扩充函数库、报表样式和单元格样式。   6.支持多种报表样式,如清单式、分组、交叉表、以及子报表等。   7. 支持多栏式报表。   8. 和应用程序完美结合,支持windows下所有的开发工具和程序语言(例如Delphi、C++、VB、PB、.NET、易语言),最终用户在设计器里可直接选择打印字段,生成表达式,报表设计器用户容易学习理解。   9. 支持脚本和窗体编程,报表设计人员可以编写脚本、在报表设计器里为报表添加窗体,为最终用户提供更丰富的交互功能。   10. 既可以使用应用程序的数据集,也支持在报表直接连接各种数据库,通过SQL直接获得报表需要的数据。   11.无失真导出为ExcelWord、Html格式的文档。   Ver2.45.008 bug修正:   1.当计算公式出错导致生成报表失败,退出调用AC Report的程序时发生内存错误。   2.含有子报表且子报表的列数大于主报表,不能导出到Excel的问题。   3.和Skin皮肤控件冲突的问题(主要是导出Excel后报错)。   Ver2.45主要变动1.修正了独立子报表(未嵌入到所属页面的子报表)预览后退出程序时,发生内存泄漏的bug。   2.图片单元格支持在公式设置URL链接地址。   3.导出Excel功能改进。   4.Cell()函数空值处理。   5.界面修改。   6.修正VCL控件和demo编译出错的问题。   Ver 2.4 主要变动:   1.可拖动对象功能增强(现在AC不仅可以支持强大的表格功能,也可以使用AC Report 像传统的报表工具例如FastReport或水晶报表那样设计报表了)。   2.在新建页面时,增加了“数据模块”类型,用来存放报表的数据控件,更方便模板的管理。   3.对话框(Form)功能增强。   4.支持双面打印。   5.其它:   财务帐薄支持套打。   单元格支持批量拖动。   设计器界面优化。   相关bug修正。   新功能相关的Demo。   Ver2.45主要变动:   1.修正了独立子报表内存泄漏的bug。   2.图片单元格支持在公式设置URL链接地址。   3.导出Excel功能改进。   4.Cell()函数空值处理。   5.界面修改。   6.修正VCL控件和demo编译出错的问题。   安装包含使用和开发接口文档,及Delphi、VB、VC++、C#.net、VB.net和WEB下的demo。

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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