主题:自定义段从MSFlexGrid1导出数据到Excel
有十个字段,制作十个CheckBox数组控件,控件标题分别对应十个字段名称,导出时只导出选中的复选框相对应的字段。
以下代码不知哪错了,如果复先框依次选中的话导出是正确的,如果隔开索引号选中(选了索引为2、3后,又选索引为6或8,只要不连续的)复选框。导出时的字段名称就不对。
Dim g, X As Integer
Dim gy(21) As Integer
g = 0
For i = 0 To 20
If Me.Check1(i).Value = 1 Then
g = g + 1
gy(i) = i
End If
Next i
If g = 0 Then
MsgBox "请至少选择一个导出项!", 64, "提示:"
Exit Sub
Else
X = Me.MSFlexGrid1.Rows '行数
Set objExlBook = objExlApp.Workbooks.Add
Set objExlSheet = objExlBook.Sheets(1)
For i = 1 To g
objExlSheet.Cells(1, i) = MSFlexGrid1.TextMatrix(0, gy(i))
Next i
End If
objExlSheet.SaveAs "D:\book1.xls"
objExlApp.Visible = True
以下代码不知哪错了,如果复先框依次选中的话导出是正确的,如果隔开索引号选中(选了索引为2、3后,又选索引为6或8,只要不连续的)复选框。导出时的字段名称就不对。
Dim g, X As Integer
Dim gy(21) As Integer
g = 0
For i = 0 To 20
If Me.Check1(i).Value = 1 Then
g = g + 1
gy(i) = i
End If
Next i
If g = 0 Then
MsgBox "请至少选择一个导出项!", 64, "提示:"
Exit Sub
Else
X = Me.MSFlexGrid1.Rows '行数
Set objExlBook = objExlApp.Workbooks.Add
Set objExlSheet = objExlBook.Sheets(1)
For i = 1 To g
objExlSheet.Cells(1, i) = MSFlexGrid1.TextMatrix(0, gy(i))
Next i
End If
objExlSheet.SaveAs "D:\book1.xls"
objExlApp.Visible = True