écrire un tableau de strings dans excel via vb.net

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - 27 févr. 2012 à 00:26
 Utilisateur anonyme - 27 févr. 2012 à 13:08
Bonjour à tous

j'ai une liste de string et je veux écrire
chaque caractère dans chaque cell
de B1 à X1 X selon la longueur du string
le prochain string
de B2 à X2
j'ai écrit le code suivant
Imports excel = Microsoft.Office.Interop.Excel
Public Class ClasseExcel
    Private objexcel As New excel.Application
    Private Sub WriteStrings()
        Dim iterchar As Integer
        Dim cellstrcopy As String
        Dim indexcol As Integer
        Dim indexrow As Integer
        Try
            With objexcel.ActiveSheet
                indexcol = 66
                indexrow = 1
                For Each item In Form2.liststrings
                    cellstrcopy = Convert.ToChar(indexcol) & indexrow.ToString
                    For iterchar = 0 To item.Length - 1
                        Dim range = objexcel.Range(cellstrcopy)
                        range(cellstrcopy) = item(iterchar)
                        indexcol += 1
                        cellstrcopy = Convert.ToChar(indexcol) & indexrow.ToString
                    Next
                    indexcol = 66
                    indexrow += 1
                Next
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
       
    End Sub
    Public Sub ShowExcel()
        WriteStrings()
        objexcel.Visible = True
        objexcel = Nothing
    End Sub
    Public Sub New()
        objexcel.SheetsInNewWorkbook = 1
        objexcel.Workbooks.Add()
    End Sub
End Class


une erreur quand je lance le prog

"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"


Merci

4 réponses

Utilisateur anonyme
27 févr. 2012 à 12:22
Bonjour Shay,

Moi j'ai vu ça:
 For iterchar = 0 To form2.liststrings.Length - 1
                        Dim range = objexcel.Range(cellstrcopy)
                        range(cellstrcopy) = form2.liststrings(iterchar)
                        indexcol += 1
                        cellstrcopy = Convert.ToChar(indexcol) & indexrow.ToString
                    Next



Cordialement


CF2i - Guadeloupe
Ingénierie Informatique
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
27 févr. 2012 à 12:30
j'ai corrigé

Imports excel = Microsoft.Office.Interop.Excel
Public Class ClasseExcel
    Private objexcel As New excel.Application
    Dim xlBook As excel.Workbook
    Dim xlworksheet As excel.Worksheet
    Private Sub WriteStrings()
        Dim iterchar As Integer
        Dim indexcol As Integer
        Dim indexrow As Integer
        objexcel.Visible = True
        Try
            With xlworksheet
                indexcol = 1
                indexrow = 1
                For Each item In Form2.liststrings
                    cellstrcopy = Convert.ToChar(indexcol) & indexrow.ToString
                    For iterchar = 0 To item.Length - 1
                        .Cells(indexrow, indexcol) = item(iterchar).ToString
                        indexcol += 1
                       
                    Next
                    indexcol = 1
                    indexrow += 1
                Next
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
       
    End Sub
    Public Sub ShowExcel()
        WriteStrings()
        objexcel.Visible = True
        objexcel = Nothing
    End Sub
    Public Sub New()
        xlBook = objexcel.Workbooks.Add
        xlworksheet = CType(xlBook.ActiveSheet, excel.Worksheet)
    End Sub
End Class
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
27 févr. 2012 à 12:40
Bonjour Acive

3 heures dans le pétrin enfin j'ai corrigé
en copiant un exemple
l'erreur était du au range

Excel et moi il va falloir qu'on s'entende
0
Utilisateur anonyme
27 févr. 2012 à 13:08
Excel et moi il va falloir qu'on s'entende


Ne te laisse pas faire!...
Impose lui tes conditions...
Tu lui dis c'est comme ça et pas autrement!...


Cordialement


CF2i - Guadeloupe
Ingénierie Informatique
0
Rejoignez-nous