我写的一个类,你参考一下
'通过Excel打印报表(title():报表的主标题和附标题,msh:网格控件,filepath:excel模板文件的路径(空excel表),password:模板文件的密码,preview:是否要打印预 0:不预览)
Public Function Printing(title() As String, ByRef msh As Object, filepath As String, password As String, Optional preview As Long = 0) As Boolean
Dim printvalue() As String
Dim i As Long
Dim j As Long
Dim ex As New Excel.Application
Dim exwbook As Excel.Workbook
Dim exsheet As Excel.Worksheet
Dim gdate As New Showdate
On Error GoTo printErr
If Dir(filepath) = "" Then GoTo printErr
ReDim printvalue(msh.Rows, msh.cols)
'MsgBox msh.Rows & "," & msh.Cols
If Printers.count = 0 Then
MsgBox "打印机不存在,请安装打印机", , "提示"
Exit Function
End If
For i = 0 To msh.Rows - 1
For j = 0 To msh.cols - 1
printvalue(i, j) = msh.TextMatrix(i, j)
Next j
Next i
End If
exwbook.Close False
Set exwbook = Nothing
Set exsheet = Nothing
Set ex = Nothing
Printing = True
Exit Function
printErr:
MsgBox "打印出错,请检查Excel 2000及打印机是否能正常使用!", vbInformation, "提示"
Printing = False
Set exwbook = Nothing
Set exsheet = Nothing
Set ex = Nothing
End Function