VB如何导入到Excel数据
发布网友
发布时间:2022-10-09 06:00
我来回答
共1个回答
热心网友
时间:2023-11-22 18:56
不知道你的控件是怎么弄的,我的控件LABLE,TEXTBOX都是控件数组,这样可以让程序简化,另外为了让下次输入方便,我在导入操作完成后,将TEXTBOX置空了,不需要的自己删掉。程序如下:
Dim row As Integer
Dim col As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlBook = xlApp.Workbooks.Open("H:\t.xls")
Set xlSheet = xlBook.Worksheets("Sheet1")
row = 1
If xlSheet.cells(1, 1) = "" Then
For i = 0 To 6
xlSheet.cells(1, i + 1) = Label1(i).Caption
xlSheet.cells(2, i + 1) = Text1(i).Text
Text1(i).Text = "" ‘将TEXTBOX置空
Next
Else
Do While xlSheet.cells(row, 1) <> ""
row = row + 1
Loop
For col = 0 To 6
xlSheet.cells(row, col + 1) = Text1(col).Text
Text1(col).Text = "" ’将TEXTBOX置空
Next
End If
xlBook.Save
xlApp.Quit