将DataGrid显示的内容导出到WROD中,并用表格形式打印?望:高手给出详解或留下联系方式。
我的QQ:249340686

解决方案 »

  1.   

    给你一段我的代码,慢慢研究 吧:
    '开始调用WORD 进行打印
    With objWord
      .Documents.Add
    End WithWith objWord.ActiveDocument.PageSetup
            .LineNumbering.Active = False
            .Orientation = wdOrientPortrait
            .TopMargin = CentimetersToPoints(1)
            .BottomMargin = CentimetersToPoints(1)
            .LeftMargin = CentimetersToPoints(1)
            .RightMargin = CentimetersToPoints(1)
            .Gutter = CentimetersToPoints(0)
            .HeaderDistance = CentimetersToPoints(0)
            .FooterDistance = CentimetersToPoints(0)
            .PageWidth = CentimetersToPoints(21.59)
            .PageHeight = CentimetersToPoints(27.94)
            .FirstPageTray = wdPrinterDefaultBin
            .OtherPagesTray = wdPrinterDefaultBin
            .SectionStart = wdSectionNewPage
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .VerticalAlignment = wdAlignVerticalTop
            .SuppressEndnotes = False
            .MirrorMargins = False
            .TwoPagesOnOne = False
            .GutterOnTop = False
            .CharsLine = 47
            .LinesPage = 43
            
     End WithWith objWord.ActiveWindow
         .Selection.TypeText Text:="                         中国矿业大学 水电费通知单 " & " [ " & Me.nian & "年" & Me.yue & "月 ] " & Chr(13)
         .Selection.TypeText Text:=Chr(13)
         '.Selection.TypeText Text:="——————————————————————————————————————————————-" & Chr(13)
         WindowState = wdWindowStateMaximize
      
      If .View.SplitSpecial = wdPaneNone Then
         .ActivePane.View.Type = wdNormalView
      End If
         
      If Not (Rstbm.EOF And Rstbm.BOF) Then
      Rstbm.MoveFirst
        Do While Not Rstbm.EOF
        objWord.Selection.TypeText Text:="NO." & Rstbm.Fields("类别编号") & "                       " & Rstbm.Fields("部门名称") & " [ " & Me.nian & "年" & Me.yue & "月 ] 水费通知单" & Chr(13)
      '  .Selection.TypeText Text:="| 帐号  |    帐号    |  上期水表  |  本期水表  |  水调整量  |水倍率| 本月水量 |水单价| 应交水费 |" & "" & Chr(13)
      '  .Selection.TypeText Text:="-------|---------|---------|----------|---------|------|---------|------|---------|" & Chr(13)
                
              Rsthz.Close
              Rsthz.Open "select *  from table_单位汇总表 where 单位帐号='" & Rstbm.Fields("类别编号") & "' order by ID", adobmdb, adOpenStatic, adLockReadOnly, adCmdText    
      

  2.   

    objWord.ActiveDocument.Tables.Add Range:=objWord.Selection.Range, NumRows:=1, NumColumns:=9
        objWord.Selection.TypeText Text:="帐号"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="户名"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="上期水表"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="本期水表"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="水调整量"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="水倍率"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="水量"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="水价"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="水费"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=12
        objWord.Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=85, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(6).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(7).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(8).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(9).SetWidth ColumnWidth:=60, RulerStyle:=wdAdjustNone
        
          
        
              If Not (Rsthz.EOF And Rsthz.BOF) Then
                Rsthz.MoveFirst
                  Do While Not Rsthz.EOF
                  
        objWord.Selection.InsertRows 1
        objWord.Selection.TypeText Text:=CStr(NullToString(Rsthz.Fields("ID")))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(NullToString(Rsthz.Fields("内部名称")))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("期初水表数"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("期末水表数"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("水表调整数"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("水倍率"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("本月水量"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Format(Rsthz.Fields("水价"), "###0.00"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("本月水费"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=12
                  
                  
             '     .Selection.TypeText Text:=Rsthz.Fields("ID") & "  " & Rsthz.Fields("内部名称") & Chr(13)
        
                  Rsthz.MoveNext
                  Loop
              End If
        
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=12
        'objWord.Selection.TypeText Text:=Chr(13)
               
        objWord.Selection.TypeText Text:="NO." & Rstbm.Fields("类别编号") & "                       " & Rstbm.Fields("部门名称") & " [ " & Me.nian & "年" & Me.yue & "月 ] 电费通知单" & Chr(13)    
        objWord.ActiveDocument.Tables.Add Range:=objWord.Selection.Range, NumRows:=1, NumColumns:=9
        objWord.Selection.TypeText Text:="帐号"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="户名"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="上期电表"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="本期电表"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="电调整量"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="电倍率"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="电量"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="电价"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:="电费"
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=12
        objWord.Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=85, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(6).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(7).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(8).SetWidth ColumnWidth:=50, RulerStyle:=wdAdjustNone
        objWord.Selection.Tables(1).Columns(9).SetWidth ColumnWidth:=60, RulerStyle:=wdAdjustNone
        
               If Not (Rsthz.EOF And Rsthz.BOF) Then
                Rsthz.MoveFirst
                  Do While Not Rsthz.EOF
                  
        objWord.Selection.InsertRows 1
        objWord.Selection.TypeText Text:=CStr(NullToString(Rsthz.Fields("ID")))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(NullToString(Rsthz.Fields("内部名称")))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("期初电表数"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("期末电表数"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("电表调整数"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("电倍率"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("本月电量"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Format(Rsthz.Fields("电价"), "###0.00"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.TypeText Text:=CStr(Rsthz.Fields("本月电费"))
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=12
                  
                  
             '     .Selection.TypeText Text:=Rsthz.Fields("ID") & "  " & Rsthz.Fields("内部名称") & Chr(13)
        
                  Rsthz.MoveNext
                  Loop
              End If
         
        If Not (Rsthz.EOF And Rsthz.BOF) Then
        Rsthz.MoveFirst
        End If
        
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
        objWord.Selection.MoveRight Unit:=wdCharacter, Count:=12
        objWord.Selection.TypeText Text:="应交费用:  = " & Format(NullToZeo(Rsthz.Fields("应交费")), "##,###.00") & "元" & Chr(13)
        objWord.Selection.TypeText Text:="———————————————— 剪 ———— 切 ———— 线 —————————————————-"
        objWord.Selection.TypeText Text:=Chr(13)
        objWord.Selection.TypeText Text:=Chr(13)
        
        
        
        Rstbm.MoveNext
        Loop
      End If
      
      Rsthz.Close
      Rstbm.Close
      End With
      

  3.   

    好像 导出到Excel比较容易些楼上的兄台 有直接打印datagrid里显示的内容的方法吗? 不通过datareport等报表工具