gpt4 book ai didi

excel - 将csv导入excel的vbscript

转载 作者:行者123 更新时间:2023-12-04 21:25:49 25 4
gpt4 key购买 nike

我想制作一个 VBSript,它将打开一个包含不同行和列的 csv,并从第 4 行第 1 列开始粘贴它们。到目前为止,我写的内容很慢,并且有很多列硬编码到其中。

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(wscript.Arguments(0))
objExcel.Visible = True
objExcel.Cells(1, 4).Value = wscript.Arguments(1)+" - "+wscript.Arguments(2)
objExcel.Cells(2, 4).Value = wscript.Arguments(3)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (wscript.Arguments(4))
cCount = 1
rCount = 4

Do While objTextFile.AtEndOfStream <> True
arrUserRecord = split(objTextFile.Readline, ",")
Do While cCount<213
objExcel.Cells(rCount, cCount).Value = arrUserRecord(cCount-1)
cCount=cCount+1
Loop
cCount=1
rCount=1+rCount
Loop

最佳答案

Excel 可以相当特别地接受它作为“有效 CSV”的内容。我不得不多次求助于以下方法:

Const vbFormatStandard = 1
Const vbFormatText = 2
Const vbFormatDate = 4

Const xlDelimited = 1
Const xlDoubleQuote = 1

' change according to number/type of the fields in your CSV
dataTypes = Array( Array(1, vbFormatText) _
, Array(2, vbFormatStandard) _
, Array(3, vbFormatText) _
, Array(4, vbFormatDate) _
)

Set xl = CreateObject("Excel.Application")
xl.Visible = True

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

关于excel - 将csv导入excel的vbscript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com