把数据到处到excel中,并在excel中打印Private Sub Command1_Click() Dim Xlapp As Object Dim i As Long Dim j As Long Dim Header As String Dim xlsheet As Excel.Worksheet Set Xlapp = CreateObject("excel.application") Xlapp.Workbooks.Add Xlapp.Visible = True Set xlsheet = Xlapp.Worksheets.Add With xlsheet ' .Range("C1") = Header ' .Range("C1").Font.Size = 20 ' .Range("A2") = "´Ó" & DTPicker1.Value & "µ½" & DTPicker2.Value & "Ϊֹ:" For i = 1 To msgList.Rows - 1 For j = 0 To msgList.Cols - 1 .Cells(i + 1, j + 1) = msgList.TextMatrix(i, j) Next Next End With Set xlsheet = Nothing Set Xlapp = Nothing End Sub
dim strcn as string '用来保存数据库连接字符 dim strsql as string '用来保存数据库查询的SQL语句 dim cn as adodb.connection '数据库连接 dim rs as adodb.recordset '数据集合set cn = new adodb.connection set rs = new adodb.recordset strcn = "在这里写数据库的连接字符" strsql = "select 字段1,字段2,...... from 表名;" '读recordset中的值----------------------- '这一段中的代码根据你的要求修改,我就不多写了. ..... = rs.fields("字段1") ..... = rs.fields("字段2") '可以用rs.movenext 来向下移动记录 '---------------------------------------- cn.open strcn rs.open strsql,cnrs.close cn.close
这是我用EXCEL做打印的代码
xayzmb(行者) 请问又如何读取DataGrid里的内容呢?????
Dim Xlapp As Object
Dim i As Long
Dim j As Long
Dim Header As String
Dim xlsheet As Excel.Worksheet
Set Xlapp = CreateObject("excel.application")
Xlapp.Workbooks.Add
Xlapp.Visible = True
Set xlsheet = Xlapp.Worksheets.Add
With xlsheet
' .Range("C1") = Header
' .Range("C1").Font.Size = 20
' .Range("A2") = "´Ó" & DTPicker1.Value & "µ½" & DTPicker2.Value & "Ϊֹ:"
For i = 1 To msgList.Rows - 1
For j = 0 To msgList.Cols - 1
.Cells(i + 1, j + 1) = msgList.TextMatrix(i, j)
Next
Next
End With
Set xlsheet = Nothing
Set Xlapp = Nothing
End Sub
直接读取数据源中的数据,如adodc或者recordset
datagrid1.col = 列号
msgbox datagrid1.text '这样可以读出来datagrid1控件中某一单元格中的值
用循环可以读出datagrid控件中的内容但是如果datagrid中的数据是从数据库中取出的话
建议不这样做
可以直接读rescordset中的值写入excel中.
dim strsql as string '用来保存数据库查询的SQL语句
dim cn as adodb.connection '数据库连接
dim rs as adodb.recordset '数据集合set cn = new adodb.connection
set rs = new adodb.recordset strcn = "在这里写数据库的连接字符"
strsql = "select 字段1,字段2,...... from 表名;"
'读recordset中的值-----------------------
'这一段中的代码根据你的要求修改,我就不多写了.
..... = rs.fields("字段1")
..... = rs.fields("字段2")
'可以用rs.movenext 来向下移动记录
'----------------------------------------
cn.open strcn
rs.open strsql,cnrs.close
cn.close