Hanyu_Li
Hanyu_Li
2017-11-23 09:39

求VB大神协助!将VB界面的MSFlexGrid中的数据导出到Excel指定的单元格

  • excel
  • 数据
  • vb

执行结果都是数据导出失败,有没有大神帮忙看看应该怎么改动呢?
Public Function ExporttoExcel(flex As MSFlexGrid)
On Error GoTo ErrHandler

Dim xlApp As Object
Dim xlBook As Object
Dim Rows As Integer, Cols As Integer
Dim iRow As Integer, hCols As Integer, iCol As Integer
Dim New_Col As Boolean
Dim New_Column As Boolean

'g_CommonDialog.CancelError = True
On Error GoTo ErrHandler
'设置标志
'g_CommonDialog.Flags = cdlOFNHideReadOnly
'设置过滤器
'g_CommonDialog.Filter = "All Fills (*.*)|*.*|Excel Files" & "(*.xls)|*xls|Batch Files (*.bat)|*.bat"
'指定缺省的过滤器
'g_CommonDialog.FilterIndex = 2
'显示“打开”对话框
'g_CommonDialog.ShowSave

If flex.Rows <= 1 Then
MsgBox "没有数据!", vbInformation, g_Msgtitle
Exit Function
End If

Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Open(FileName)
xlApp.Visible = False

With flex
xlApp.Cells(2, 3).Value = .TextMatrix(0, 0)
xlApp.Cells(6, 6).Value = .TextMatrix(0, 1)
xlApp.Cells(7, 3).Value = .TextMatrix(0, 2)
xlApp.Cells(7, 10).Value = .TextMatrix(0, 3)
xlApp.Cells(7, 13).Value = Grid1.TextMatrix(0, 4)
xlApp.Cells(11, 4).Value = Grid1.TextMatrix(0, 5)
xlApp.Cells(11, 5).Value = Grid1.TextMatrix(0, 6)
xlApp.Cells(11, 6).Value = Grid1.TextMatrix(0, 7)
xlApp.Cells(11, 7).Value = Grid1.TextMatrix(0, 8)
End With

With xlApp
.Rows(1).Font.Bold = True
.Cells.Select
.Columns.AutoFit
.Cells(1, 1).Select
.Application.Visible = True
End With

xlBook.SaveAs (g_CommonDialog.FileName)
xlApp.Application.Visible = False
xlApp.DisplayAlerts = False
xlApp.Quit
Set xlApp = Nothing
Set xlApp = Nothing
flex.SetFocus
MsgBox "数据已经导出到Excel中。", vbInformation, "成功"
Exit Function

ErrHandler:
'用户按了“取消”按钮
If Err.Number <> 32755 Then
MsgBox "数据导出失败!", vbInformation, "失败"
End If
End Function

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答