Convert .csv file into Excel in VBScript

主宰稳场 提交于 2019-12-04 21:18:31

There are several issues with the code from your updated question:

  • Const XlPlatform = "xlWindows"

    XlPlatform must be a numeric value, not a string. The xlWindows member of the enumeration has the value 2. However, since that's the default anyway, you can simply omit this value when calling OpenText.

  • Set workBook1 = xlApp.ActiveWorkBook

    A newly spawned Excel instance doesn't have an active workbook, so it's pointless to assign that to a variable before you actually open or create a workbook.

  • Set workBook1 = xlApp.WorkBooks.OpenText(...)

    The OpenText method does not return an object, so there's nothing to assign in the above statement.

  • ...ooks.OpenText(..., true, "CRLF", ...)

    The string "CRLF" is not a character. Why do you want to specify line-breaks as separator-characters anyway?

  • ..., Array(...), true, false)

    The last 2 parameters you specified are TextVisualLayout (specifies left-to-rigt/right-to-left) and DecimalSeparator. Both of them are not boolean values. Simply omit them if you don't know for certain that you need them.

  • workBook1.Save "Y:\Personal Folders\XXXX\x.xlsx", xlNormal

    The Save method saves a workbook under its current name. To save a workbook under a different name you must use the SaveAs method. The constant xlNormal isn't defined anywhere in your code. Plus, xlNormal produces an Excel 97/2003 workbook (.xls). To save a workbook as an Excel 2007/2010 workbook (.xlsx) you must use the xlOpenXMLWorkbook constant.

The following code worked for me:

Const xlDelimited                =  1
Const xlTextQualifierDoubleQuote =  1
Const xlOpenXMLWorkbook          = 51

Set xl = CreateObject("Excel.Application")

xl.Workbooks.OpenText "Y:\Personal Folders\XXXX\TestFile1.csv", , , xlDelimited _
  , xlTextQualifierDoubleQuote, True, False, False, True, False, False, _
  , Array(Array(1,2), Array(2,2), Array(3,2), Array(4,1), Array(5,2) _
  , Array(6,1), Array(7,1), Array(8,1), Array(9,1), Array(10,1), Array(11,1))
Set wb = xl.ActiveWorkbook

wb.SaveAs "Y:\Personal Folders\XXXX\x.xlsx", xlOpenXMLWorkbook, , , , False
wb.Close

xl.Quit
SriniShine

I'm using Excel 2003.The following is a code I got to specify the .csv file format

Set xl = CreateObject("Excel.Application")

Set wb = xl.Workbooks.OpenText "input.csv", , , xlDelimited, xlDoubleQuote, _
  False, False, True, , , , dataTypes

from Vbscript to import csv into excel

Could you please explain the function of the following line?

Set wb = xl.Workbooks.OpenText "input.csv", , , xlDelimited, xlDoubleQuote, _
      False, False, True, , , , dataTypes


My implementation of the OpenText property for the above mentioned .csv file

Set workBook1 = xlApp.WorkBooks.OpenText("D:\Personal Folders\XXXX\TestFile1.csv", , ,xlDelimited, ,false ,false ,false ,True,false ,false ,"~")

You need to specify what Format you are saving as. The following will specify it as a standard .xls for 2003

    Workbooks.Open Filename:= _
    "D:\Personal Folders\XXXXX\TestFile1.csv"

    ActiveWorkbook.SaveAs Filename:= _
    "D:\Personal Folders\XXXXX\x.xls", FileFormat:=xlNormal

    ActiveWorkbook.Close

Also can try

ActiveWorkbook.SaveAs Filename:= _
    "D:\Personal Folders\XXXXX\x.xls", FileFormat:= 56
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!