小宇,你的问题。。。
和尚已经看了。。。
你要搞清楚,你现在的database是需要分类的。如果是随便key in一个字母进去,而一次过读完所需要资料,那么就很简单。。
可是,你的老板的要求不是,是需要已经要分类好的资料而已。这叫做DataGrid,是需要分类的。
因此,你先搞清楚你的database。
如果单单只是叫一个顾客的全部资料,而不需要分类的,那么就简单很多。
给以一个例子:
在FORM的LOAD事件中加入:
Data1.DatabaseName = 数据库名称
Data1.RecordSource = 表名
Data1.Refresh
在按的CLICK把database加入中
Dim Irow, Icol As Integer
Dim Irowcount, Icolcount As Integer
Dim Fieldlen() "
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject(“Excel.Application”)
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
With Data1.Recordset.MoveLast
If .RecordCount < 1 Then
MsgBox (“Error Not record!”)
Exit Sub
End If
Irowcount = .RecordCount "
Icolcount = .Fields.Count "
ReDim Fieldlen(Icolcount).MoveFirst
For Irow = 1 To Irowcount + 1
For Icol = 1 To Icolcount
Select Case Irow
Case 1 "
xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
Case 2 "FIELDLEN()
If IsNull(.Fields(Icol - 1)) = True Then
Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
Else
Fieldlen(Icol) = LenB(.Fields(Icol - 1))
End If
xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
Case Else
Fieldlen1 = LenB(.Fields(Icol - 1))
If Fieldlen(Icol) < Fieldlen1 Then
xlSheet.Columns(Icol).ColumnWidth = Fieldlen1
Fieldlen(Icol) = Fieldlen1
Else
xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
End If
xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
End Select
Next
If Irow <> 1 Then
If Not .EOF Then .MoveNext
End If
Next
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = “Bold”
"
.Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
.Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous
End With
xlApp.Visible = True
xlBook.Save
Set xlApp = Nothing
End With
你仔细看,在click 加入database中,有很多种分类data,例如:Irowrowcount Fieldle xlApp xlBoo xlSheexlAppxlBook xlSheelS。那就是要你要重点了。。。