2013-06-05 2 views
0

J'essaye d'exporter un DataTable à Excel 2007. Quand j'arrive à la ligne Excel.Range j'exporte partiellement le tableau que j'ai créé mais me donne une erreur (System.Runtime.InteropServices.COMException (0x800A03EC): Exception de HRESULT: 0x800A03EC). Je suis en mesure de voir les données dans la feuille Excel et cette erreur se produit à la ligne 75 des données exportées. La valeur sur laquelle il est en train de mourir est ===> REGARDEZ LES CHANGEMENTS DE SEEPAGE - DES SÉPAGES SUBSTANTIELS SE PRODUISENT EN AVAL DU REMBLAI, CÔTÉ EST DU RUISSEAU DANIELS.Exportation Datatable to Excel donne Exception de HRESULT: 0x800A03EC à mi-chemin

La valeur de la chaîne ExcelRange est 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 

Répondre

0

Apparemment, Excel n'aime pas que vous saisissiez = comme premier caractère dans une cellule. Remplacer le premier = avec '= le problème est résolu.

Questions connexes