julia Namor
Messages postés524Date d'inscriptionjeudi 27 mars 2014StatutMembreDernière intervention13 janvier 2024
-
17 déc. 2018 à 12:30
julia Namor
Messages postés524Date d'inscriptionjeudi 27 mars 2014StatutMembreDernière intervention13 janvier 2024
-
19 déc. 2018 à 12:10
Bonjour,
Avec ce code ci-dessous j'arrive à exporter mon datagridview en PDF .
Je souhaiterais masquer quelques colonnes dans le fichier PDF notamment la colonne ID.
Qui pourrais venir à mon aide ?
merci d'avance
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If dgvData.ColumnCount = 0 Then
MsgBox("Veuillez choisir une entité et lancer une recherche svp")
Exit Sub
End If
'Récupération des données du Dgv dans une table.
Dim dt As DataTable = New DataTable
For Each col As DataGridViewColumn In dgvData.Columns
dt.Columns.Add(col.HeaderText)
Next
For Each row As DataGridViewRow In dgvData.Rows
Dim dRow As DataRow = dt.NewRow
For Each cell As DataGridViewCell In row.Cells
dRow(cell.ColumnIndex) = cell.Value
Next
dt.Rows.Add(dRow)
Next
'Agencement des colonnes pour le visuel voulu.
' dt.Columns(8).SetOrdinal(1)
'Récupération des données organisées pour le PDF.
Dim pdfTable As New PdfPTable(dt.Columns.Count)
pdfTable.DefaultCell.Padding = 3
pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
pdfTable.DefaultCell.NoWrap = True
'Formatage des données.
For Each column As DataColumn In dt.Columns
Dim cell As New PdfPCell(New Phrase(column.Caption))
'MsgBox(column.Ordinal)
cell.BackgroundColor = New iTextSharp.text.BaseColor(196, 228, 229)
pdfTable.AddCell(cell)
Next
Dim j As Integer = 0
For Each row As DataRow In dt.Rows
Dim cellPDF As New PdfPCell
If dgvData.Rows(j).DefaultCellStyle.BackColor = Color.LightGray Then
cellPDF.BackgroundColor = New iTextSharp.text.BaseColor(234, 246, 208)
End If
If dgvData.Rows(j).DefaultCellStyle.BackColor = Color.LightCyan Then
cellPDF.BackgroundColor = New iTextSharp.text.BaseColor(255, 251, 207)
End If
j = j + 1
For i As Integer = 0 To dt.Columns.Count - 1
Dim cell As String = row(i).ToString
Select Case i
Case 5
Dim dat As Date
If Date.TryParse(cell, dat) Then
cell = dat.ToShortDateString
End If
cellPDF.Phrase = New Phrase(cell)
cellPDF.Column.Alignment = Element.ALIGN_RIGHT
cellPDF.PaddingRight = 10
Case 4, 8
Dim dbl As Double
If Double.TryParse(cell, dbl) Then
cell = dbl.ToString("0")
End If
cellPDF.Phrase = New Phrase(cell)
cellPDF.Column.Alignment = Element.ALIGN_RIGHT
cellPDF.PaddingRight = 10
Case Else
cellPDF.Phrase = New Phrase(cell)
End Select
pdfTable.AddCell(cellPDF)
Next
Next
pdfTable.TotalWidth = 1100.0F
pdfTable.LockedWidth = True
pdfTable.HorizontalAlignment = 0
pdfTable.SpacingBefore = 5.0F
pdfTable.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
''''' pdfTable.SetWidths(New Integer() {6, 7, 25, 6, 8, 6, 25, 6, 10})
'''''' insert title into pdf file
Dim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
Dim Paragraph As New Paragraph ' declaration for new paragraph
Dim Paragraph2 As New Paragraph ' declaration for new paragraph
Paragraph = New Paragraph(New Chunk("Fichier pdf crée", pTitle))
Paragraph2 = New Paragraph(New Chunk("Mise à jour du: " & Now, pTitle))
Paragraph.Alignment = Element.ALIGN_LEFT
Paragraph.SpacingAfter = 5.0F
Paragraph2.Alignment = Element.ALIGN_RIGHT
Paragraph2.SpacingAfter = 5.0F
' set and add page with current settings
Dim folderPath As String = "C:\PDF TEST"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & "\test.pdf", FileMode.Create)
'Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
Dim PdfDoc As New Document(PageSize.A2, 40, 40, 40, 50) ' set pdf page size
PdfWriter.GetInstance(pdfDoc, stream)
PdfDoc.Open()
PdfDoc.Add(Paragraph) ' set our pdf title
PdfDoc.Add(Paragraph2) ' set our pdf title
PdfDoc.Add(pdfTable)
PdfDoc.Close()
stream.Close()
End Using
''' AxAcroPDF1.src = "C:\Users\degecom\Desktop\PDF TEST\test.pdf"
MsgBox("Fichier pdf crée")
End Sub
A voir également:
Export datagridview to pdf c#
How to export datagridview to excel in vb net 2010 - Meilleures réponses