Private Sub Command3_Click() Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim j As Integer
Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True Set xlBook = xlApp.Workbooks.Open(App.Path & "\text.xlsx") Set xlSheet = xlBook.Worksheets(1) Sheets("sheet1").Select Sheets("sheet1").Name = "New Trial" For i = 0 To DataGrid1.Columns.Count - 1 xlSheet.Cells(1, i + 1) = DataGrid1.Columns.Item(i).Caption Next i Adodc1.Recordset.MoveFirst For i = 0 To Adodc1.Recordset.RecordCount - 1 For j = 0 To Adodc1.Recordset.Fields.Count - 1 xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j) Next j Adodc1.Recordset.MoveNext Next i
Set xlSheet = xlBook.Worksheets(2) Sheets("sheet2").Select Sheets("sheet2").Name = "Underway Trial" For i = 0 To DataGrid2.Columns.Count - 1 xlSheet.Cells(1, i + 1) = DataGrid2.Columns.Item(i).Caption Next i Adodc2.Recordset.MoveFirst For i = 0 To Adodc2.Recordset.RecordCount - 1 For j = 0 To Adodc2.Recordset.Fields.Count - 1 xlSheet.Cells(i + 2, j + 1) = Adodc2.Recordset(j) Next j Adodc2.Recordset.MoveNext Next i End Sub
Private Sub Command3_Click() Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim j As Integer
Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True Set xlBook = xlApp.Workbooks.Open(App.Path & "\text.xlsx") Set xlSheet = xlBook.Worksheets(1) Sheets("sheet1").Select Sheets("sheet1").Name = "New Trial" For i = 0 To DataGrid1.Columns.Count - 1 xlSheet.Cells(1, i + 1) = DataGrid1.Columns.Item(i).Caption Next i Adodc1.Recordset.MoveFirst For i = 0 To Adodc1.Recordset.RecordCount - 1 For j = 0 To Adodc1.Recordset.Fields.Count - 1 xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j) Next j Adodc1.Recordset.MoveNext Next i
Set xlSheet = xlBook.Worksheets(2) Sheets("sheet2").Select Sheets("sheet2").Name = "Underway Trial" For i = 0 To DataGrid2.Columns.Count - 1 xlSheet.Cells(1, i + 1) = DataGrid2.Columns.Item(i).Caption Next i Adodc2.Recordset.MoveFirst For i = 0 To Adodc2.Recordset.RecordCount - 1 For j = 0 To Adodc2.Recordset.Fields.Count - 1 xlSheet.Cells(i + 2, j + 1) = Adodc2.Recordset(j) Next j Adodc2.Recordset.MoveNext Next i End Sub
Dim a As Integer
Dim b As Integer
Dim d As Integer
On Error GoTo Ert
Me.MousePointer = 11
Dim Excelapp As Excel.Application
Set Excelapp = New Excel.Application
On Error Resume Next
DoEvents
Excelapp.SheetsInNewWorkbook = 1
Excelapp.Workbooks.Add
Excelapp.ActiveSheet.Cells(1, 3) = s
Excelapp.Range("C1").Select
Excelapp.Selection.Font.FontStyle = "Bold"
Excelapp.Selection.Font.Size = 16
Excelapp.Caption = "输出表格"
With MSFlexGrid1
d = MSFlexGrid1.Rows
For a = 0 To d - 1
For b = 0 To 5
DoEvents
Excelapp.ActiveSheet.Cells(3 + a, b + 1) = "'" & .TextMatrix(a, b)
Next b
Next a
End With
Me.MousePointer = 0
Excelapp.Visible = True
' Excelapp.Sheets.PrintPreview
Excelapp.Cells.EntireColumn.AutoFit
Ert:
If Not (Excelapp Is Nothing) Then
Excelapp.Quit
End If
Dim e As String
Dim f As Integer
Dim m As Integer
Dim n As Integer
On Error GoTo Ert2
Me.MousePointer = 11
Dim Excelapp2 As Excel.Application
Set Excelapp2 = New Excel.Application
On Error Resume Next
DoEvents
Excelapp2.SheetsInNewWorkbook = 1
Excelapp2.Workbooks.Add
Excelapp2.ActiveSheet.Cells(1, 3) = e
Excelapp2.Range("C1").Select
Excelapp2.Selection.Font.FontStyle = "Bold"
Excelapp2.Selection.Font.Size = 16
Excelapp2.Caption = "合计表格"
With MSFlexGrid2
n = MSFlexGrid2.Rows
For f = 0 To n - 1
For m = 0 To 5
DoEvents
Excelapp2.ActiveSheet.Cells(3 + f, m + 1) = "'" & .TextMatrix(f, m)
Next m
Next f
End With
Me.MousePointer = 0
Excelapp2.Visible = True
' Excelapp2.Sheets.PrintPreview
Excelapp2.Cells.EntireColumn.AutoFit
Ert2:
If Not (Excelapp2 Is Nothing) Then
Excelapp2.Quit
我写的是这样 出来的Excel表格是两个 我希望写到一个表格里面