How to modify an Excel spreadsheet without Excel using VBScript?

I need to add a row to a spreadsheet using VBScript on a PC on which Microsoft Office is not installed.

I tried [ Set objExcel = CreateObject("Excel.Application")]

Since Excel does not exist on the PC, I cannot create this object.

Is there a way to change a table without Excel?

+3
source share
10 answers

To use the code below, create an Excel workbook named "Test.xls" in the same folder as the vbscript file.

In Test.xls, enter the following data into cells A1 through B4:

First   Last
Joe     Smith
Mary    Jones
Sam     Nelson

Paste the vbscript code below into the .vbs file:

Const adOpenStatic = 3
Const adLockOptimistic = 3

filename = "Test.xls"
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filename & _
        ";Extended Properties=Excel 8.0"

query = "Select * from [Sheet1$A1:B65535]"
Set rs = CreateObject("ADODB.Recordset")
rs.Open query, cn, adOpenStatic, adLockOptimistic

rs.AddNew
rs("First") = "George"
rs("Last") = "Washington"
rs.Update

rs.MoveFirst
Do Until rs.EOF
  WScript.Echo rs.Fields("First") & " " & rs.Fields("Last")
  rs.MoveNext
Loop

At the command prompt, type:

CSCRIPT Yourfile.vbs

He will add the name to the spreadsheet, and then write out all the names.

Joe Smith
Mary Jones
Sam Nelson
George Washington
+8
source

Microsoft Jet:

. vbscript. . .

+2

. Microsoft Excel , , , VBScript.

+1

script, , .

Dim arrValue
arrValue = Array("Test","20","","I","2.25","3.9761","20","60","12","1","","1","1","1")
AddXLSRow "C:\Test.xls", "A1:N109", arrValue

Sub AddXLSRow(strSource, strRange, arrValues)
'This routine uses the data from an array to fill fields in the specified spreadsheet.
'Input strSource (String) = The Full path and filename of the spreadsheet to be used.
'Input arrValues (Array) = An array of values to be added to the spreadsheet.
Dim strConnection, conn, rs, strSQL, index

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strSource & ";Extended Properties=""Excel 8.0;HDR=Yes;"";"

Set conn = CreateObject("ADODB.Connection")
conn.Open strConnection
Set rs = CreateObject("ADODB.recordset")
strSQL = "SELECT * FROM " & strRange
rs.open strSQL, conn, 3,3
rs.AddNew 
index = 0
For Each field In rs.Fields
         If field.Type = 202 Then
                   field.value = arrValues(index)
         ElseIffield.Type = 5 And arrValues(index) <> "" Then
                   field.value = CDbl(arrValues(index))
         End If
         If NOT index >= UBound(arrValues) Then
                   index = index + 1
         End If
Next
rs.Update
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
+1

... , , Excel vbScript Excel . , , . , , , , - , .

(/) Excel vbScript Windows 2008, Excel . ( PowerShell, VBS):

vbScript Excel Excel

vbScript Excel Excel

, -, .

l8r...

UCG

+1

Excel , Excel.

, Excel 2007 (xslx), OpenXML- .NET Framework Excel.

Office OpenXML.

0

, . #, -.

0

, . , VSTO, , , . , , , .

0

, - , COM Excel, Excel. Office - (Excel, Word - ), .

-1

EPPlus. epplus.codeplex.com

, VSTO, excel.

-1

Source: https://habr.com/ru/post/1698968/


All Articles