专业的JAVA编程教程与资源

网站首页 > java教程 正文

使用vba将Excel 文件转成 CSV 文件

temp10 2025-07-21 17:47:17 java教程 4 ℃ 0 评论

使用vba将Excel中的内容写入文本文件,保存为txt或csv格式。

方法一:

使用vba将Excel 文件转成 CSV 文件

Sub toCSV()

    Dim sht As Worksheet
    Dim i As Integer, j As Integer
    Dim arr()
    Dim line As String
    
    For Each sht In Sheets
        If Int(sht.Name) Mod 2 = 1 Then
            arr = sht.Range("a1").CurrentRegion
            Open "C:\Users\heju\Desktop\csv & xlsx\" & sht.Name & ".csv" For Output As #1
                For i = LBound(arr) To UBound(arr)
                    line = ""
                    For j = LBound(arr, 2) To UBound(arr, 2)
                        line = line & arr(i, j) & ","
                    Next
                    line = Left(line, Len(line) - 1)
                    Print #1, line
                Next
            Close
        End If
    Next
End Sub

方法二:

Sub shttobook()
    Dim sht As Worksheet, path As String
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    path = ThisWorkbook.path & "\csv & xlsx"
    For Each sht In Worksheets
        sht.Copy
        ActiveWorkbook.SaveAs Filename:=path & "\" & sht.Name & ".csv", FileFormat:=xlCSV
        ActiveWorkbook.Close
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表