IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Choix de type de sortie d'impression


Sujet :

VB.NET

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2015
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 52
    Points : 54
    Points
    54
    Par défaut Choix de type de sortie d'impression
    Bonjour à tous,j'aimerais juste savoir comment faire pour imprimer un formulaire que j'ai crée en un fichier type excel.
    J'utilisais habituellement l'outil printform mais je sais pas si avec ce même outil,je peux imprimer le formulaire vers excel.Si oui,comment faire?Sinon, comment puis-je procéder?

  2. #2
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Points : 0
    Points
    0
    Par défaut
    Bonjour,

    Voici un exemple ce que vous pouvez faire :
    Nom : excel.jpg
Affichages : 410
Taille : 131,5 Ko

    voici mon codes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Public Class Form1
     
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim LIRE As String
            LIRE = Application.StartupPath & "\Exemple.xls"
            IO.File.WriteAllBytes(LIRE, My.Resources.Exemple)
            DataGridView1.Rows.Clear()
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim dataSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & LIRE & ";Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [BDD$]", MyConnection)
            dataSet = New System.Data.DataSet
            MyCommand.Fill(dataSet)
            DataGridView1.DataSource = dataSet.Tables(0)
            DataGridView1.Columns(0).Width = 100
            DataGridView1.Columns(1).Width = 100
            DataGridView1.Columns(2).Width = 100
            DataGridView1.Columns(3).Width = 100
            DataGridView1.Columns(4).Width = 100
            DataGridView1.Columns(5).Width = 100
            DataGridView1.Columns(6).Width = 100
            DataGridView1.Columns(7).Width = 100
            DataGridView1.Columns(8).Width = 100
        End Sub
     
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            PrintDocument1.DefaultPageSettings.Landscape = True
            Dim margins As New Printing.Margins(50, 100, 100, 100)
            PrintDocument1.DefaultPageSettings.Margins = margins
            PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.Size = New System.Drawing.Size(1000, 200)
            AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
            PrintPreviewDialog1.ShowDialog()
        End Sub
     
        Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
             Dim dataGridViewImage As Bitmap = New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height)
            DataGridView1.DrawToBitmap(dataGridViewImage, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
            e.Graphics.DrawImage(dataGridViewImage, 0, 0)
        End Sub
     
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            PrintDocument1.DefaultPageSettings.Landscape = True
            PrintDocument1.Print()
        End Sub
    End Class
    cordialement,

  3. #3
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2015
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 52
    Points : 54
    Points
    54
    Par défaut
    Déjà merci pour l'aide que tu m'apportes.
    Je rencontre un problème quant à la lecture du fichier,le message d'erreur suivant s'affichent: "Cannot clear this list."
    Et en ce qui concerne l'impression à proprement parlé,ça passe,mais c'est comme ce que je faisais avant.Il est imprimer au format pdf.
    En fait,ce que je souhaite (au cas où je n'ai pas été bien explicite), c'est que la partie à imprimer (mon formulaire dans mon cas) soit imprimé au format excel.
    Merci d'avance pour ta réponse.

  4. #4
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Points : 0
    Points
    0
    Par défaut
    Bonjour,

    Je rencontre un problème quant à la lecture du fichier,le message d'erreur suivant s'affichent: "Cannot clear this list."

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Dim LIRE As String = "'Tu ajoute le répertoire du fichier XLS => ( c:/exemple.xls)"
    DataGridView1.Rows.Clear()
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim dataSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & LIRE & ";Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [BDD$]", MyConnection)
            dataSet = New System.Data.DataSet
            MyCommand.Fill(dataSet)
            DataGridView1.DataSource = dataSet.Tables(0)
            DataGridView1.Columns(0).Width = 100 '
            DataGridView1.Columns(1).Width = 100 '
            DataGridView1.Columns(2).Width = 100 '
            DataGridView1.Columns(3).Width = 100 '=> nombre de colonne dans ton fichier XLS
            DataGridView1.Columns(4).Width = 100 '
            DataGridView1.Columns(5).Width = 100 '
            DataGridView1.Columns(6).Width = 100 '
            DataGridView1.Columns(7).Width = 100 '
            DataGridView1.Columns(8).Width = 100
    j'ai pas bien compris ce que vous voulez dire? Es ce que vous voulez dire que vous avez un fichier PDF est l'imprimer en format Excel?

  5. #5
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2015
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 52
    Points : 54
    Points
    54
    Par défaut Imprimer vers excel
    Bonsoir!
    Non,je reconnais ne pas avoir été assez clair au début,et je m'en excuse.
    En fait, je développe un programme et j'utilise mysql workbrench comme base de donnée.
    Avec un tablelayoutpanel, j'ai crée un tableau dans lequel les informations entrées (à l'aide de textbox) sont insérées dans une des tables de ma base de donnée (soit par exemple la table x).
    J'ai utilisé ensuite un datagridview pour afficher les informations de la table x sur un windows form, et quand je clique sur une donnée,toutes les informations sont affichées dans des textbox prévus à cet effet. Puis, avec un bouton,j'arrive à imprimer ces informations dans un fichier pdf créée au moment de l'impression.Voici le code que j'utilise pour ce faire:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    56
    57
    58
    59
    60
     
    Dim myConnexion As MySqlConnection
        Dim myCommand As MySqlCommand
        Dim dbDataset As New DataTable
     
    Private Sub Etat_client_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     
    myConnexion = New MySqlConnection
            myConnexion.ConnectionString = "server = localhost; userid = root; password = abc; database = ok"
            Dim sda As New MySqlDataAdapter
            Dim bsource As New BindingSource
     
            Try
                myConnexion.Open()
                Dim query As String
                query = "select date,denomination,numero_facture,cc from ok.a"
                myCommand = New MySqlCommand(query, myConnexion)
                sda.SelectCommand = myCommand
                sda.Fill(dbDataset)
                bsource.DataSource = dbDataset
                DataGridView1.DataSource = bsource
                sda.Update(dbDataset)
     
                myConnexion.Close()
     
            Catch ex As MySqlException
                MessageBox.Show(ex.Message)
            Finally
                myConnexion.Dispose()
            End Try
     
    End Sub
     
    Private Sub BT_IMPRIMER_Click(sender As Object, e As EventArgs) Handles BT_IMPRIMER.Click
     
     
     
    With PrintForm1.PrinterSettings.DefaultPageSettings.Margins
                        .Top = 10
                        .Left = 10
                        .Bottom = 100
                        .Right = 10
                    End With
                    PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
                    PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
     
     
    End Sub
     
    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If e.RowIndex >= 0 Then
                Dim Row As DataGridViewRow
                Row = Me.DataGridView1.Rows(e.RowIndex)
     
                MTDATE.Text = Row.Cells("date").Value.ToString
                TXT_DENOMINATION.Text = Row.Cells("denomination").Value.ToString
                TXTFACTURE.Text = Row.Cells("numero_facture").Value.ToString
                TXTNUMEROCC.Text = Row.Cells("cc").Value.ToString
    End If
        End Sub
    Ce code marche très bien,et les informations sont imprimer dans une feuille au format pdf. Mais je veux à présent imprimer les informations dans un fichier excel qui sera créée au moment de l'impression.

  6. #6
    Inactif  

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2012
    Messages
    4 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2012
    Messages : 4 904
    Points : 10 168
    Points
    10 168
    Billets dans le blog
    36
    Par défaut
    Bonjour,

    Trouvé avec le moteur de recherche du site:

    http://www.developpez.net/forums/d76...ew-vers-excel/

    Il y a aussi quelques liens à partir de cette page:

    http://www.codeproject.com/search.as...sbo=kw&x=8&y=9
    À ma connaissance, le seul personnage qui a été diagnostiqué comme étant allergique au mot effort. c'est Gaston Lagaffe.

    Ô Saint Excel, Grand Dieu de l'Inutile.

    Excel n'a jamais été, n'est pas et ne sera jamais un SGBD, c'est pour cela que Excel s'appelle Excel et ne s'appelle pas Access junior.

  7. #7
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2015
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 52
    Points : 54
    Points
    54
    Par défaut
    Merci bien,je passe à l'essai.

  8. #8
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2015
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 52
    Points : 54
    Points
    54
    Par défaut
    J'ai essayé mais j'ai comme l'impression que le fait que j'utilise mysql workbrench comme base de donnée pose problème.
    Qu'en pensez-vous?
    Je suis tombé sur un tutoriel qui semble ne pas utiliser access.
    Voici le code que j'obtiens en suivant le tutoriel:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
     
    Imports MySql.Data.MySqlClient
    Imports Microsoft.Office.Core
    Imports Microsoft.Office.Interop
     
    Public Class Form1
     
        'Déclarations de variables
     
        Dim myConnexion As MySqlConnection
        Dim myCommand As MySqlCommand
        Dim dbDataset As DataTable
        Dim excelLocation As String = "C:\cool.xlsx"
        Dim ds As DataSet
        Dim tables As DataTableCollection
        Dim bsource As New BindingSource
        Dim APP As New Excel.Application
        Dim worksheet As Excel.Worksheet
        Dim workbook As Excel.Workbook
     
        Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
            workbook.Save()
            workbook.Close()
            APP.Quit()
        End Sub
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     
            myConnexion = New MySqlConnection
            myConnexion.ConnectionString = "server = localhost; userid = root; password = abc; database = sup"
            Dim sda As New MySqlDataAdapter
            Dim bsource As New BindingSource
            Dim Dts As New DataSet
     
            Try
                myConnexion.Open()
                Dim query As String
                query = "select date,denomination,numero_facture,mt_total,acompte,net from sup.test"
                myCommand = New MySqlCommand(query, myConnexion)
                sda.SelectCommand = myCommand
                Dim dbDataset As New DataTable
                sda.Fill(dbDataset)
                bsource.DataSource = dbDataset
                DataGridView1.DataSource = bsource
                sda.Update(dbDataset)
     
                myConnexion.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                myConnexion.Dispose()
            End Try
     
        End Sub
     
     
     
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim xlapp As Microsoft.Office.Interop.Excel.Application
            Dim xlworkbook As Microsoft.Office.Interop.Excel.Workbook
            Dim xlworksheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim misvalue As Object = System.Reflection.Missing.Value
            Dim i As Integer
            Dim j As Integer
     
            xlapp = New Microsoft.Office.Interop.Excel.Application
            xlworkbook = xlapp.Workbooks.Add(misvalue)
            xlworksheet = xlworkbook.Sheets("sheet1")
     
            For i = 0 To DataGridView1.RowCount - 1
                For j = 0 To DataGridView1.ColumnCount - 1
                    For k As Integer = 1 To DataGridView1.Columns.Count
                        xlworksheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                        xlworksheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
     
                    Next
                Next
            Next
     
            xlworksheet.SaveAs("C:\cool.xlsx")
            xlworkbook.Close()
            xlapp.Quit()
     
            releaseobject(xlapp)
            releaseobject(xlworkbook)
            releaseobject(xlworksheet)
     
            Dim res As MsgBoxResult
            res = MsgBox("ok", MsgBoxStyle.YesNo)
            If (res = MsgBoxResult.Yes) Then
                Process.Start("c:\cool.xlsx")
            End If
     
        End Sub
     
        Private Sub releaseobject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
     
    End Class
    Bon,le problème, c'est que quand je clique sur le bouton pour imprimer, j'ai le message d'erreur suivant qui s'affiche au niveau de :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    xlworksheet = xlworkbook.Sheets("sheet1")
    Voici le message d'erreur :
    "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in testdatagridview.exe"
    "Additional information: Index non valide. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"

    Qu'est-ce que cela veut dire? je n'arrive pas à corriger l'erreur.

  9. #9
    Inactif  

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2012
    Messages
    4 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2012
    Messages : 4 904
    Points : 10 168
    Points
    10 168
    Billets dans le blog
    36
    Par défaut
    Bonjour,

    Est-ce que le nom de ta feuille Excel est "vraiment" sheet1 ?

    Remplace sheet1 par le nom de ta feuille ou appelle ta feuille sheet1.
    À ma connaissance, le seul personnage qui a été diagnostiqué comme étant allergique au mot effort. c'est Gaston Lagaffe.

    Ô Saint Excel, Grand Dieu de l'Inutile.

    Excel n'a jamais été, n'est pas et ne sera jamais un SGBD, c'est pour cela que Excel s'appelle Excel et ne s'appelle pas Access junior.

  10. #10
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2015
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2015
    Messages : 52
    Points : 54
    Points
    54
    Par défaut
    Merci pour la remarque.En fait, il s'agissait du nom de la première feuille du fichier excel.Et c'était
    Feuil1
    Bon,après cela, un autre problème s'est afficher quant à la création du fichier même en intégrant les données.Mais bon,toujours à la recherche de solution,suis tombé sur une page parlant de l'outil
    report viewer
    J'ai utilisé cet outil, et j'ai réussi à régler tous les problèmes.
    Mais encore merci à vous
    (hacker59 et clementmarcotte)
    pour les efforts que vous avez fournit en me proposant des solutions.Au moins,sur ce forum,on sent la solidarité des développeurs.Et bonne journée à vous.


+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Choix du type de champs
    Par hbellahc dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 03/09/2006, 21h40
  2. Mysql : choix des types pour les champs entre :
    Par Thierry8 dans le forum Administration
    Réponses: 3
    Dernier message: 14/06/2006, 08h22
  3. [architecture] Choix du type de pile
    Par Neitsa dans le forum Algorithmes et structures de données
    Réponses: 3
    Dernier message: 29/11/2005, 10h04
  4. Problème de choix de page lors de l'impression
    Par Olaf MENJI dans le forum Windows
    Réponses: 2
    Dernier message: 22/11/2005, 10h51
  5. choix des types
    Par cali dans le forum Langage SQL
    Réponses: 3
    Dernier message: 10/08/2004, 13h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo