écrire un tableau de strings dans excel via vb.net
cs_ShayW
Messages postés3253Date d'inscriptionjeudi 26 novembre 2009StatutMembreDerniè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))"
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
cs_ShayW
Messages postés3253Date d'inscriptionjeudi 26 novembre 2009StatutMembreDernière intervention 3 décembre 201957 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