请教VB msflexgrid控件导出到excel表格问题
界面有两个msflexgrid表格,点击command控件(只有一个)输出到excel表格。问题是:1.excel中两个表格是相互独立的,我想做到一个excel里面。2.excel表格无法调节适合大小。刚输来的有点凌乱。请教大神!!!
只能给你一个实例,你自己试着去调试吧!
程序代码:
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