none
excel to pdf RRS feed

  • Question

  • bonjour

    je voudrais exporter chaque ligne de mon fichier excel en PDF

    sheet1

    nom entreprise A B C
    a aa aaa aaaa aaaaa
    b bb bbb bbbb bbbbb
    c cc ccc cccc ccccc
    d dd ddd dddd dddd

    il me faudrait 4 fichier PDF (a.pdf;b.pdf; etc) avec pour le fichier a.pdf et ainsi de suite

    Nom : a
    entreprise: aa
    A: aaa
    B: aaaa
    C: aaaaa

    Merci d'avance

    jeudi 25 mai 2017 10:21

Réponses

  • Bonsoir,

    Voici un exemple :

    Le code :

    Imports System.IO
    Imports System.Data
    Imports System.Reflection
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                Dim MyConnection As OleDb.OleDbConnection
                Dim Ds As System.Data.DataSet
                Dim MyAdapter As System.Data.OleDb.OleDbDataAdapter
                MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\nathalie\Desktop\teste.xlsx';Extended Properties='Excel 12.0 Xml;HDR=YES;'")
                MyAdapter = New System.Data.OleDb.OleDbDataAdapter("Select * from [Feuil1$]", MyConnection)
                Ds = New System.Data.DataSet
                MyAdapter.Fill(Ds)
                Me.DataGridView1.DataSource = Ds.Tables(0)
                MyConnection.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
            pdfTable.DefaultCell.Padding = 3
            pdfTable.WidthPercentage = 30
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
            pdfTable.DefaultCell.BorderWidth = 1
            For Each column As DataGridViewColumn In DataGridView1.Columns
                Dim cell As New PdfPCell(New Phrase(column.HeaderText))
                cell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
                pdfTable.AddCell(cell)
            Next
            For Each row As DataGridViewRow In DataGridView1.Rows
                For Each cell As DataGridViewCell In row.Cells
                    If cell.Value = vbNullString Then
                    Else
                        pdfTable.AddCell(cell.Value.ToString())
                    End If
                Next
            Next
            Dim folderPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\PDF\"
            If Not Directory.Exists(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If
            Using stream As New FileStream(folderPath & "MyPDF.pdf", FileMode.Create)
                Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
                PdfWriter.GetInstance(pdfDoc, stream)
                pdfDoc.Open()
                pdfDoc.Add(pdfTable)
                pdfDoc.Close()
                stream.Close()
            End Using
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Dim paragraph As New Paragraph
            Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
            Dim folderPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\PDF\"
            If Not Directory.Exists(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If
            Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(folderPath & "a.pdf", FileMode.Create))
            doc.Open()
            Dim PdfTable As New PdfPTable(5)
            PdfTable.TotalWidth = 490.0F
            PdfTable.LockedWidth = True
            Dim widths As Single() = New Single() {1.0F, 4.0F, 1.0F, 1.0F, 1.0F}
            PdfTable.SetWidths(widths)
            PdfTable.HorizontalAlignment = 1
            PdfTable.SpacingBefore = 2.0F
            Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
            Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
            Dim PdfPCell As PdfPCell = Nothing
            PdfPCell = New PdfPCell(New Phrase(New Chunk("Nom", font12Bold)))
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            PdfTable.AddCell(PdfPCell)
            PdfPCell = New PdfPCell(New Phrase(New Chunk("Entrepise", font12Bold)))
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            PdfTable.AddCell(PdfPCell)
            PdfPCell = New PdfPCell(New Phrase(New Chunk("A", font12Bold)))
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            PdfTable.AddCell(PdfPCell)
            PdfPCell = New PdfPCell(New Phrase(New Chunk("B", font12Bold)))
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            PdfTable.AddCell(PdfPCell)
            PdfPCell = New PdfPCell(New Phrase(New Chunk("C", font12Bold)))
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            PdfTable.AddCell(PdfPCell)
            PdfTable.HeaderRows = 0
            For Each row As DataGridViewRow In DataGridView1.SelectedRows
                For Each cell As DataGridViewCell In row.Cells
                    If cell.Value = vbNullString Then
                    Else
                        PdfTable.AddCell(cell.Value.ToString())
                    End If
                Next
            Next
            doc.Add(PdfTable)
            doc.Close()
        End Sub

    Il faudra import un fichier itextsharp.dll : itextsharp-5.0.6-dll

    J'espère que j'ai bien répondu à votre question.

    Si c'est le cas, n'hésitez pas à mettre ma réponse en réponse à votre post

    Cordialement,

    jeudi 25 mai 2017 18:37