简体   繁体   English

VBA:将xls转换为csv并将其保存在特定路径中

[英]VBA: convert xls to csv and save it in a certain path

Please help me. 请帮我。 I'm trying to convert a .xls/.xlsx placed on a certain path on a server (let's say one\\test\\1.xls) file to .csv (without damageing the date format or any other info) and save it in a fixed path (let's say one\\result\\1.csv). 我正在尝试将放置在服务器(例如one \\ test \\ 1.xls)文件上某个路径上的.xls / .xlsx转换为.csv(不破坏日期格式或任何其他信息)并将其保存在固定路径(比方说一个\\结果\\ 1.csv)。 This process must repeat itself automatically every 24 hours. 此过程必须每24小时自动重复一次。

Thank you! 谢谢!

So far I only got the code below that only converts to csv and saves it in the same location as the original file. 到目前为止,我只得到下面的代码,该代码仅转换为csv并将其保存在与原始文件相同的位置。
Also for some unknown reason it converts HALF of the dates to 05/31/2017 for exemple (and the original format is 31.05.2017). 同样由于某种未知原因,它会将日期的一半转换为例如05/31/2017(原始格式为31.05.2017)。

Sub ConvertXLSToCSV()

    Dim InputPath As String

    Dim PowershellCommand As String



    ' To suppress excel prompts and alert messages while the macro is running.

    Application.DisplayAlerts = False

    Do

        ' Taking the input excel file path which needs to be converted to .csv format.

        InputPath = InputBox("Enter the full path of the input excel file.", "Convert XLS to CSV")

        If Trim(InputPath) <> "" Then

            If Dir(InputPath) = vbNullString Then

                MsgBox "File: '" & InputPath & "' doesn't exists.", vbOKOnly + vbCritical, "Convert XLS to CSV"

            ElseIf Split(Dir(InputPath), ".")(1) = "xlsx" Or Split(Dir(InputPath), ".")(1) = "xls" Then

                InputPath = Trim(InputPath)

                ' Opening the input excel file and saving it in .csv using powershell.

                PowershellCommand = "$ExcelWB = new-object -comobject excel.application" & vbNewLine

                PowershellCommand = PowershellCommand & "$ExcelWB = new-object -comobject excel.application" & vbNewLine

                PowershellCommand = PowershellCommand & "$ExcelWB.Visible = $false" & vbNewLine & "$ExcelWB.DisplayAlerts = $false" & vbNewLine

                PowershellCommand = PowershellCommand & "$Workbook = $ExcelWB.Workbooks.Open('" & InputPath & "')" & vbNewLine

                PowershellCommand = PowershellCommand & "$Workbook.SaveAs('" & Left(InputPath, (InStrRev(InputPath, ".", -1, vbTextCompare) - 1)) & ".csv',6)" & vbNewLine

                PowershellCommand = PowershellCommand & "$Workbook.Close($false)" & vbNewLine

                PowershellCommand = PowershellCommand & "$ExcelWB.quit()" & vbNewLine

                PowershellCommand = "Powershell.exe -command " & PowershellCommand

                Set WshShell = CreateObject("WScript.Shell")

                WshShell.Exec (PowershellCommand)

                Exit Do

            Else:

                MsgBox "Input file is not in excel (.xlsx/.xls) format.", vbOKOnly + vbCritical, "Convert XLS to CSV"

            End If

        ElseIf StrPtr(InputPath) <> 0 Then MsgBox "Please enter the full path of the input excel file.", vbOKOnly + vbExclamation, "Convert XLS to CSV"

        End If

    Loop Until StrPtr(InputPath) = 0

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM