VBS读取Excel

Function FastReadExcel(byval myXlsFile,byval mySheet,byref arrData( ), byref Rscount,byval blnHeader ) 
        Erase arrData
        Dim  i, j 
        Dim objExcel, objRS 
        Dim strHeader, strRange 

        Const adOpenForwardOnly = 0 
        Const adOpenKeyset = 1 
        Const adOpenDynamic = 2 
        Const adOpenStatic = 3 

        ' Define header parameter string for Excel object 
        If blnHeader Then 
                strHeader = "HDR=YES;" 
        Else 
                strHeader = "HDR=NO;" 
        End If 

        ' Open the object for the Excel file 
        Set objExcel = CreateObject( "ADODB.Connection" ) 
        ' IMEX=1 includes cell content of any format; tip by Thomas Willig 
        objExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ 
        myXlsFile & ";Extended Properties=""Excel 8.0;IMEX=1;" & _ 
        strHeader & """" 

        ' Open a recordset object for the sheet and range 
        Set objRS = CreateObject( "ADODB.Recordset" ) 

        objRS.Open "Select * from [" & mySheet & "$]", objExcel, adOpenStatic 
        ReDim Preserve arrData( objRS.RecordCount-1, objRS.Fields.Count - 1) 

        i = 0 
        Do Until objRS.EOF 
                ' Stop reading when an empty row is encountered in the Excel sheet 
                If IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do 
                ' Add a new row to the output array 
                    'ReDim Preserve arrData( i, objRS.Fields.Count - 1) 
                

                For j = 0 To objRS.Fields.Count - 1 
                        If IsNull( objRS.Fields(j).Value ) Then 
                                arrData( i,j ) = "" 
                        Else 
                                arrData(i,j) = Trim( objRS.Fields(j).Value ) 
                        End If 
                Next 
                ' Move to the next row 
                objRS.MoveNext 
                ' Increment the array "row" number 
                i = i + 1 
        Loop 
        Rscount = i
        ' Close the file and release the objects 
        objRS.Close 
        objExcel.Close 
        Set objRS = Nothing 
        Set objExcel = Nothing 
End Function 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WINCC VBS操作EXCEL可以使用Microsoft Excel对象库来实现。你可以使用VBS脚本来创建、打开、读取和写入Excel文件。下面是一个简单的示例代码,演示如何使用VBS脚本在WINCC中操作Excel: ```vbscript ' 创建Excel对象 Dim excelApp Set excelApp = CreateObject("Excel.Application") ' 打开Excel文件 Dim excelWorkbook Set excelWorkbook = excelApp.Workbooks.Open("C:\path\to\your\file.xlsx") ' 操作Excel文件 ' 例如,读取单元格A1的值 Dim value value = excelWorkbook.Sheets(1).Range("A1").Value ' 写入单元格B1的值 excelWorkbook.Sheets(1).Range("B1").Value = "Hello, World!" ' 保存并关闭Excel文件 excelWorkbook.Save excelWorkbook.Close ' 释放Excel对象 Set excelWorkbook = Nothing Set excelApp = Nothing ``` 请注意,你需要根据你的实际情况修改文件路径和操作的单元格范围。此外,你还可以使用其他Excel对象库提供的功能来实现更复杂的操作,如复制粘贴数据、创建图表等。希望这个示例能帮到你! #### 引用[.reference_title] - *1* [wincc里vbs脚本](https://blog.csdn.net/m0_70446985/article/details/129145376)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* *3* [vbs保存Excel 怎样用VBS关闭EXCEL](https://blog.csdn.net/weixin_39601056/article/details/117970998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值