Ecrire dans un fichier Excel
Bonjour à tous,
J'ai suivi le tuto http://faqvbnet.developpez.com/?page...dgrid_to_excel afin de pouvoir remplir un fichier Excel
Si je fais ça,
Code:
1 2 3 4 5 6 7 8 9
| Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
xlSheet.Cells(1, 1) = 13 |
ça marche correctement, mais si je rajoute une boucle toute bête telle que
Code:
1 2 3
| For j As Integer = 1 To 13
xlSheet.Cells(0, j).Value2 = 1
Next |
j'ai un message d'erreur COMException
Citation:
Exception de HRESULT : 0x800A03EC
J'ai aussi essayé avec un .Cells(0,j) = 1, la même erreur est levée...
Quelqu'un aurait-il une idée d'où cela peut-il venir ?
Je précise que j'ai importé Interop.Office.Excel v11 et que sur mon poste, il s'agit de Excel 2003...
Par avance, MERCI
@+
ecrire dans fichier excel
bonjour
supprime ce cast inutile ,en interop.excel.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
'corrige cette ligne (cast inutile)
'xlSheet = CType(xlBook.Worksheets(1), xls.Worksheet)
'remplace par :
xlSheet =xlBook.Worksheets(1)
'initialise l'interface range avant d'entrer dans la boucle
Dim castRange As xls.Range =nothing
For j As Integer = 1 To 13
'pas besoin de caster un objet excel à autre objet excel
castRange = xlSheet.Cells(1, j)
castRange.Value = j
Next |
bon code............