说明 下面的代码不是我写的 是我在前几天逛CSDN的时候 看见就贴下来了 对不起 我真忘记了 那位大侠的名字了 记得至少两个猩猩 楼主你用吧 给不给分无所谓 只要你以后成了高手也多多解答别人的问题就好了参考代码: Sub print_grid(Grd As MSFlexGrid, MainTitle As String, SecTitle As String, PageLine As Integer, DjCol As Integer, JeCol As Integer) Dim x0 As Single, y0 As Single Dim x As Single, y As Single Dim fs As String * 10 Dim zje As Currency, yje As Currency Dim HzNum As Integer Dim n As Integer, r As Integer Dim dx(0 To 20) As Integer Dim Cellf(0 To 20) As Single Dim str1 As String, str2 As String, str3 As String, str4 As String Dim I As Integer, j As Integer, k As Integer 'Load printer 'printer.Show If Grd.Rows <= 1 Then MsgBox "本表格没有数据,没有必要打印!", 48, "提示" Exit Sub End If x0 = 10 '打印边界 y0 = 20 With Grd '计算栏空 dx(i) .Row = 1 w = .Width For I = .Cols - 1 To 1 Step -1 .Col = I dx(I) = (.CellWidth + 72) / 56.7 / 4.233 w = w - (.CellWidth + 72) Next I dx(0) = w / 4.233 / 56.7 '计算 str1 到 str4 str1 = "┌" str2 = "├" str3 = "│" str4 = "└" HzNum = 1 '汉字总数 For I = 0 To .Cols - 1 Cellf(I) = HzNum * 4.233 '计算每栏起始打印位置 For j = 1 To dx(I) str1 = str1 + "─" str2 = str2 + "─" str3 = str3 + " " str4 = str4 + "─" Next j HzNum = HzNum + dx(I) + 1 If I < .Cols - 1 Then str1 = str1 + "┬" str2 = str2 + "┼" str3 = str3 + "│" str4 = str4 + "┴" Else str1 = str1 + "┐" str2 = str2 + "┤" str3 = str3 + "│" str4 = str4 + "┘" End If Next IPrinter.ScaleMode = 6 '毫米为单位 Printer.Width = 210 * 56.7 '窄行打印纸大小 Printer.Height = 297 * 56.7 k = 1 n = 0 '当前页数 zje = 0 '总计金额r = Int((.Rows + PageLine - 2) / PageLine) '总页数 While k <= .Rows - 1 yje = 0 '页小计金额 ' printer.FontName = "楷书" Printer.FontName = "宋体" Printer.FontBold = True Printer.FontSize = 18 x = x0 + (HzNum * 4.233 - Len(MainTitle) * 6.46) / 2 y = y0 Printer.CurrentX = x Printer.CurrentY = y Printer.Print MainTitle '打印主标题 Printer.FontBold = False Printer.FontName = "宋体" Printer.FontSize = 12 y = Printer.CurrentY + 3 x = x0 Printer.CurrentX = x Printer.CurrentY = y Printer.Print SecTitle '打印次标题 Printer.CurrentX = x + HzNum * 4.233 - 50 Printer.CurrentY = y Printer.Print Format$(Date, "yyyy-mm-dd") ''打印日期 y = Printer.CurrentY + 2 Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str1 '打印┌───┬──┬─┐ y = Printer.CurrentY Printer.CurrentX = x0 Printer.Print str3 '打印│ │ │ │ For j = 0 To .Cols - 1 '打印表头内容 '表头内容位于单元中间 Printer.CurrentX = x0 + Cellf(j) + (dx(j) - Len(.TextMatrix(0, j))) / 2 * 4.233 Printer.CurrentY = y Printer.Print .TextMatrix(0, j) Next j For I = 1 To PageLine If k = .Rows Then Exit For '表格结束 y = Printer.CurrentY Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str2 '打印├───┼──┼─┤ y = Printer.CurrentY Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str3 '打印│ │ │ │ For j = 0 To .Cols - 1 '打印一行的内容 Printer.CurrentX = x0 + Cellf(j) Printer.CurrentY = y If j = DjCol Then fs = " " Mid$(fs, 7 - Len(Format$(.TextMatrix(k, j), "##0.00")), Len(Format$(.TextMatrix(k, j), "##0.00"))) = Format$(.TextMatrix(k, j), "##0.00") Printer.Print Mid$(fs, 1, 6) ElseIf j = JeCol Then fs = " " Mid$(fs, 10 - Len(Format$(.TextMatrix(k, j), "####0.00")), Len(Format$(.TextMatrix(k, j), "####0.00"))) = Format$(.TextMatrix(k, j), "####0.00") Printer.Print fs yje = yje + Val(.TextMatrix(k, j)) zje = zje + Val(.TextMatrix(k, j)) Else Printer.Print .TextMatrix(k, j) End If Next j k = k + 1 Next I n = n + 1 y = Printer.CurrentY Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str4 '打印└──┴──┴──┘ If zje <> 0 Then Printer.CurrentX = x0 + 80 Printer.Print "本页小计金额:" + Format$(yje, "######0.00") + "元" End If Printer.CurrentX = x0 + 90 Printer.Print "第" + Str$(n) + "页,共" + Str$(r) + "页" If k < .Rows - 1 Then Printer.NewPage Else Printer.CurrentX = x0 + 80 Printer.Print "总计金额:" + Format$(zje, "######0.00") + "元" End If Wend Printer.EndDoc End With End Sub
对不起 我真忘记了 那位大侠的名字了 记得至少两个猩猩
楼主你用吧 给不给分无所谓 只要你以后成了高手也多多解答别人的问题就好了参考代码:
Sub print_grid(Grd As MSFlexGrid, MainTitle As String, SecTitle As String, PageLine As Integer, DjCol As Integer, JeCol As Integer)
Dim x0 As Single, y0 As Single
Dim x As Single, y As Single
Dim fs As String * 10
Dim zje As Currency, yje As Currency
Dim HzNum As Integer
Dim n As Integer, r As Integer Dim dx(0 To 20) As Integer
Dim Cellf(0 To 20) As Single Dim str1 As String, str2 As String, str3 As String, str4 As String
Dim I As Integer, j As Integer, k As Integer
'Load printer
'printer.Show
If Grd.Rows <= 1 Then
MsgBox "本表格没有数据,没有必要打印!", 48, "提示"
Exit Sub
End If
x0 = 10 '打印边界
y0 = 20
With Grd
'计算栏空 dx(i)
.Row = 1
w = .Width
For I = .Cols - 1 To 1 Step -1
.Col = I
dx(I) = (.CellWidth + 72) / 56.7 / 4.233
w = w - (.CellWidth + 72)
Next I
dx(0) = w / 4.233 / 56.7
'计算 str1 到 str4
str1 = "┌"
str2 = "├"
str3 = "│"
str4 = "└"
HzNum = 1 '汉字总数
For I = 0 To .Cols - 1
Cellf(I) = HzNum * 4.233 '计算每栏起始打印位置
For j = 1 To dx(I)
str1 = str1 + "─"
str2 = str2 + "─"
str3 = str3 + " "
str4 = str4 + "─" Next j
HzNum = HzNum + dx(I) + 1
If I < .Cols - 1 Then
str1 = str1 + "┬"
str2 = str2 + "┼"
str3 = str3 + "│"
str4 = str4 + "┴"
Else
str1 = str1 + "┐"
str2 = str2 + "┤"
str3 = str3 + "│"
str4 = str4 + "┘"
End If
Next IPrinter.ScaleMode = 6 '毫米为单位
Printer.Width = 210 * 56.7 '窄行打印纸大小
Printer.Height = 297 * 56.7
k = 1
n = 0 '当前页数
zje = 0 '总计金额r = Int((.Rows + PageLine - 2) / PageLine) '总页数
While k <= .Rows - 1
yje = 0 '页小计金额
' printer.FontName = "楷书"
Printer.FontName = "宋体" Printer.FontBold = True
Printer.FontSize = 18
x = x0 + (HzNum * 4.233 - Len(MainTitle) * 6.46) / 2
y = y0
Printer.CurrentX = x
Printer.CurrentY = y
Printer.Print MainTitle '打印主标题 Printer.FontBold = False
Printer.FontName = "宋体"
Printer.FontSize = 12
y = Printer.CurrentY + 3
x = x0
Printer.CurrentX = x
Printer.CurrentY = y
Printer.Print SecTitle '打印次标题
Printer.CurrentX = x + HzNum * 4.233 - 50
Printer.CurrentY = y
Printer.Print Format$(Date, "yyyy-mm-dd") ''打印日期
y = Printer.CurrentY + 2
Printer.CurrentX = x0
Printer.CurrentY = y
Printer.Print str1 '打印┌───┬──┬─┐ y = Printer.CurrentY
Printer.CurrentX = x0
Printer.Print str3 '打印│ │ │ │
For j = 0 To .Cols - 1 '打印表头内容
'表头内容位于单元中间
Printer.CurrentX = x0 + Cellf(j) + (dx(j) - Len(.TextMatrix(0, j))) / 2 * 4.233
Printer.CurrentY = y
Printer.Print .TextMatrix(0, j)
Next j For I = 1 To PageLine
If k = .Rows Then Exit For '表格结束
y = Printer.CurrentY
Printer.CurrentX = x0
Printer.CurrentY = y
Printer.Print str2 '打印├───┼──┼─┤ y = Printer.CurrentY
Printer.CurrentX = x0
Printer.CurrentY = y
Printer.Print str3 '打印│ │ │ │ For j = 0 To .Cols - 1 '打印一行的内容
Printer.CurrentX = x0 + Cellf(j)
Printer.CurrentY = y
If j = DjCol Then
fs = " "
Mid$(fs, 7 - Len(Format$(.TextMatrix(k, j), "##0.00")), Len(Format$(.TextMatrix(k, j), "##0.00"))) = Format$(.TextMatrix(k, j), "##0.00")
Printer.Print Mid$(fs, 1, 6)
ElseIf j = JeCol Then
fs = " "
Mid$(fs, 10 - Len(Format$(.TextMatrix(k, j), "####0.00")), Len(Format$(.TextMatrix(k, j), "####0.00"))) = Format$(.TextMatrix(k, j), "####0.00")
Printer.Print fs
yje = yje + Val(.TextMatrix(k, j))
zje = zje + Val(.TextMatrix(k, j))
Else
Printer.Print .TextMatrix(k, j)
End If
Next j
k = k + 1
Next I
n = n + 1
y = Printer.CurrentY
Printer.CurrentX = x0
Printer.CurrentY = y
Printer.Print str4 '打印└──┴──┴──┘
If zje <> 0 Then
Printer.CurrentX = x0 + 80
Printer.Print "本页小计金额:" + Format$(yje, "######0.00") + "元"
End If
Printer.CurrentX = x0 + 90
Printer.Print "第" + Str$(n) + "页,共" + Str$(r) + "页"
If k < .Rows - 1 Then
Printer.NewPage
Else
Printer.CurrentX = x0 + 80
Printer.Print "总计金额:" + Format$(zje, "######0.00") + "元"
End If
Wend
Printer.EndDoc
End With
End Sub