【问题标题】:Exporting Datatable to Excel gives Exception from HRESULT: 0x800A03EC part way through将数据表导出到 Excel 会导致 HRESULT 出现异常:0x800A03EC 部分通过
【发布时间】:2013-06-01 19:50:37
【问题描述】:

我正在尝试将 DataTable 导出到 Excel 2007。当我到达 Excel.Range 行时,我部分导出了我创建的数组,但随后给了我一个错误 (System.Runtime.InteropServices.COMException (0x800A03EC): Exception来自 HRESULT:0x800A03EC)。我能够在 Excel 工作表中看到数据,并且此错误发生在导出数据的第 75 行。它正在消亡的价值是===>注意渗水的变化 - 大量渗水发生在路堤下游,丹尼尔斯溪东侧。

ExcelRange 字符串的值为 A1:CL1132。

Private Sub ExportExcelFast(ByVal dt As DataTable)
  Try


        Dim Excel As New Excel.Application
        Dim Wb As Microsoft.Office.Interop.Excel.Workbook
        Dim Ws As Microsoft.Office.Interop.Excel.Worksheet

        Excel.SheetsInNewWorkbook = 1
        Excel.Workbooks.Add()
        Excel.Worksheets.Select()
        Excel.Visible = True


        Dim col, row As Integer
        ' Copy the DataTable to an object array
        Dim rawData(dt.Rows.Count, dt.Columns.Count - 1) As Object

        ' Copy the column names to the first row of the object array
        For col = 0 To dt.Columns.Count - 1
            rawData(0, col) = dt.Columns(col).ColumnName.ToUpper
        Next



        ' Copy the values to the object array
        For col = 0 To dt.Columns.Count - 1
            For row = 0 To dt.Rows.Count - 1
                rawData(row + 1, col) = dt.Rows(row).ItemArray(col)
            Next
        Next


        ' Calculate the final column letter
        Dim finalColLetter As String = String.Empty
        finalColLetter = ExcelColName(dt.Columns.Count)

        Dim excelRange As String = String.Format("A1:{0}{1}", _
                                   finalColLetter, dt.Rows.Count + 1)

        Excel.Range(excelRange, Type.Missing).Value2 = rawData
        System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)

    Catch ex As Exception

            Throw

    End Try
End Sub

Public Function ExcelColName(ByVal Col As Integer) As String
    If Col < 0 And Col > 256 Then
        MsgBox("Invalid Argument", MsgBoxStyle.Critical)
        Return Nothing
        Exit Function
    End If
    Dim i As Int16
    Dim r As Int16
    Dim S As String
    If Col <= 26 Then
        S = Chr(Col + 64)
    Else
        r = CShort(Col Mod 26)
        i = CShort(System.Math.Floor(Col / 26))
        If r = 0 Then
            r = 26
            i = CShort(i - 1)
        End If
        S = Chr(i + 64) & Chr(r + 64)
    End If
    ExcelColName = S
End Function

【问题讨论】:

    标签: vb.net os.execl


    【解决方案1】:

    显然 Excel 不喜欢您输入 = 作为单元格中的第一个字符。将第一个 = 替换为 '= 问题就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2013-03-13
      相关资源
      最近更新 更多