[英]How do i convert an xlsx file into a csv file, using powershell?
如何將 xlsx 文件轉換為 csv 文件並使用 Powershell 為每個單元格添加雙引號?
我需要將很多文件從 .xlsx 轉換為 csv。除此之外,每個單元格都必須用雙引號引起來,並且必須添加一個分號分隔符。
我制作了一個 VBA 腳本來執行從 .xlsx 到 .csv 的轉換並添加雙引號,但每個文件需要花費多個小時。
我希望我使用 powershell 會更快。
有人知道如何在 Powershell 中重寫此功能嗎?
非常感謝您的幫助!
我在 VBA 中的做法:
Sub ConvertToCSV()
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Long
Dim StrFile As String
Application.ScreenUpdating = False
StrFile = Dir("C:\Users\example\*PLZ*")
Do While Len(StrFile) > 0
Workbooks.Open ("C:\Users\example\" & StrFile)
Range("A1").Select
Selection.CurrentRegion.Select
NameWithoutExtension = Left(StrFile, Len(StrFile) - 5)
DestFile = "C:\Users\example\" & NameWithoutExtension
FileNum = FreeFile()
Open DestFile For Output As #FileNum
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
On Error GoTo 0
For RowCount = 1 To Selection.Rows.Count
' Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.Count
OldText = Selection.Cells(RowCount, ColumnCount).Text
MiddleText = Replace(OldText, "\", "/")
NewText = Replace(MiddleText, """", "\""")
' Write current cell's text to file with quotation marks.
Print #FileNum, """" & NewText & """";
' Check if cell is in last column.
If ColumnCount = Selection.Columns.Count Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ";";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount
' Start next iteration of RowCount loop.
Next RowCount
' Close destination file.
Close #FileNum
ActiveWorkbook.Close
StrFile = Dir
Loop
MsgBox ("Done")
End Sub
真的沒有必要為此使用powershell,你只需要在VBA中使用正確的方法......
代碼未經測試,但認為它應該可以正常工作......
Sub ConvertToCSV()
Application.ScreenUpdating = False
Dim StrFile As String
StrFile = Dir("C:\Users\example\*PLZ*")
Do While Len(StrFile) > 0
'Open workbook
Dim wb as workbook
set wb = Workbooks.Open("C:\Users\example\" & StrFile)
'Save workbook to new location
Dim DestFile As String
DestFile = "C:\Users\example\" & Left(StrFile, Len(StrFile) - 5)
wb.saveAs destFile, xlFileFormat.xlCSV
'Ensure no alerts while close
Application.displayAlerts = false
wb.close false
Application.displayAlerts = true
'Continue loop
StrFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
編輯:
抱歉,我沒有看到需要引用字符串的要求。 可以對您的代碼進行一些修補,這將使它運行得更快,關鍵是首先將文件轉換為數組:
我試圖讓您的代碼盡可能接近以前的代碼。 請注意,您當前使用分號分隔文件...
Sub ConvertToCSV()
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Long
Dim StrFile As String
Application.ScreenUpdating = False
StrFile = Dir("C:\Users\example\*PLZ*")
Do While Len(StrFile) > 0
'Open workbook and store in variable
Dim wb as workbook
set wb = Workbooks.Open("C:\Users\example\" & StrFile)
'Get data as array
Dim r as range, v as variant
set r = wb.ActiveSheet.Range("A1").CurrentRegion
v = r.value2
'Get dest path
NameWithoutExtension = Left(StrFile, Len(StrFile) - 5)
DestFile = "C:\Users\example\" & NameWithoutExtension
FileNum = FreeFile()
'Try open file
On Error Resume Next
Open DestFile For Output As #FileNum
'If error then end
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
On Error GoTo 0
'Loop over array
Dim i as long, j as long
For i = 1 To ubound(v,1)
For j = 1 To ubound(v,2)
OldText = v(i,j)
MiddleText = Replace(OldText, "\", "/")
NewText = Replace(MiddleText, """", "\""")
' Write current cell's text to file with quotation marks.
Print #FileNum, """" & NewText & """";
' Check if cell is in last column.
If j = ubound(v,2) Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ";";
End If
Next j 'Next column
Next i 'Next row
' Close destination file.
Close #FileNum
'Close workbook
wb.Close false
'Get next file path
StrFile = Dir
Loop
MsgBox ("Done")
End Sub
注意:如果您的數據集很大,這可能會導致 out of memory 錯誤。 如果是,go 和 powershell。
Import-Excel .\Book1.xlsx | Export-Csv .\book1.csv
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.