通过命令行将XLS转换为管道分隔的CSV文件

时间:2015-03-18 03:41:14

标签: 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 = 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"

但我想要管道分隔符。

我将值从6更改为22,但分隔符为TAB

    call oBook.SaveAs(WScript.Arguments.Item(1), 6)

感谢您的所有帮助。

RYL

2 个答案:

答案 0 :(得分:0)

如果你知道电子表格的格式,你可以使用这样的东西。

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

答案 1 :(得分:0)

这更优雅一点。它更改全局分隔符保存文件并将其更改回来。

 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"