Vb.net 通过命令行将XLS转换为管道分隔的CSV文件
我找到了将xls转换为csv的代码Vb.net 通过命令行将XLS转换为管道分隔的CSV文件,vb.net,excel,csv,vbscript,Vb.net,Excel,Csv,Vbscript,我找到了将xls转换为csv的代码 if WScript.Arguments.Count < 2 Then WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv" Wscript.Quit End If Dim oExcel Set oExcel = Crea
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
call oBook.SaveAs(WScript.Arguments.Item(1), 6)
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
谢谢你的帮助
Ryl如果您知道电子表格的格式,您可以使用类似的格式
Const ForReading = 1, ForWriting = 2, Forappending = 8
SheetName = "Sheet1"
fileDir = "C:\XXXXX\stack\"
fileOut = "Pipe.csv"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = oFSO.OpenTextFile(fileDir & fileOut, ForWriting,true)
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
("C:\XXXX\stack\sample.xlsx")
objExcel.Sheets(SheetName).Select
intRow = 1
Do Until objExcel.Cells(intRow,1).Value = ""
lineOut = objExcel.Cells(intRow, 1).Value & "|" & objExcel.Cells(intRow, 2).Value & "|" & objExcel.Cells(intRow, 3).Value
objFile.writeline lineOut
intRow = intRow + 1
Loop
objExcel.Quit
这个稍微优雅一点。它更改全局分隔符保存文件并将其更改回
Set oWshShell = WScript.CreateObject("WScript.Shell")
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
oWshShell.RUN "powershell.exe -noprofile -window hidden -command " &_
"C:\XXXXX\stack\Pipe.ps1" ,0,True
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
call oBook.SaveAs(WScript.Arguments.Item(1),6,0,0,0,0,0,0,0,0,0,True )
oBook.Close False
oExcel.Quit
oWshShell.Run "powershell.exe -noprofile -window hidden -command " &_
"C:\XXXXX\stack\Comma.ps1" ,0, True
WScript.Echo "Done"
我认为您需要通过计算机的区域设置设置列表分隔符。请看,您可以在脚本中更改它,保存文件,然后将其更改回。创建上面在Pipe.ps1 put Set ItemProperty-Path HKCU:\Control Panel\International-Name sList-Value |中列出的2个文件,在Comma.ps1 Set ItemProperty-Path HKCU:\Control Panel\International-Name sList-Value,
Set oWshShell = WScript.CreateObject("WScript.Shell")
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
oWshShell.RUN "powershell.exe -noprofile -window hidden -command " &_
"C:\XXXXX\stack\Pipe.ps1" ,0,True
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
call oBook.SaveAs(WScript.Arguments.Item(1),6,0,0,0,0,0,0,0,0,0,True )
oBook.Close False
oExcel.Quit
oWshShell.Run "powershell.exe -noprofile -window hidden -command " &_
"C:\XXXXX\stack\Comma.ps1" ,0, True
WScript.Echo "Done"