请帮帮我。 我试图将放置在服务器上某个路径上的.xls / .xlsx(让一个\ test \ 1.xls)文件转换为.csv(不会损坏日期格式或任何其他info)并将其保存在固定路径中(让我们说一个\ result \ 1.csv)。此过程必须每24小时自动重复一次。
谢谢!
答案 0 :(得分:0)
到目前为止,我只得到下面的代码,只转换为csv并将其保存在与原始文件相同的位置。
另外由于某些未知原因,它将日期的HALF转换为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