简体   繁体   English

VB6 将数据从数据库插入到excel

[英]VB6 insert data into excel from database

I have been looking for a solution to inserting data into excel using vb6 code and access database.我一直在寻找使用 vb6 代码和访问数据库将数据插入 excel 的解决方案。 There are many cases where I need to write to an excel spreadsheet multiple times with different records of data.在很多情况下,我需要使用不同的数据记录多次写入 Excel 电子表格。 I have an existing workbook that I am trying to open and "save as" when I am complete.我有一个现有的工作簿,我正在尝试打开并在完成后“另存为”。 I have been able to open the excel workbook, access the sheet I am writing to, and the cells I am writing to, however I can only write to the workbook once and when I leave the scope of the open workbook the connection is closed.我已经能够打开 excel 工作簿,访问我正在写入的工作表以及我正在写入的单元格,但是我只能向工作簿写入一次,当我离开打开的工作簿的范围时,连接关闭。

I have a sub routine that creates the workbook object, opens the existing workbook and work sheet, writes to a specified cell number to insert the new data.我有一个创建工作簿对象的子例程,打开现有的工作簿和工作表,写入指定的单元格编号以插入新数据。 I have looked at official support pages and it doesn't seem to have what I am looking for at this time.我查看了官方支持页面,目前似乎没有我要找的内容。

Am I making this too complicated or is there a solution for this?是我把这弄得太复杂了还是有解决方案? Please help.请帮忙。

My current code:我目前的代码:

Row Arrays行数组

Private oldDataRowArray(3 To 21) As Integer
Private updatedDataRowArray(5 To 2) As Integer

Loop logic循环逻辑

Dim i As Integer
Dim n As Integer
i = 3
n = 5
Do While i <= UBound(oldDataRowArray) And n <= UBound(updatedDataRowArray)
EditExcelSheet txtWorkbookFileName.Text, i, n //sub routine
i = i + 3 //skip number of cells
n = n + 3 //skip number of cells
Loop

Sub Routine to Insert data into Excel将数据插入Excel的子程序

Private Sub EditStakingSheet(ByVal workbook As String, ByVal oldDataRowIndex As Integer, ByVal newDataRowIndex As Integer)
Dim objExcel As Object
Dim objWorkBook As Object
Dim objSheet As Object

Set objExcel = New Excel.Application
Set objWorkBook = objExcel.Workbooks.Open(workbook)
Set objSheet = objWorkBook.Worksheets(1)

objExcel.Visible = True
//insert old value
objSheet.Cells(oldDataRowIndex , 26).Value = "old Value"

//insert new value
objSheet.Cells(newDataRowIndex , 26).Value = "new Value"

End Sub

You could use adodb objects.您可以使用 adodb 对象。

This video is a good tutorial for this.这个视频是一个很好的教程。

Here is an example how you can use adodb.这是一个如何使用 adodb 的示例。 You need to install the activeX Data Objects Libary for this.为此,您需要安装 activeX 数据对象库。 For .source= you can use any sql-query.对于.source=你可以使用任何 sql-query。

Public Function get_Value(table As String, order_by As String) As Variant

Set db_data = New ADODB.Recordset

Set db1 = New ADODB.Connection

pDB_path = "#enter db-path here"

db1.ConnectionString = _
    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & pDB_path & ";Persist Security Info=False;"

db1.Open


With db_data
    .ActiveConnection = db1
    .Source = "SELECT * FROM " & table & " ORDER BY " & order_by & " ASC"
    .LockType = adLockReadOnly 'read only access to db
    .CursorType = adOpenStatic 'how to update the database
    .Open
End With

get_Value = TransposeArray(db_data.GetRows)

db_data.Close

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM