要求:导出的csv文件,用文本工具打开时,文字内容需要有双引号,如下:"1","name1","name2",""
我之前的处理方式是excel的方式导出,发现很难做到这个效果。所以我换了一种方案,直接将内容处理后写入文件
首先选择保存文件的路径:
Dim result As String
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "Please select the target folder"
.InitialFileName = "filename.csv" If .Show = - Then
result = .SelectedItems()
Else
Exit Function
End If
End With
If Dir(result , vbNormal) <> "" Then
Kill result
End If
然后处理内容
Dim Obj_DataBase As DAO.Database
Dim Obj_Recordset As DAO.Recordset
Dim strLineConts As String
Set Obj_DataBase = CurrentDb()
Dim fileNo As Integer
fileNo = FreeFile() strLineConts = ""
strLineConts = strLineConts & Chr() & "列名1" & Chr() & ","
strLineConts = strLineConts & Chr() & "列名2" & Chr()
Print #fileNo, strLineConts
strSQL = "Select * From 表名 Order By 字段1" Set Obj_Recordset = Obj_DataBase.OpenRecordset(strSQL) Do While Not Obj_Recordset.EOF
strLineConts = ""
strLineConts = strLineConts & Chr() & Obj_Recordset.Fields("值1") & Chr() & ","
strLineConts = strLineConts & Chr() & Obj_Recordset.Fields("值2") & Chr()
Print #fileNo, strLineConts
Obj_Recordset.MoveNext
i = i +
Loop
Close #fileNo
Set Obj_DataBase = Nothing
Set Obj_Recordset = Nothing
Exit Function