要求:导出的csv文件,用文本工具打开时,文字内容需要有双引号,如下:"1","name1","name2",""
我之前的处理方式是excel的方式导出,发现很难做到这个效果。所以我换了一种方案,直接将内容处理后写入文件
首先选择保存文件的路径:
Dim result As String With Application.FileDialog(msoFileDialogSaveAs) .Title = "Please select the target folder" .InitialFileName = "filename.csv" If .Show = -1 Then result = .SelectedItems(1) 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(34) & "列名1" & Chr(34) & "," strLineConts = strLineConts & Chr(34) & "列名2" & Chr(34) 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(34) & Obj_Recordset.Fields("值1") & Chr(34) & "," strLineConts = strLineConts & Chr(34) & Obj_Recordset.Fields("值2") & Chr(34) Print #fileNo, strLineConts Obj_Recordset.MoveNext i = i + 1 Loop Close #fileNo Set Obj_DataBase = Nothing Set Obj_Recordset = Nothing Exit Function