Inserer une image dans un ficher .xls par Vb.net 2008

Résolu
alienware62 Messages postés 20 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 23 mai 2010 - 4 févr. 2010 à 12:26
alienware62 Messages postés 20 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 23 mai 2010 - 8 févr. 2010 à 14:49
Bonjour à vous, j'aurais besoin de vos lumières

Je récupère des information via un programme vb.net 2008 dans une base de donnée et aussi la signature en .gif, jusque la tout va bien

Mais maintenant je dois remplir une feuille excel avec les infos de la base de donnée (voici le code, si ça peut aider quelqu'un)
 Dim cnxexcel As New ADODB.Connection
        Dim commandexcel As New ADODB.Command
        Dim rst As New ADODB.Recordset

        cnxexcel.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Classeur1.xls;Extended Properties=""Excel 8.0;HDR=No;"";"
        cnxexcel.Open()
        commandexcel.ActiveConnection = cnxexcel
        commandexcel.CommandText = "SELECT * FROM [Feuil1$A1:A1]"
        rst = New ADODB.Recordset
        rst.Open(commandexcel, , ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        rst(0).Value ="toto"
        rst.Update()

        cnxexcel.Close()
        commandexcel = Nothing
        cnxexcel = Nothing
        rst = Nothing


donc avec ceci je remplis les cellules une par une et ça marche nickel

Mais pour la signature au format .gif je n'ai aucune idée comment faire pour la placé dans ce ficher xls ?

pour pouvoir l'imprimer totalement rempli
(voici le code pour imprimer,si ça peut aider quelqu'un)
 Dim pp As New Process()
        pp.StartInfo.FileName = "c:\classeur1.XLS"
        pp.StartInfo.Verb = "print"
        pp.StartInfo.CreateNoWindow = True
        pp.StartInfo.CreateNoWindow = False
        pp.Start()


Cordialement Alienware62

3 réponses

alienware62 Messages postés 20 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 23 mai 2010
8 févr. 2010 à 14:49
merci moi même ,voici la solution
   Dim sFileImage, sFilePath As String
        Dim objapp As New Microsoft.Office.Interop.Excel.ApplicationClass
        Dim objsheet As New Microsoft.Office.Interop.Excel.Worksheet
        Dim objworkbooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing

        Dim objworkbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
        objworkbooks = objapp.Workbooks
        objworkbook = objworkbooks.Open("c:\classeur1.xls")
        sFileImage = "C:\test.Gif"
        sFilePath = "c:\classeur1.xls"
        Try

            objsheet = objworkbook.ActiveSheet
            objsheet = objworkbook.Sheets(1)
            objsheet.Name = "feuil1"

            objsheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 100, 140, 70)
            objworkbook.SaveAs(sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
            objapp.Quit()
            objworkbook = Nothing
            objapp = Nothing
        Catch ex As Exception

        End Try
3
alienware62 Messages postés 20 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 23 mai 2010
5 févr. 2010 à 10:52
j'ai trouvé une autre méthode pour écrire dans le ficher excel en ajoutant au projet les Microsoft.Office.Interop.Excel et le dll office

Dim toto As New Microsoft.Office.Interop.Excel.Application
        Dim tata As New Microsoft.Office.Interop.Excel.Worksheet

        toto.Workbooks.Open("c:\classeur1.xls")
        toto.Cells("2", "b") = "test"
        tata.Name = "feuil1"
        tata.Shapes.AddPicture("C:\TEST.Gif", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 100, 160, 96)
        toto.Workbooks.Close()

mais il me dit l'erreur suivante que je ne comprends pas trop :
Impossible d'effectuer un cast d'un objet COM de type 'Microsoft.Office.Interop.Excel.WorksheetClass' en type d'interface 'Microsoft.Office.Interop.Excel._Worksheet'. Cette opération a échoué, car l'appel QueryInterface sur le composant COM pour l'interface avec l'IID '{000208D8-0000-0000-C000-000000000046}' a échoué en raison de l'erreur suivante : Cette interface n'est pas prise en charge (Exception de HRESULT : 0x80004002 (E_NOINTERFACE)).
quelqu'un pourrait il m'expliquer comment résoudre cette erreur ?
qui se produit à la ligne
tata.Shapes.AddPicture("C:\TEST.Gif", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 100, 160, 96)
0
alienware62 Messages postés 20 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 23 mai 2010
8 févr. 2010 à 11:38
bon j'avance encore, j'ai trouvé le code suivant et l'image se rajoute bien mais dans un nouveau fichier totalement vierge, je ne voix pas comment le faire dans un déjà existant
        Dim sFileImage, sFilePath As String
        Dim objapp As New Microsoft.Office.Interop.Excel.ApplicationClass
        Dim objsheet As New Microsoft.Office.Interop.Excel.Worksheet
        Dim objworkbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
        sFileImage = "C:\test.Gif"
        sFilePath = "c:\classeur1.xls"
        Try

            objworkbook = objapp.Workbooks.Add(Type.Missing)
            objsheet = objworkbook.ActiveSheet
            objsheet = objworkbook.Sheets(1)
            objsheet.Name = "feuil1"

            objsheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350)
            objworkbook.SaveAs(sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
            objapp.Quit()
            objworkbook = Nothing
            objapp = Nothing
        Catch ex As Exception

        End Try
0
Rejoignez-nous