心随意动下一句:VB DataGrid 的导出或打印
来源:百度文库 编辑:偶看新闻 时间:2024/04/29 00:36:18
导出到d:\text2.xls中
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
xlSheet.Cells(6, 1) = "i"
adodc1.Recordset.MoveFirst
For i = 0 To adodc1.Recordset.recordcount - 1
'For j = 0 To DataGrid1.Columns.Count - 1 '这一行跟下一行效果一样
For j = 0 To adodc1.Recordset.fields.Count - 1
xlSheet.Cells(i + 1, j + 1) = adodc1.Recordset(j)
Next j
adodc1.Recordset.movenext
Next i
End Sub
提问人的追问 2009-08-15 14:30
Dim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
xlSheet.Cells(6, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text
If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text
End If
Next j
Next i
提问人的追问 2009-08-15 14:59
On Error GoTo ErrMsg
fnt = 15
X = 1000
Y = 1000
Dim i As Long, J As Long, K As Long
Dim PrintString As String
Y = 4400
Printer.CurrentX = 1000
Printer.CurrentY = 4000
Printer.FontSize = 13
Printer.Print "学号/姓名 /课程/成绩/班级"
For i = 0 To DataGrid1.Row
If K = DataGrid1.VisibleRows Then
DataGrid1.Scroll 0, DataGrid1.VisibleRows
K = 0
End If
For J = 0 To DataGrid1.Columns.Count - 1
PrintString = PrintString & _
DataGrid1.Columns(J).CellText(DataGrid1.RowBookmark(K)) & "/"
Next
Printer.CurrentX = 1000
Printer.CurrentY = Y
Printer.FontSize = 10
Printer.Print PrintString
PrintString = ""
K = K + 1
Y = Y + 300
DoEvents
If i = DataGrid1.Row Then '无此条件则打印机与计算机不断开,不能进行新的打印任务
Printer.EndDoc
End If
Next
ErrMsg:
If Err.Number <> 0 Then
MsgBox CStr(Err.Number) + Err.Description, vbOKOnly + vbCritical, "错误提示"
Exit Sub
End If
End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
xlSheet.Cells(6, 1) = "i"
adodc1.Recordset.MoveFirst
For i = 0 To adodc1.Recordset.recordcount - 1
'For j = 0 To DataGrid1.Columns.Count - 1 '这一行跟下一行效果一样
For j = 0 To adodc1.Recordset.fields.Count - 1
xlSheet.Cells(i + 1, j + 1) = adodc1.Recordset(j)
Next j
adodc1.Recordset.movenext
Next i
End Sub
提问人的追问 2009-08-15 14:30
用户类型未定义回答人的补充 2009-08-15 14:49
不好意思,忘记说了 。
在工程——引用里,添加Microsoft Excel 11.0 Object Library即可
回答人的补充 2009-08-15 14:49 提问人的追问 2009-08-15 14:52恩~~除了标题外~~没有任何内容~
回答人的补充 2009-08-15 14:58 Dim i As IntegerDim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
xlSheet.Cells(6, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text
If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text
End If
Next j
Next i
提问人的追问 2009-08-15 14:59
问下~要是不导出可以直接打印那个DataGrid1里面的内容吗?回答人的补充 2009-08-15 15:02
可以的!
Private Sub Command3_Click()
On Error GoTo ErrMsg
fnt = 15
X = 1000
Y = 1000
Dim i As Long, J As Long, K As Long
Dim PrintString As String
Y = 4400
Printer.CurrentX = 1000
Printer.CurrentY = 4000
Printer.FontSize = 13
Printer.Print "学号/姓名 /课程/成绩/班级"
For i = 0 To DataGrid1.Row
If K = DataGrid1.VisibleRows Then
DataGrid1.Scroll 0, DataGrid1.VisibleRows
K = 0
End If
For J = 0 To DataGrid1.Columns.Count - 1
PrintString = PrintString & _
DataGrid1.Columns(J).CellText(DataGrid1.RowBookmark(K)) & "/"
Next
Printer.CurrentX = 1000
Printer.CurrentY = Y
Printer.FontSize = 10
Printer.Print PrintString
PrintString = ""
K = K + 1
Y = Y + 300
DoEvents
If i = DataGrid1.Row Then '无此条件则打印机与计算机不断开,不能进行新的打印任务
Printer.EndDoc
End If
Next
ErrMsg:
If Err.Number <> 0 Then
MsgBox CStr(Err.Number) + Err.Description, vbOKOnly + vbCritical, "错误提示"
Exit Sub
End If
End Sub
VB 打印DataGrid 表格里的内容
紧急!!!VB.NET中如何打印DATAGRID数据
VB 中,能不能让DataGrid 控件中的第一列或前两列的数据不能被修改?
关于VB的DataGrid控件的使用
关于vb中datagrid的问题!
Vb中关于datagrid的请教
vb(sql)数据库,与datagrid的连接
将DataGrid数据导出到Excel文档的问题
VB中过程间调用RS或DATAGRID记录
vb中我打印当前窗口中的datagrid中的数据怎么编写代码?
有谁知道coreldraw12导出打印的问题?
怎样获得vb的datagrid中单元格的数据
关于VB的DataGrid空件的问题
怎样控制vb里datagrid控件每列的长度
vb中的datagrid问题,如何设置每列的宽度
vb 如何实现datagrid中数据的修改和添加
vb如果捕捉datagrid 所单击行的某一列
关于VB中datagrid控件的一个问题
VB datagrid的修改、删除、增加与显示问题
VB的数据保存和导出!
关于VB打印的问题?
vb中怎么用datagrid?
vb datagrid不显示数据
vb中datagrid控件问题