VB通过Excel打印程序怎么写

来源:百度知道 编辑:UC知道 时间:2024/09/23 20:50:02
我在VB6.0中引用了Microsoft Excel11.0 object library,我按打印这个COMMAND,然后就在EXCEL里输出我DATAGRID中的数据,这个程序要怎么编

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet.Name = "Sheet1" Then
Cancel = True
End If
End Sub

"Sheet1"这里是你要限制的表名

打印到Excel,导出数据到Excel,表体部分只是参考,
strSource = App.Path & "\U8Cus.xls"
strDestiantion = App.Path & "\temp1.xls"
If Dir(strDestiantion) <> "" Then
Kill (strDestiantion) '删除文件时要判断文件是否已打开
FileCopy strSource, strDestiantion
Else '在复制前,查询是否有此文件,有则删除后才复制
FileCopy strSource, strDestiantion
End If
mobjExcel.Visible = True
Set mobjWorkBook = mobjExcel.Workbooks.Open(strDestiantion)
Set xlSheet = mobjWorkBook.Worksheets(1)

'表体部分
Dim i As Double, j As Double, x As Integer, y As Integer
x = rs.RecordCount '记录条总数
if x=0 then End
y = rs.Fields.Count '记录列数
mobjExcel.Application.C