datagrid里的数值写入到excel表中指定位置
datagrid里的数值显示如下11 000
12 111
13 222
14 333
15 444
16 555
17 777
。 。
。 。
。 。
要把这两列的数值分别写到excel表中 如下
A B C D E F G
1
2
3
4 11 111
5 12 222
6 13 333
7 . .
8 . .
9 . .
10
11
这vb6.0代码怎么写? --------------------编程问答-------------------- 补充上面:11值对应excel表中4D 111值对应excel表中4E
12值对应excel表中5D 222值对应excel表中5E --------------------编程问答--------------------
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim Irow, Icol As Integer
Dim Irowcount, Icolcount As Integer
Dim Fieldlen() As Integer
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(1)
'--------------------------------------------导出数据至excell中-----------------------------------------
With Adodc1.Recordset
Adodc1.Recordset.MoveLast
If .RecordCount < 1 Then
MsgBox ("Error 没有记录!")
Exit Sub
End If
Irowcount = .RecordCount '记录总数
Icolcount = .Fields.Count '字段总数
ReDim Fieldlen(Icolcount) As Integer
Adodc1.Recordset.MoveFirst
For Irow = 1 To Irowcount + 1
For Icol = 1 To Icolcount
Select Case Irow
Case 1 '在Excel中的第一行加标题
xlsheet.Cells(Irow, Icol).Value = RTrim(.Fields(Icol - 1).Name)
Case 2 '将数组FIELDLEN()存为第一条记录的字段长
If IsNull(.Fields(Icol - 1)) = True Then
Fieldlen(Icol) = LenB(RTrim(.Fields(Icol - 1).Name))
Else
aa = RTrim(.Fields(Icol - 1))
Fieldlen(Icol) = LenB(aa)
End If
xlsheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
xlsheet.Cells(Irow, Icol).Value = RTrim(.Fields(Icol - 1))
Case Else
Fieldlen1 = LenB(.Fields(Icol - 1))
If Fieldlen(Icol) < Fieldlen1 Then
xlsheet.Columns(Icol).ColumnWidth = Fieldlen1
Fieldlen(Icol) = Fieldlen1
Else
xlsheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
End If
xlsheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
End Select
Next
If Irow <> 1 Then
If Not .EOF Then .MoveNext
End If
Next
End With
'-------------------------------------定义excell标题字体---------------------------
With xlsheet
.Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑体"
.Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
.Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous --------------------编程问答--------------------
Dim xlApp As Excel.Application--------------------编程问答-------------------- 高人,他是要写入 excel表中指定的位置 比如:datagrid里第一数值在excel表中是第三列第四行,后面的依次而下 --------------------编程问答--------------------
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim Irow, Icol As Integer
Dim Irowcount, Icolcount As Integer
Dim Fieldlen() As Integer
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(1)
xlsheet.Cells(4, 1).Value = datagrid1.item(0,0)
'............ 若有规律,可以用循环
数据很多,不好实现 --------------------编程问答-------------------- for i=1 to totalrow
xlsheet.Cells(i+3, 4).Value = datagrid1.item(i-1,0)
xlsheet.Cells(i+3, 5).Value = datagrid1.item(i,1)
next i
补充:VB , 基础类