導(dǎo)出到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
用戶類型未定義 回答人的補充 2009-08-15 14:49
不好意思,忘記說了 。 在工程——引用里,,添加Microsoft Excel 11.0 Object Library即可 回答人的補充 2009-08-15 14:49
提問人的追問 2009-08-15 14:52
恩~~除了標題外~~沒有任何內(nèi)容~ 回答人的補充 2009-08-15 14:58
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 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
問下~要是不導(dǎo)出可以直接打印那個DataGrid1里面的內(nèi)容嗎,? 回答人的補充 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 "學(xué)號/姓名 /課程/成績/班級" 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 '無此條件則打印機與計算機不斷開,,不能進行新的打印任務(wù) 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 |
|