VB如何操作EXCEL数据
我用VB把数据存在EXCEL里面,Text1和Text2里面放得EXECEL两列的数据,但是我在这两个文本框里面输入新的数据后不知道怎么添加到EXCEL里面存储,也是添加按钮的代码写不来,怎么写,谁教我一下,最好举个例子,谢谢 问题点数:50、回复次数:3Top
1 楼chuting(学习的动力)回复于 2006-06-04 17:11:28 得分 0
Private Sub command1_click()
Dim i As Integer
Dim NewApp As New Excel.Application '类
Dim newBook As New Excel.Workbook '区
Dim NewSheet As New Excel.Worksheet '表
Me.MousePointer = 11
Set newBook = NewApp.Workbooks.Add
Set NewSheet = newBook.Worksheets(1)
NewSheet.Columns.Font.Size = 9
Dim strProv As String
Dim strName As String
Dim intNum As Integer
Dim strPhone As String
strProv = "某某省市"
strName = "人物姓名"
intNum = 5 '为输入件数
strPhone = "0514-6430199"
'這上面的四個變量就是對應的文本框的值
Dim intLoop As Integer
If intNum Mod 2 = 0 Then
intLoop = intNum \ 2
Else
intLoop = intNum \ 2 + 1
End If
i = 1
Do Until i > intLoop
NewSheet.Cells(4 * i - 3, 1) = "收货地址:" '
NewSheet.Cells(4 * i - 3, 2) = strProv
NewSheet.Cells(4 * i - 2, 1) = "收货人:"
NewSheet.Cells(4 * i - 2, 2) = strName
NewSheet.Cells(4 * i - 2, 3) = "第" & 2 * i - 1 & "件 共" & intNum & "件"
NewSheet.Cells(4 * i - 1, 1) = "联系电话:"
NewSheet.Cells(4 * i - 1, 2) = strPhone
If 2 * i > intNum Then Exit Do
NewSheet.Cells(4 * i - 3, 5) = "收货地址:"
NewSheet.Cells(4 * i - 3, 6) = strProv
NewSheet.Cells(4 * i - 2, 5) = "收货人:"
NewSheet.Cells(4 * i - 2, 6) = strName
NewSheet.Cells(4 * i - 2, 7) = "第" & 2 * i & "件 共" & intNum & "件"
NewSheet.Cells(4 * i - 1, 5) = "联系电话:"
NewSheet.Cells(4 * i - 1, 6) = strPhone
i = i + 1
Loop
NewSheet.Columns.AutoFit
NewApp.Visible = False
Me.MousePointer = 0
NewSheet.PrintOut
newBook.Close False
Set NewApp = Nothing
Set NewSheet = Nothing
Set newBook = Nothing
End Sub
Top
2 楼chuting(学习的动力)回复于 2006-06-04 17:12:05 得分 0
有打印,如果要保存你自己再研究一下Top
3 楼of123()回复于 2006-06-04 17:49:09 得分 0
采用数据库引擎可以直接将数据插入 Excel:(需要引用 Microsoft ADO 2.1 以上版本)
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended
Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
cn.Execute "INSERT INTO [Sheet1](第一列标题, 第二列标题) VALUES('" & Text1 & "','" & Text2 "')"
cn.Close
Set cn = Nothing
End SubTop




