VB6.0 连接Access数据库后 如何编写代码使数据库导出到excel、word或html上?

我用了adodc和datagrid控件 希望有大神能帮忙写一个导出程序!

Private Sub Command1_Click()
    Dim xlapp As Variant
    Dim xlBook As Variant
    Dim xlSheet As Variant
    Dim sum As Long
    Set xlapp = CreateObject("excel.application")
    Set xlBook = xlapp.Workbooks.Open(App.Path & "\data\报表.xlt") '打开EXCEL模板
    'Set xlBook = xlapp.Workbooks.Add
    Set xlSheet = xlBook.worksheets(1)
    xlapp.Visible = True
    Adodc1.ConnectionString = cn.ConnectionString
    Adodc1.RecordSource = "select * from [Sheet1] where è¯•éªŒå·='" & shiyanH & "'"
    Adodc1.Refresh
    If Adodc1.Recordset.RecordCount > 0 Then
    Adodc1.Recordset.MoveFirst
    xlSheet.Cells(sum + 1, 2) = shiyanH
    For sum = 0 To Adodc1.Recordset.RecordCount - 1
        xlSheet.Cells(sum + 3, 1) = Adodc1.Recordset(1)
        For j = 2 To 21
            If Adodc1.Recordset(j) <> "" Then
                If Adodc1.Recordset(j) = "********" Then
                    xlSheet.Cells(sum + 3, j) = (Adodc1.Recordset(j))
                Else
                    xlSheet.Cells(sum + 3, j) = Val(Adodc1.Recordset(j))
                End If
            End If
        Next
        Adodc1.Recordset.MoveNext
    Next sum
    End If
End Sub
Sub Main()
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\tsdb.mdb;Persist Security Info=False"
    frmStart.Show
End Sub

以上是读取Access数据表,导入到Excel的代码。

追问

您好!请问这个代码是不是需要先引用Microsoft Excel部件?
还有就是第12行的“试验号”是不是要改成我自己数据库的名称,还有第12行和第16行的shiyanH是Excel里sheet1的名字吗?

追答

1.引用见上图

2.需改动33行代码为你的数据库。
3.12行代码用你的表名和索引字段。

温馨提示:内容为网友见解,仅供参考
第1个回答  2018-04-16
可以的,私信一下我追答

晚上帮你处理

已通知提问者对您的回答进行评价,请稍等

相似回答