将数据导出或打印为PDF [英] Exporting or printing data to PDF
问题描述
请帮助,
我正在努力获取代码将表单(vb.net)打印到pdf或打印机。我想使用  ;
I am struggling to get a code to print data from a form(vb.net) to pdf or to printer.I want to use
下面是一个过滤或填充表格的代码,我希望能够将此日期打印为PDF
Below is a code which filters or popule form, i want to be able to print this date to PDF
Private Sub BtnFilter_Click(sender As Object, e As EventArgs) Handles BtnFilter.Click
Try
Call ConnectionResults()
cm = New OleDb.OleDbCommand
With cm
.Connection = cnR
.CommandType = CommandType.Text
.CommandText = "Select serialnumber,(select count(*)from Passes where WorksOrder='" & TxtWorksOrderFilter.Text & "') from Passes"
dr = .ExecuteReader
End With
' strSQL = "Select serialnumber,(select count(*)from Passes where WorksOrder='" & TxtWorksOrderFilter.Text & "') from Passes"
PassesBindingSource.Filter = "WorksOrder='" & TxtWorksOrderFilter.Text & "'"
DataGridView1.Refresh()
'Debug.Print("Me.ResultsDataSet.Passes.Count")
'dr = cm.ExecuteReader()
While (dr.Read())
'MessageBox.Show(dr(0).ToString())
' MessageBox.Show(dr(1).ToString())
TxtQTY.Text = dr(1).ToString()
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'
End Sub
您的指导将受到赞赏
Your guidance will be appreciated
推荐答案
您好SIsyb,
Hi SIsyb,
根据您的描述,您希望将从数据库过滤的数据导出为PDF。现在您已将数据从数据库过滤到DataGridView,您可以参考下面的代码将这些数据导出为PDF。
According to your description, you want to export data that you filter from database to PDF. now you have filtered data from database into DataGridView, you can refer to the code below to export these data to PDF.
首先 将itextsharp.dll添加到应用程序中。
First add the itextsharp.dll into application.
'Creating iTextSharp Table from the DataTable data
Dim pdfTable As New PdfPTable(DataGridView1.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 DataGridView1.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 DataGridView1.Rows
For Each cell As DataGridViewCell In row.Cells
pdfTable.AddCell(cell.Value.ToString())
Next
Next
'Exporting to PDF
Dim folderPath As String = "D:\test\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & "DataGridViewExport.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
最诚挚的问候,
Cherry
这篇关于将数据导出或打印为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!