将txt转换为xlsx时,vbscript保留前导零

时间:2019-01-03 06:32:15

标签: excel vba vbscript

我正在尝试将文本文件(制表符分隔)转换为xlsx。下面的脚本成功完成了转换,但是删除了前导0,我尝试使用

预先将所有单元格格式化为文本
objWorkSheet.Cells.NumberFormat = "@"

但是结果仍然没有前导零 尽管所有单元格都格式化为文本。 有什么我可以添加到代码中以使其保留前导0的代码吗?谢谢

 Function convert()
Const xlTextQualifierDoubleQuote = 1
Const xlInsertDeleteCells = 1
Const xlDelimited = 1

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts= False

strCSVfile = "C:\Users\xxxx\Downloads\a.txt"
strXLSfile = "C:\Users\xxxx\Downloads\b.xlsx"

Set objWorkbook = objExcel.Workbooks.Add
Set objWorkSheet = objWorkbook.Worksheets(1)
objWorkSheet.Cells.NumberFormat = "@"
With objWorkSheet.QueryTables.Add("TEXT;" & strCSVfile, objWorkSheet.Range("$A$1"))
    .Name = "input"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileOtherDelimiter = "#"
    .TextFileColumnDataTypes = Array(1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh False
End With

'Save Spreadsheet, 51 = Excel 2007-2010
objWorkSheet.SaveAs strXLSfile, 51

'Release Lock on Spreadsheet
objExcel.Quit()
Set ObjExcel = Nothing
End Function

0 个答案:

没有答案