0

我正在尝试将 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
4

1 回答 1

0

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

于 2013-06-05T23:20:26.643 回答