在vb.net中从数据库表生成PDF

在vb.net中从数据库表生成PDF,pdf,itextsharp,vb.net-2010,Pdf,Itextsharp,Vb.net 2010,我想从数据库表中的数据制作一个pdf报告。在VB.net中,我所看到的一切就是如何使用asp。甚至可能。这是用于windows窗体的 好的,经过一段时间的研究和对我的代码的玩转,我想我会发布它,因为看起来大多数itextsharp的东西都是针对c#或ASP.net的。这对我来说很有效,我使用的是VS express 2010。其中显示dtg.reports,您将在这里用dataGridView的名称替换它。我希望这能帮助一些人,让它工作起来是件痛苦的事。我做了很多研究,通过我的发现和我的代码,我

我想从数据库表中的数据制作一个pdf报告。在VB.net中,我所看到的一切就是如何使用asp。甚至可能。这是用于windows窗体的


好的,经过一段时间的研究和对我的代码的玩转,我想我会发布它,因为看起来大多数itextsharp的东西都是针对c#或ASP.net的。这对我来说很有效,我使用的是VS express 2010。其中显示dtg.reports,您将在这里用dataGridView的名称替换它。我希望这能帮助一些人,让它工作起来是件痛苦的事。

我做了很多研究,通过我的发现和我的代码,我发现了一些有用的东西,所以我希望这能帮助一些人。这个解决方案是在VB.net中使用VS express 2012完成的,一直在为我工作注意:您将在datagridview的名称中插入dtgReport

Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf

'Creating iTextSharp Table from the DataTable data
Dim pdfTable As New PdfPTable(dtgReport.ColumnCount)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 30
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1

'Adding Header row
For Each column As DataGridViewColumn In dtgReport.Columns
    Dim cell As New PdfPCell(New Phrase(column.HeaderText))
    cell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
    pdfTable.AddCell(cell)
 Next

 'Adding DataRow
 For Each row As DataGridViewRow In dtgReport.Rows
     For Each cell As DataGridViewCell In row.Cells
         pdfTable.AddCell(cell.Value.ToString())
     Next
 Next

'Saving to PDF
Dim folderPath As String = "C:\reports\"
If Not Directory.Exists(folderPath) Then
    Directory.CreateDirectory(folderPath)
End If

Using stream As New FileStream(folderPath & "test2.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

嗨,克里斯,我看到你的问题得到了一张反对票和两张结束问题的票。我想这是在你更新你的问题之前发生的。我认为如果你从问题中删除解决方案并使用该代码创建答案会更好。然后你可以接受自己的答案,问题不需要关闭。我从哪里得到答案?它在我的2010 vb express中未被识别