使用 VBS 更新电子表格中的单元格 - 保存文件时更改输入格式



我有一个csv文件的以下输入:

s,asdaas, 123123.00,asdasd
123123.00,bubu1,wqeqw,asd
asdasd,asdasd

我正在使用以下 vbs:

On Error Resume Next
sheet_number = CInt(Wscript.Arguments.Item(1))
row_index = CInt(Wscript.Arguments.Item(2))
col_index = CInt(Wscript.Arguments.Item(3))
value_insert = Wscript.Arguments.Item(4)
if value_insert <> "" then
c = Replace(value_insert, "#", Chr(10))
end if
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
if sheet_number = 0 Then
sheet_number = 1
end if
oBook.Sheets(sheet_number).Select
oExcel.Cells(row_index,col_index).NumberFormat = "@"
oExcel.Cells(row_index,col_index).Select
oExcel.Cells(row_index,col_index).Value2 = value_insert
oBook.Save
oBook.Close
oExcel.Quit
If Err.Number <> 0 Then
oBook.Close False
oExcel.Quit
Err.Clear             ' Clear the Error
End If

例如,如果我尝试将第 2 行、第 2 列的值从"bubu1"更新为"oi",则保存文件时其他单元格将受到影响。例如,"123123.00"将变为"123123",请注意.00和空格已消失。

有没有办法防止这种情况? 修改后,csv 文件将被发送到另一个进程,该进程无法处理具有这些"自动"更改的文件。

电流输出:

s,asdaas,123123,asdasd
123123,oi,wqeqw,asd
asdasd,asdasd

期望输出:

s,asdaas, 123123.00,asdasd
123123.00,oi,wqeqw,asd
asdasd,asdasd

我最终没有使用ExcelApplication来处理CSV。

相反,我使用了:http://www.chilkatsoft.com/downloads_ActiveX.asp

完全按照我的需要做了。

最新更新