Convert pipe-delimited files to .xls

前端 未结 2 1043
隐瞒了意图╮
隐瞒了意图╮ 2021-01-27 18:16

I\'m trying to convert pipe-delimited files to xls (Excel) with batch file and vbscript. Unfortunately, my \"output.xls\" file is still showing the pipe delimiter in the table a

相关标签:
2条回答
  • 2021-01-27 18:40

    Excel is a little picky when it comes to reading CSV files. If you have a delimited file with the extension .csv Excel will only open it correctly via the Open method if the delimiter is the character configured in the system's regional settings.

    The Open method has optional parameters that allow you to specify a custom delimiter character (credit to @Jeeped for pointing this out):

    set objWorkbook = objExcel.Workbooks.Open(srccsvfile, , , 6, , , , , "|")
    

    You can also use the OpenText method (which will be used when recording the action as a macro):

    objExcel.Workbooks.OpenText srccsvfile, , , 1, , , , , , , True, "|"
    Set objWorkbook = objExcel.Workbooks(1)
    

    Note that the OpenText method does not return a workbook object, so you must assign the workbook to a variable yourself after opening the file.

    Important: either way your file must not have the extension .csv if your delimiter character differs from your system's regional settings, otherwise the delimiter will be ignored.

    0 讨论(0)
  • 2021-01-27 18:44

    Pipe doesn't equal Comma, Excel natively knows what to do with a CSV, but not with Pipe.

    All is not lost, record your actions opening the file manually, once open highlight column A and click Data / Text To Columns, choose delimited and in the "other" box put a pipe then click next, choose the column formats (great to format numbers as text if you need to like Postcodes and phone numbers) then click finish.

    Now stop the recorder and look at the code it generated. Port this over to your Excel object in your script.

    0 讨论(0)
提交回复
热议问题