如何将DBgrid上显示的数据导出到pdf文件?
答案 0 :(得分:7)
嗯,DBGrid中显示的数据是由附加到该dbgrid的数据集提供的,因此将DBGrid中的数据导出为PDF意味着将数据集中的数据导出为PDF。
最简单的选择是使用报告工具。 Delphi有许多不同的报告工具,例如Rave Report,FastReport,Report Builder,QuickReport等。
此类工具可让您根据数据设计打印报告,并允许您打印报告或将其导出为HTML,DOC,PDF等格式。 Rave Report随Delphi一起提供,您可以免费使用它。我个人喜欢FastReport并在我的应用程序中使用它。
另一个选择是,如果您在目标系统上安装了虚拟PDF打印机,您可以选择它作为您的打印机并使用Delphi的TPrinter类直接在打印机画布上书写,并且您的虚拟PDF打印机将生成PDF文件为您而不是在纸上打印您的数据。
第三种选择是使用专为PDF操作而构建的第三方组件,并允许您在应用程序中创建或编辑PDF文件。
此致
答案 1 :(得分:3)
Delphi的Scalabium导出套件(包括2009)支持许多导出格式,包括PDF和其他带有和不带OLE的办公格式。导出组件可以与TDBGrid和TDataSet后代一起使用。
它可以非视觉使用,但也提供可配置的导出向导。我们成功地将它用于从Delphi 7到2009的应用程序套件迁移。
答案 2 :(得分:2)
尝试EMS高级数据导出VCL
http://sqlmanager.net/en/products/tools/advancedexport
答案 3 :(得分:1)
您可以自己迭代数据并使用出色的导出VCL eDocEngine 来自Gnostice。它还连接到报告工具和其他组件。
答案 4 :(得分:0)
Public Sub ExportDataTableToPDF(ByVal dtImport As DataTable)
Dim strQuery As String = "select er_num,er_shortd,er_longd,er_severity from mv_error"
Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Try
'====================================================================
'Dim str As String = DateTime.Now.ToString("yyyyMMddHHmmssff")
'Dim sWebSettingPath As String = ConfigurationSettings.AppSettings("ExptLoctPath")
'Dim str1 As String = "E:\ExportPdf_File"
'Dim WorkingFile As String
'Dim s As String
'If (Directory.Exists(sWebSettingPath)) Then
' s = "already exists"
' WorkingFile = Path.Combine(sWebSettingPath, "" & str & " DataTestData.pdf")
'Else
' Dim ss As String = "C:\ReportPDF"
' WorkingFile = Path.Combine(str1, "" & str & " DataTestData.pdf")
' Directory.CreateDirectory(ss)
'End If
'====================================================================
'Folder Exists in Particular folder or not cheak ifnot create a folder
Dim strDateTime As String = DateTime.Now.ToString("yyyyMMddHHmmssff")
Dim sWebSettingPath As String = ConfigurationSettings.AppSettings("ExptLoctPath")
Dim str1 As String = "E:\ExportPdf_File"
Dim WorkingFile As String
Dim s As String
If (Directory.Exists(sWebSettingPath)) Then
s = "already exists"
WorkingFile = Path.Combine(sWebSettingPath, "" & strDateTime & "_DataTestData.pdf")
Else
Dim sWebStingNotPath As String = "C:\ReportPDFTest"
Directory.CreateDirectory(sWebStingNotPath)
WorkingFile = Path.Combine(sWebStingNotPath, "" & strDateTime & " DataTestData.pdf")
End If
'====================================================================
Dim fs As New FileStream(WorkingFile, FileMode.Create, FileAccess.Write, FileShare.None)
'Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter.GetInstance(doc, fs)
' Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("C:\Documents and Settings\lessly.l\Desktop\iTextSharp\Test11.pdf", FileMode.Create))
doc.Open()
'Open Document to write
Dim font8 As Font = FontFactory.GetFont("ARIAL", 7)
'Write some content
Dim paragraph As New Paragraph("Team :: CataPult")
Dim dt As DataTable = dtImport
If dt IsNot Nothing Then
'Craete instance of the pdf table and set the number of column in that table
Dim PdfTable As New PdfPTable(dt.Columns.Count)
Dim PdfPCell As PdfPCell = Nothing
Dim pdfrow As PdfPRow = Nothing
For column As Integer = 0 To dt.Columns.Count - 1
PdfTable.HeaderRows = dt.Columns.Count
PdfPCell = New PdfPCell(New Phrase(New Chunk(dt.Columns(column).Caption.ToString().ToUpper, New Font(Font.HELVETICA, 8.0F, Font.BOLD, Color.WHITE))))
PdfPCell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#66CCFF"))
PdfTable.AddCell(PdfPCell)
Next
'Each Row Values added
For rows As Integer = 0 To dt.Rows.Count - 1
For column As Integer = 0 To dt.Columns.Count - 1
PdfTable.HeaderRows = dt.Columns.Count
PdfPCell = New PdfPCell(New Phrase(New Chunk(dt.Rows(rows)(column).ToString(), font8)))
PdfTable.AddCell(PdfPCell)
Next
Next
PdfTable.SpacingBefore = 15.0F
' Give some space after the text or it may overlap the table
doc.Add(paragraph)
' add paragraph to the document
' add pdf table to the document
doc.Add(PdfTable)
End If
Catch docEx As DocumentException
'handle pdf document exception if any
Catch ioEx As IOException
' handle IO exception
Catch ex As Exception
' ahndle other exception if occurs
Finally
'Close document and writer
doc.Close()
End Try
End Sub