[VB.NET] Lire un Fichiers Excel
Salut à tous
je débute dans le monde de .net avec une application utilisant des fichier excel. j'ais un petit problème d'ouverture du fichier (j'ai utiliser "Office PIA" comme indiquer dans un tutorial) avec l'érreur suivante :
Code:
Exception from HRESULT: 0x800A03EC
voila mon code source :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
Imports System
Imports System.Data
Imports System.Web
Imports System.IO
Imports Microsoft.Office.Interop
Public Class Form1
Dim opfile As String
Private oExcelApp As Excel.ApplicationClass
Private oBooks As Excel.Workbooks
Private oBook As Excel.Workbook
Private oSheet As Excel.Worksheet
Private oRien As Object
Private nIndex As Integer
Private strTemplate As String
Private strExportFormat As String
Private strCache As String
Private Sub QuitterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitterToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles od.FileOk
End Sub
Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
od.InitialDirectory = "c:\"
od.Filter = "xls files (*.xls)|*.xls|cvs file (*.cvs)|*.cvs"
od.FilterIndex = 2
od.RestoreDirectory = True
If od.ShowDialog() = DialogResult.OK Then
opfile = od.FileName
Label1.Text() = opfile
oExcelApp = New Excel.ApplicationClass()
oExcelApp.Visible = False
oBooks = oExcelApp.Workbooks
oBook = oBooks.Open(opfile, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien, oRien)
' oSheet = oBook.Worksheets(0)
End If
End Sub
End Class |
aidez moi svp à résoudre ce problème si non avez vous une autre méthode pour la manipulation des fichier Excel (lecture , ecriture)
merci :)
lecture dans fichier excel fermé
Je travaille justement sur la lecture/ecriture dans un fichier excel fermé.
Tu veux lire dans un fichier excel ouvert ou fermé?
si c'est un fermé, en 1er mettre la référence "microsoft activex data object 2.8"
voici le code,
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
'on ouvre la connextion avec excel
dim fichier as string = "nom du fichier excel.xls"
dim valeur as string
'***************
'** Conextion **
'***************
Source = New ADODB.Connection
'suivant le type de fichier on choisit le provider
If UCase(Mid(fichier, Len(fichier) - 2, 3)) = "XLS" Then
Source.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & fichier & ";Extended Properties=""Excel 8.0;HDR=No;"";")
ElseIf UCase(Mid(fichier, Len(fichier) - 2, 3)) = "LSX" Then
Source.Open("Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & fichier & ";Extended Properties=""Excel 12.0;HDR=No;"";")
end If
If ADOCommand Is Nothing Then ADOCommand = New ADODB.Command
With ADOCommand
.ActiveConnection = Source
.CommandText = "SELECT * FROM [feuil1$A1:A1]" 'attention ne pas oublier le $
End With
'** recuperation de la valeur**
Rst = New ADODB.Recordset
Rst.CursorLocation = ADODB.CursorLocationEnum.adUseClient
Rst.Open(ADOCommand, , ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
Rst = Source.Execute("[feuil1$A1:A1]")
valeur = Rst(0).Value
'on affiche la valeur
console.writeline(valeur)
'*************************
'** fermeture de la connextion**
'*************************
Rst.Close()
Source.Close()
'et on libére tout
Source = Nothing
Rst = Nothing
ADOCommand = Nothing
fichier = Nothing |
j'espére que cela peut t'aider.