for i = 0 to grid.rows-1 for j = 0 to grid.cols-1 grid.row = i grid.col = j print grid.text next j next i
例子:在你的FORM中加入,一个名为MSFlexGrid1 的MSFlexGrid1 控件和一个名为command1的command控件Sub command1_click(MSFlexGrid1 As MSFlexGrid) On Error GoTo ErrHandler Dim EXCELApp As Excel.Application Dim EXCELWorkBook As Excel.Workbook Dim xlSheet As Excel.WorksheetDim rows, cols As Integer Dim Irow, hCol, Icol As Integer Dim New_Col As Boolean If MSFlexGrid1.rows <= 1 Then MsgBox "还没有数据记录", vbInformation, App.Title Exit Sub End If
Set EXCELApp = CreateObject("Excel.application") Set EXCELWorkBook = EXCELApp.Workbooks.Add Set xlSheet = EXCELWorkBook.Sheets(1) Dim New_Column As Boolean
With MSFlexGrid1 rows = .rows cols = .cols Irow = 0 Icol = 1 For hCol = 0 To cols - 1 For Irow = 1 To rows EXCELApp.Cells(Irow + 1, Icol).Value = .TextMatrix(Irow - 1, hCol) Next Irow Icol = Icol + 1 Next hCol End With 'xlsheet.Range(xlsheet.Cells(2, 0), xlsheet.Cells(rows, cols - 1)).Borders.LineStyle = xlContinuous
EXCELApp.rows(2).Font.Bold = True EXCELApp.Cells.Select EXCELApp.Columns.AutoFit EXCELApp.Cells(1, 1).Select EXCELApp.Application.Visible = True 'EXCELApp.Sheets.PrintPreview Set EXCELWorkBook = Nothing Set EXCELApp = Nothing MSFlexGrid1.SetFocus ' MsgBox "Data has been successfully copied to Excel. ", vbInformation, "Success" Exit Sub ErrHandler: MsgBox "报表错误", vbInformation, "报表错误" End Sub
for j = 0 to grid.cols-1
grid.row = i
grid.col = j
print grid.text
next j
next i
On Error GoTo ErrHandler
Dim EXCELApp As Excel.Application
Dim EXCELWorkBook As Excel.Workbook
Dim xlSheet As Excel.WorksheetDim rows, cols As Integer
Dim Irow, hCol, Icol As Integer
Dim New_Col As Boolean
If MSFlexGrid1.rows <= 1 Then
MsgBox "还没有数据记录", vbInformation, App.Title
Exit Sub
End If
Set EXCELApp = CreateObject("Excel.application")
Set EXCELWorkBook = EXCELApp.Workbooks.Add
Set xlSheet = EXCELWorkBook.Sheets(1)
Dim New_Column As Boolean
With MSFlexGrid1
rows = .rows
cols = .cols
Irow = 0
Icol = 1
For hCol = 0 To cols - 1
For Irow = 1 To rows
EXCELApp.Cells(Irow + 1, Icol).Value = .TextMatrix(Irow - 1, hCol)
Next Irow
Icol = Icol + 1
Next hCol
End With
'xlsheet.Range(xlsheet.Cells(2, 0), xlsheet.Cells(rows, cols - 1)).Borders.LineStyle = xlContinuous
EXCELApp.rows(2).Font.Bold = True
EXCELApp.Cells.Select
EXCELApp.Columns.AutoFit
EXCELApp.Cells(1, 1).Select
EXCELApp.Application.Visible = True
'EXCELApp.Sheets.PrintPreview
Set EXCELWorkBook = Nothing
Set EXCELApp = Nothing
MSFlexGrid1.SetFocus
' MsgBox "Data has been successfully copied to Excel. ", vbInformation, "Success"
Exit Sub
ErrHandler:
MsgBox "报表错误", vbInformation, "报表错误"
End Sub