zoukankan      html  css  js  c++  java
  • VBA 代码集

    1. 读取本机网卡地址

      Dim MyMac
        Set MyMac = GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
        For Each MyMacAddress In MyMac
          If MyMacAddress.IPEnabled = True Then
          MsgBox "本机网卡MAC地址是:" & MyMacAddress.MacAddress
        Exit For
        End If
        Next

    2.从数据集读取数据至Sheet

        Dim strDate As String, strEntity As String
        strDate = Format(Sheets("sheet1").[b2], "yyyy-MM-dd")  
        strEntity = Sheets("sheet1").[b3]
                
        Dim strSql As String    
        '查询语句,
         strSql = "select * from xxxx where ..."
            
         Sheets("sheet1").Range("A7:J65535").ClearContents '清除内容
            
         Dim ds1 As ADODB.Recordset
         Set ds1 = runSql(strSql)
         '根据查询语句获得数据,并填充至A7
          Worksheets("sheet10").Range("A7").CopyFromRecordset ds1
          Set ds1 = Nothing      

     3. 数据库连接及执行

    Option Explicit
    
    Public conn As ADODB.Connection
    Public Const strConn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=ts123;Initial Catalog=SQLDB;Data Source=.;Connect Timeout=720; "
    
    
    Public Function runSql(strSql) As ADODB.Recordset
        Dim ds As ADODB.Recordset
         Set ds = New ADODB.Recordset
         OpenConn          
         ds.Open strSql, conn     
         Set runSql = ds
         CloseConn     
    End Function
    
    Public Function OpenConn()
        If conn Is Nothing Then
            Set conn = New ADODB.Connection
        End If
        If conn.State <> 1 Then
            conn.Open strConn
        End If
    End Function

    Public Function CloseConn() If conn.State = 1 Then Set conn = Nothing End If End Function

     4. 读取文件内容

    Dim txt As String, Result As String
           
    Result = ""
    Open "c:loc_conn.txt" For Input As #1 '
    Do While Not EOF(1)
      Line Input #1, txt  
      Result = Result & txt & Chr(13) & Chr(10)
    Loop
    Close #1
    MsgBox Result
  • 相关阅读:
    接口类和抽象类
    生成器和迭代器
    装饰器
    KMP算法(java,c++)
    java EE
    XML
    SQL语法
    单元测试
    JDBC
    SpringCloud
  • 原文地址:https://www.cnblogs.com/jerron/p/5573406.html
Copyright © 2011-2022 走看看