天下三一天一亿经验:用VB做成的软件当中,要当中的可填区的内容自动填写到EXCEL中的某一个单元格,应该使用哪些具体的语句?

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 06:28:00
比如说要将这个单元格中的内容自动填写到EXCEL表格中,应该怎么做?
Text1.Text = Data2.Recordset.Fields("收货单位")
谢谢各位大哥大姐了!

1 打开Excel文件的表单(可以启动Excel后台运行):
Dim Sht As Worksheet
Set Bk = Excel.Workbooks.Open(strFullName)
Set Sht = Bk.Worksheets("Sheet1")
2 写入值到单元格
sht.range("A1")=text1.text
sht.range("A2")=Data2.Recordset.Fields("收货单位")
...
3 保存并关闭Excel
bk.save
bk.close
实现的方法有多种,供您参考。

关注

给你一个例子
Private Sub ExportDetail(strFileName As String)
Dim iRow As Long
Dim myExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set myExcel = New Excel.Application

myExcel.Visible = False
myExcel.SheetsInNewWorkbook = 1
Set xlBook = myExcel.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

xlSheet.Columns.ClearFormats
xlSheet.Cells(1, 1) = "下料明细表【" & treeCatalog.SelectedItem.Text & "】"
xlSheet.Range("A1:Q1").MergeCells = True
xlSheet.Range("I2:Q2").MergeCells = True
xlSheet.Range("A2:A3").MergeCells = True
xlSheet.Range("E2:E3").MergeCells = True
xlSheet.Range("F2:F3").MergeCells = True
xlSheet.Range("G2:G3").MergeCells = True
xlSheet.Range("H2:H3").MergeCells = True

xlSheet.Cells(2, 1) = "序 号"
xlSheet.Cells(2, 2) = "零件图号"
xlSheet.Cells(2, 3) = "属装图号"
xlSheet.Cells(2, 4) = "物料名称"
xlSheet.Cells(2, 5) = "数 量"
xlSheet.Cells(2, 6) = "工艺分工"
xlSheet.Cells(2, 7) = "备注"
xlSheet.Cells(2, 8) = "分类名称"
xlSheet.Cells(2, 9) = "工艺流程"
xlSheet.Cells(2, 10) = "工艺流程"
xlSheet.Cells(2, 11) = "工艺流程"
xlSheet.Cells(2, 12) = "工艺流程"
xlSheet.Cells(2, 13) = "工艺流程"
xlSheet.Cells(2, 14) = "工艺流程"
xlSheet.Cells(2, 15) = "工艺流程"
xlSheet.Cells(2, 16) = "工艺流程"
xlSheet.Cells(2, 17) = "工艺流程"
xlSheet.Cells(3, 1) = "序 号"
xlSheet.Cells(3, 2) = "零件名称"
xlSheet.Cells(3, 3) = "属装序号"
xlSheet.Cells(3, 4) = "规格尺寸"
xlSheet.Cells(3, 5) = "数量"
xlSheet.Cells(3, 6) = "工艺分工"
xlSheet.Cells(3, 7) = "备注"
xlSheet.Cells(3, 8) = "分类名称"
xlSheet.Cells(3, 9) = "1"
xlSheet.Cells(3, 10) = "2"
xlSheet.Cells(3, 11) = "3"
xlSheet.Cells(3, 12) = "4"
xlSheet.Cells(3, 13) = "5"
xlSheet.Cells(3, 14) = "6"
xlSheet.Cells(3, 15) = "7"
xlSheet.Cells(3, 16) = "8"
xlSheet.Cells(3, 17) = "9"

For iRow = 2 To grdDetail.Rows - 1

If iRow Mod 2 = 0 Then
xlSheet.Cells(iRow + 2, 1) = "'" & grdDetail.TextMatrix(iRow, 0)
xlSheet.Range(xlSheet.Cells(iRow, 1), xlSheet.Cells(iRow + 1, 1)).MergeCells = True
End If
xlSheet.Cells(iRow + 2, 2) = "'" & grdDetail.TextMatrix(iRow, 1)
xlSheet.Cells(iRow + 2, 3) = "'" & grdDetail.TextMatrix(iRow, 2)
xlSheet.Cells(iRow + 2, 4) = "'" & grdDetail.TextMatrix(iRow, 3)
xlSheet