| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2396 人关注过本帖
标题:求助::用ADO的方法修改EXCEL中单元格中的数据
只看楼主 加入收藏
anjing572
Rank: 1
等 级:新手上路
帖 子:28
专家分:1
注 册:2007-7-7
结帖率:100%
收藏
 问题点数:0 回复次数:4 
求助::用ADO的方法修改EXCEL中单元格中的数据
如何在vb中用ADO的方法修改EXCEL中单元格中的数据.本站看到的方法都是要打开的(在进程中可以看到),在写入.今天看到http://www.中实现不打开文件写入,不知道是怎么实现的,那位高人赐教一下,最好有源码.谢谢.
搜索更多相关主题的帖子: ADO EXCEL 单元 数据 
2008-12-10 20:42
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:0 
用ODBC吧
有两种用法
一种是用ODBC API 这种不多说
另一种用ADO然后动态创建DSN,当然效率会比第一种低,但简单
动态创建DSN的例子:
程序代码:
ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const ODBC_ADD_DSN = 4
Private Const vbAPINull As Long = 0&

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
    'Private Declare Function SQLCreateDataS
    '     ource Lib "ODBCCP32.DLL" (ByVal hwndPare
    '     nt As Long, ByVal lpszDriver As String)
    ' Api sql

Private Declare Function GetActiveWindow Lib "user32.dll" () As Long

Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (phenv&) As Integer

Private Declare Function SQLAllocConnect Lib "ODBC32.DLL" (ByVal henv&, hDBC&) As Integer

Private Declare Function SQLCreateDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal lpszDriver As String)

Private Declare Function SQLDriverConnect Lib "ODBC32.DLL" (ByVal hDBC As Long, ByVal hwnd As Long, _
    ByVal szCSIn As String, ByVal cbCSIn As Long, ByVal szCSOut As String, _
    ByVal cbCSMax As Long, cbCSOut As Long, ByVal f As Long) As Long

Private Declare Function SQLAllocStmt Lib "ODBC32.DLL" (ByVal hDBC As Long, HStmt As Long) As Long
****************************************************
Public Function CreateDSN(ByVal pstrServer As String, ByVal pstrDescription As String, ByVal pstrDSN As String, ByVal pstrDataBase As String) As Long
    On Error Resume Next
    Dim strDriver As String
    Dim strAttributes As String
    'set the driver to SQL Server because it
    '     is most common.
    strDriver = "SQL Server"'set the attributes delimited by null.
    'See driver documentation for a complete
    'list of supported attributes.
    strAttributes = "SERVER=" & pstrServer & Chr(0)
    strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
    strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
    strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
    ' strAttributes = strAttributes & "UID=u
    '     serid" & Chr$(0)
    ' strAttributes = strAttributes & "PWD=p
    '     assword" & Chr$(0)
    strAttributes = strAttributes & "Trusted_Connection=Yes"
    'Calls API to create DSN
    CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
End Function

Public Sub DeleteDSN(sDSN As String)
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "SQL Server"
    sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
    nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, sDriver, sAttributes)
End Sub

Private Sub Command1_Click()
    Dim lngRet As Long
    Dim RetValue As Integer
    RetValue = CreateDSN("Server", "DSN Server", "server", "master")

    If RetValue = 0 Then
        MsgBox Err.Description
    Else
        MsgBox "DSN Created!"
    End If
End Sub

Private Sub Command2_Click()
    'DeleteDSN "dsnTEst3"
    Dim szConnectOut As String * 512
    Dim Cadena As String
    Dim Server As String
    Dim stTmp As String
    Dim User As String
    Dim Cbout As Long
    Dim lgTmp As Long
    Dim henv As Long
    Dim retcode As Long
    ' Lit le nom de l'utilisateur courant de
    '     l'ordinateur
    stTmp = Space$(250)
    lgTmp = 251
    henv = 0
    ' Allocation de m閙oire pour un handle d
    'environnement "HEnv".

    If SQLAllocEnv(henv) = 0 Then
        ' Allocation d'un handle de connexion "H
        '     dbc".

        If SQLAllocConnect(henv, hDBC) = 0 Then
            ' Etablir la connexion
            ' If you like you can specify a userId a
            '     nd Password and the database to be linke
            '     d the File DSN
            'DATABASE=DB_CARTAS;UID=userid;PWD=passw
            '     ord
            Cadena = "SAVEFILE=Testing_drv;DRIVER={SQL Server};FILEDSN=TEst_Drive;DSN=DB_PMR;SERVER=PMR02;Trusted_Connection=Yes"
            retcode = SQLDriverConnect(hDBC, Me.hwnd, Cadena, Len(Cadena), szConnectOut, 255, Cbout, 1)
        End If
    End If
End Sub

Private Sub Command3_Click()
    DeleteDSN "server"
    MsgBox "DSN Remove!"
End Sub


[[it] 本帖最后由 三断笛 于 2008-12-10 22:32 编辑 [/it]]
2008-12-10 22:26
anjing572
Rank: 1
等 级:新手上路
帖 子:28
专家分:1
注 册:2007-7-7
收藏
得分:0 
版主你好,我是个初学者你的代码没看懂,能不能给个简单的例子,谢谢
2008-12-11 19:53
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:0 
当选中一个Excel文件时先调用CreateDSN给它创建一个DSN,然后用ADO调用这个DSN就行了,用完就删除DSN
2008-12-11 20:59
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:0 
方法2:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Excel文件名";Extended Properties=Excel 5.0;Persist Security Info=False
方法3:用OpenDataSource,效果跟方法2一样
2008-12-13 03:19
快速回复:求助::用ADO的方法修改EXCEL中单元格中的数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043214 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved