从 sqlserver HRESULT 填充 Excel 工作簿:0x800401A8 错误

Posted

技术标签:

【中文标题】从 sqlserver HRESULT 填充 Excel 工作簿:0x800401A8 错误【英文标题】:Filling Excel Workbook from sqlserver HRESULT: 0x800401A8 error 【发布时间】:2015-07-13 15:07:10 【问题描述】:

我正在尝试从 SQL Server 数据库中填写一个 Excel 表并收到一条带有代码的消息

HRESULT:0x800401A8

请帮助我。

   Dim file As String = "C:\Techform.xlsx"


    Dim xlApp_2 As New Excel.Application
    xlApp_2.Visible = False
    xlApp_2.DisplayAlerts = False
    Dim oBook As Excel.Workbook
    oBook = xlApp_2.Workbooks.Open(file)

    Dim oSheet As Excel.Worksheet


    oSheet = oBook.Worksheets(1)
    objConnection.Open()
    adapter_view.SelectCommand = New SqlCommand()
    adapter_view.SelectCommand.Connection = objConnection
    adapter_view.SelectCommand.CommandText = "SELECT * FROM TECHNICAL_AMS_CALC_GARGANTUA "
    adapter_view.SelectCommand.CommandType = CommandType.Text
    adapter_view.Fill(dataset, "GARGANTUA_AVERAGE_Data")
    dataTable_CALC = dataset.Tables("GARGANTUA_AVERAGE_Data")
    adapter_view.Dispose()
    For h As Integer = 0 To dataTable_CALC.Rows.Count - 1


        SoG_AVG = dataTable_CALC.Rows(h).Item("VPM_SPEED_THROUGH_WATER_AVG")
        oSheet.Range("E3").Value = dataTable_CALC.Rows(h).Item("VPM_SPEED_THROUGH_WATER_AVG")
        oSheet.Range("F3").Value = dataTable_CALC.Rows(h).Item("VPM_SPEED_THROUGH_WATER_MAX")



        oBook.SaveAs(file + " " + dataTable_CALC.Rows(h).Item("ACTION_DATE") + ".xlsx", CreateBackup:=False)
        oBook.Close()
        xlApp_2.Quit()
    Next

【问题讨论】:

尝试移动 Nextabove oBook.SaveAs 我无法理解你的意思 【参考方案1】:

像这样:

    Dim file As String = "C:\Techform.xlsx"

    Dim xlApp_2 As New Excel.Application
    xlApp_2.Visible = False
    xlApp_2.DisplayAlerts = False
    Dim oBook As Excel.Workbook
    oBook = xlApp_2.Workbooks.Open(file)

    Dim oSheet As Excel.Worksheet


    oSheet = oBook.Worksheets(1)
    objConnection.Open()
    adapter_view.SelectCommand = New SqlCommand()
    adapter_view.SelectCommand.Connection = objConnection
    adapter_view.SelectCommand.CommandText = "SELECT * FROM TECHNICAL_AMS_CALC_GARGANTUA "
    adapter_view.SelectCommand.CommandType = CommandType.Text
    adapter_view.Fill(dataset, "GARGANTUA_AVERAGE_Data")
    dataTable_CALC = dataset.Tables("GARGANTUA_AVERAGE_Data")
    adapter_view.Dispose()
    For h As Integer = 0 To dataTable_CALC.Rows.Count - 1

        SoG_AVG = dataTable_CALC.Rows(h).Item("VPM_SPEED_THROUGH_WATER_AVG")
        oSheet.Range("E3").Value = dataTable_CALC.Rows(h).Item("VPM_SPEED_THROUGH_WATER_AVG")
        oSheet.Range("F3").Value = dataTable_CALC.Rows(h).Item("VPM_SPEED_THROUGH_WATER_MAX")

    Next
    oBook.SaveAs(file + " " + dataTable_CALC.Rows(h).Item("ACTION_DATE") + ".xlsx", CreateBackup:=False)
    oBook.Close()
    xlApp_2.Quit()

【讨论】:

以上是关于从 sqlserver HRESULT 填充 Excel 工作簿:0x800401A8 错误的主要内容,如果未能解决你的问题,请参考以下文章

刚安装sql server2014,就遇到以下问题 : 异常来自HRESULT:0X8024402C,不知道 是啥原因,请指教?

如何更正 hresult error.ms sql server

找不到适合 jdbc:sqlserver:ex;databaseName=Cities_names 的驱动程序

Azure 远程调试:无法从 HRESULT 启用远程调试异常:0x89710023

从 C# 调用 COM dll,HRESULT 错误处理?

尝试从 UWP WebView 获取 html 时出现异常(HRESULT 异常:0x80020101)