我使用PowerShell通过Ole Automation将Excel保存为Csv:
$Sheet.SaveAs($FileName + ".csv", 6)
为什么Excel使用","来保存csv?与Ole Automation和";"什么时候手工做?
区域设置分隔符是&#34 ;;"
更新:最后在其他论坛上,这似乎是一个真正的难题,没有真正的解决方案,因为它似乎是一个错误。
答案 0 :(得分:1)
从Excel保存CSV时,您有以下选项(使用区域设置分隔符 - >“;”)
Option Explicit
Public Sub SaveCSV()
With ActiveWorkbook
.SaveAs Filename:="C:\x.csv", FileFormat:=xlCSV ' 6: -> ","
.SaveAs Filename:="C:\x.csv", FileFormat:=xlCSVMSDOS '24: -> ","
.SaveAs Filename:="C:\x.csv", FileFormat:=xlCSVWindows '23: -> ";"
.SaveAs Filename:="C:\x.csv", FileFormat:=xlCSVMac '22: -> ";"
End With
End Sub
在PowerShell脚本中,您使用的是 6 (FileFormat)
还有另一个因素可以改变Excel中的分隔符 - TextToColumns:
Public Sub testDelim()
setDelim "@"
End Sub
Public Sub setDelim(ByVal delim As String)
Application.DisplayAlerts = False
If 1 Then '-------------------------------------------------- Change delimiter
Range("A1") = "A" & delim & "1"
Range("A1").TextToColumns Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:=delim
Range("A1:B1") = vbNullString
Else '------------------------------------------------------ Reset delimiter
Range("A1") = "x"
Range("A1").TextToColumns Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=False, _
OtherChar:=""
Range("A1") = vbNullString
End If
Application.DisplayAlerts = True
End Sub