Private Sub excelout() '输出逗号分隔符文件 Dim headline As String Dim textline As String Dim rs As Recordset Dim adk As Adodc textout = "" headline = "" Set adk = DataGrid1.DataSource If adk Is Nothing Then Exit Sub Set rs = adk.Recordset a = rs.Fields.Count For i = 0 To a - 1 If TypeName(rs.Fields(i).Name) <> "String" Then b = Str(rs.Fields(i).Name) Else b = rs.Fields(i).Name End If headline = headline + b + "," Next i headline = headline + vbCrLf Do While adk.Recordset.EOF = False For i = 0 To a - 1 If IsNull(rs.Fields(i).Value) = True Then b = " " Else If TypeName(rs.Fields(i).Value) <> "String" Then b = Str(rs.Fields(i).Value) Else b = rs.Fields(i).Value End If End If textline = textline + b + "," Next i textline = textline + vbCrLf rs.MoveNext Loop textout = headline + textline On Error GoTo ErrHandler 保存到excel.Filter = "逗号分隔符文件(*.csv)|*.csv" 保存到excel.FilterIndex = 2 保存到excel.ShowSave Call csvsave(保存到excel.FileName,textout) Exit Sub rs.MoveFirst ErrHandler: '用户按“取消”按钮。 Exit Sub End Sub
Public Sub csvsave(filedir,textin) '保存写文件 Open filedir For Output As #1 Print #1,textin Close #1 End Sub