Access導出csv 內容添加雙引號 vba

要求:導出的csv文件,用文本工具打開時,文字內容須要有雙引號,以下:"1","name1","name2",""工具

我以前的處理方式是excel的方式導出,發現很難作到這個效果。因此我換了一種方案,直接將內容處理後寫入文件oop

首先選擇保存文件的路徑:spa

    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

而後處理內容excel

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
相關文章
相關標籤/搜索