Excel批量转csv格式

EXCEL的开发工具中有Visual Basic。打开,输入如下代码:

Sub SaveToCSVs()
    Dim fDir As String
    Dim wB As Workbook
    Dim wS As Worksheet
    Dim fPath As String
    Dim sPath As String
    fPath = "C:\Users\qiany\Desktop\文件\"
    sPath = "C:\Users\qiany\Desktop\csv保存位置\"
    fDir = Dir(fPath)
    Do While (fDir <> "")
        If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then
            On Error Resume Next
            Set wB = Workbooks.Open(fPath & fDir)
            'MsgBox (wB.Name)
            For Each wS In wB.Sheets
                wS.SaveAs sPath & wB.Name & ".csv", xlCSV
            Next wS
            wB.Close False
            Set wB = Nothing
        End If
        fDir = Dir
        On Error GoTo 0
    Loop
End Sub

中间fPath和sPath改为自己的路径即可。

此时编码格式不是UTF-8。如果想转为UTF-8编码格式的,将csv文件用记事本打开,另存为,选择时注意下面的编码格式选为UTF-8即可。

你可能感兴趣的:(文件处理)