具体操作如下:(注:首先需添加引用,选择COM-->选择Microsoft Word 10.0 Object Library和Microsoft Excel 10.0 Object Library组件) 1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。   Private Function CreaTable() As DataTable     Dim dt As New DataTable()     dt.Columns.Add("列1", GetType(String))     dt.Columns.Add("列2", GetType(Integer))     dt.Columns.Add("列3", GetType(String))     dt.Columns.Add("列4", GetType(String))     Dim row, row1 As DataRow     row = dt.NewRow()     row!列1 = "行1"     row!列2 = 1     row!列3 = "d"     row!列4 = "a"     dt.Rows.Add(row)     row1 = dt.NewRow()     row1!列1 = "行2"     row1!列2 = 12     row1!列3 = "b"     row1!列4 = "c"     dt.Rows.Add(row1)     Return dt   End Function 2.将表中的内容导出到Excel         Dim xlApp As New Excel.Application()         Dim xlBook As Excel.Workbook         Dim xlSheet As Excel.Worksheet         Dim rowIndex, colIndex As Integer         rowIndex = 1         colIndex = 0         xlBook = xlApp.Workbooks().Add         xlSheet = xlBook.Worksheets("sheet1")         Dim Table As New DataTable()         Table = CreaTable()         '将所得到的表的列名,赋值给单元格         Dim Col As DataColumn         Dim Row As DataRow         For Each Col In Table.Columns           colIndex = colIndex + 1           xlApp.Cells(1, colIndex) = Col.ColumnName         Next         '得到的表所有行,赋值给单元格         For Each Row In Table.Rows           rowIndex = rowIndex + 1           colIndex = 0           For Each Col In Table.Columns             colIndex = colIndex + 1             xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)           Next         Next         With xlSheet           .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体"           '设标题为黑体字           .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True           '标题字体加粗           .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1           '设表格边框样式         End With         With xlSheet.PageSetup           .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:"  ' & Gsmc           .CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"           .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"           .LeftFooter = "&""楷体_GB2312,常规""&10制表人:"           .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"           .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"         End With         xlApp.Visible = True 3.将表中的内容导出到WORD         Dim wordApp As New Word.Application()         Dim myDoc As Word.Document         Dim oTable As Word.Table         Dim rowIndex, colIndex As Integer         rowIndex = 1         colIndex = 0         wordApp.Documents.Add()         myDoc = wordApp.ActiveDocument         Dim Table As New DataTable()         Table = CreaTable()         oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, End:=0), NumRows:=Table.Rows.Count + 1, NumColumns:=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 

解决方案 »

  1.   

    try水晶报表的导出到word
    ----------------------------------------------------------------------Crystal Reports 提供了导出为几种不同格式的能力,包括 Adobe PDF 和 Microsoft Word。 导出为 PDF下面的示例演示如何将报表导出为 PDF 格式并将其发送到客户端机器。 注意   ASPNET 帐户必须具有写入将报表导出到的文件夹的权限。如果该文件夹没有至少设置更改权限,您将会收到一条错误消息,告诉您临时报表文件出错。出现这条错误消息是因为 ASPNET 帐户在导出时创建了一个临时报表文件,但是在试图将该报表文件写入到导出文件夹时由于权限不足而失败。手动创建的文件夹可能不会自动将完全控制赋予 ASPNET 帐户。
    [Visual Basic] 
    ' 定义 Crystal Reports 变量
    Dim crReportDocument As ReportDocument
    Dim crExportOptions As ExportOptions
    Dim crDiskFileDestinationOptions As DiskFileDestinationOptions
    Dim Fname as String' 以下代码可以直接放在窗体的构造函数中对 
    '  InitializeComponent() 的调用之后,或者放在 ,
    '  Button_Click 事件内部,在该事件中,客户端使用该按钮
    '  来获取报表的可打印副本。crReportDocument = New ReportDocument()
    ' 以下代码行加载 
    '  示例报表“Chart.rpt”
    crReportDocument.Load ("C:\Program Files\Microsoft Visual Studio .NET 2003\Crystal Reports\Samples\Reports\Feature Examples\Chart.rpt")Fname = "c:\exports\" & Session.SessionID.ToString & ".pdf"
    crDiskFileDestinationOptions = New DiskFileDestinationOptions()
    CrDiskFileDestinationOptions.DiskFileName = Fname
    crExportOptions = crReportDocument.ExportOptions
    With crExportOptions
       .DestinationOptions = crDiskFileDestinationOptions
       .ExportDestinationType = ExportDestinationType.DiskFile
       .ExportFormatType = ExportFormatType.PortableDocFormat
    End With
    crReportDocument.Export()
    ' 以下代码将 pdf 文件写入 
    '  客户端浏览器。
    Response.ClearContent()
    Response.ClearHeaders()
    Response.ContentType = "application/pdf"
    Response.WriteFile(Fname)
    Response.Flush()
    Response.Close()' 从磁盘删除导出的文件
    System.IO.File.Delete(Fname)
      

  2.   

    会出现这种错误,不知道怎么搞定--ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。