**************************************
' 过程名: dB_RsToCSVFile
' 功能:数据库导入到 CSV 格式文件
'**************************************
Sub dB_RsToCSVFile(Rs As ADODB.Recordset, FileName As String, Optional Delimiter As String = ",")
Dim fh As Integer
Dim FileIsOpen As Boolean, s As Variant
Dim t As Integer
Dim Buf As String, TempStr As String
FileIsOpen = False
On Error Goto Err_Out
fh = FreeFile()
Open FileName For Output As fh
FileIsOpen = True
Buf = ""
For t = 0 To Rs.Fields.Count - 1
If Buf = "" Then
Buf = """" & Rs.Fields(t).Name & """"
Else
Buf = Buf & Delimiter & """" & Rs.Fields(t).Name & """"
End If
Next t
Print #fh, Buf
Do While Not Rs.EOF
Buf = ""
For t = 0 To Rs.Fields.Count - 1
If IsNull(Rs.Fields(t).Value) Then
TempStr = ""
Else
TempStr = Rs.Fields(t).Value
End If
If Buf = "" Then
Buf = """" & TempStr & """"
Else
Buf = Buf & Delimiter & """" & TempStr & """"
End If
Next t
Print #fh, Buf
Rs.MoveNext
Loop
Close fh
Exit Sub
Err_Out:
If FileIsOpen Then
Close fh
End If
MsgBox "当前错误: " & Error, vbOKOnly, "错误"
End Sub
转自:枕善居
更多精彩>>>
如果觉得《[VB]数据库导入到 CSV 格式文件》对你有帮助,请点赞、收藏,并留下你的观点哦!