将数据表导出到Excel会从HRESULT:0x800A03EC中获取异常

时间:2013-06-05 23:09:46

标签: vb.net os.execl

我正在尝试将DataTable导出到Excel 2007.当我到达Excel.Range行时,我部分导出我创建的数组但后来给我一个错误(System.Runtime.InteropServices.COMException(0x800A03EC):异常来自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

1 个答案:

答案 0 :(得分:0)

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