Grid++Report 怎么将VB DataGrid1里的数据显示出来啊。

2025-06-26 04:46:26
推荐回答(1个)
回答1:

打印的话 首先要设置好表格模版!
具体的一两句也说不清楚。
下面是个小例子,仔细研究下吧。

Private Sub cmdReport_Click()

On Error GoTo errExcel

Dim i As Integer, j As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim File1, File2 As String

File1 = App.Path & "\报表\模板报表\模板.xls"
File2 = App.Path & "\报表\明细表.xls"
FileCopy File1, File2

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(File2)
Set xlSheet = xlBook.Worksheets(1)

Dim reUnit As Recordset
Set reUnit = dbYY.OpenRecordset("SELECT unit FROM admin")
xlSheet.Cells(4, 1) = "编制单位:" & reUnit(0)
reUnit.Close

Prog1.Min = 0
Prog1.Max = Grid1.Rows * 2
Prog1.Visible = True

For i = 1 To Grid1.Rows - 1
Grid1.Row = i
For j = 1 To 10
Grid1.Col = j
If IsNull(Grid1.Text) = False Then
xlSheet.Cells(i + 5, j) = Grid1.Text
If j = 10 Then xlSheet.Cells(i + 5, 10) = Format(Grid1.Text, "yyyy-mm-dd")
End If
Next
Next

xlSheet.Cells(i + 5, 2) = "制单:" & Czy
xlSheet.Cells(i + 5, 4) = "审核:"
xlSheet.Cells(i + 5, 9) = "打印日期:"
xlSheet.Cells(i + 5, 10) = Format(Date, "yyyy-mm-dd")

For i = 2 To Grid1.Rows - 2
Prog1.Value = Prog1.Value + 1
For j = 1 To 10
xlSheet.Cells(i + 4, j).Select
xlSheet.Cells(i + 4, j).Borders(xlDiagonalDown).LineStyle = xlNone
xlSheet.Cells(i + 4, j).Borders(xlDiagonalUp).LineStyle = xlNone
With xlSheet.Cells(i + 4, j).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Cells(i + 4, j).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Cells(i + 4, j).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Cells(i + 4, j).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Next
Next

Prog1.Visible = False
xlApp.Visible = True
xlBook.Save

Exit Sub
errExcel:
If Err.Number = 70 Then
MsgBox "请关闭Excel报表文件,再生成!", vbInformation, "系统提示"
Else
MsgBox Err.Description, vbInformation, "系统出错"
End If
End Sub