Hanyu_Li 2017-11-23 09:39 采纳率: 0%
浏览 1087

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

执行结果都是数据导出失败,有没有大神帮忙看看应该怎么改动呢?
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条回答 默认 最新

  • wild84 2018-03-05 00:44
    关注

    调试查看 Err.number的值是多少?可获得具体原因然后具体分析

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)