如何将 DBgrid 上显示的数据导出为 pdf 文件?
5 回答
好吧,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 文件。
问候
Delphi的Scalabium导出套件(包括 2009)支持多种导出格式,包括 PDF 和其他带或不带 OLE 的办公格式。导出组件可以与 TDBGrid 和 TDataSet 后代一起使用。
它可以用于非可视化,但也提供可配置的导出向导。我们在从 Delphi 7 到 2009 的应用程序套件迁移中成功使用了它。
试试 EMS 高级数据导出 VCL
http://sqlmanager.net/en/products/tools/advancedexport
- 数据(数据集)导出为 17 种最流行的格式:MS Access、MS Excel、MS Word、Open XML Format、Open Document Format (ODF)、RTF、HTML、XML、PDF、TXT、DBF、CSV、SYLK、DIF、LaTeX , SQL 和 Windows 剪贴板
- Borland Delphi 5-7、2005、2006、CodeGear Delphi 2007、2009 和 Borland C++ Builder 5-6、CodeGear C++ Builder 2007、2009 支持
- 导出 Unicode 数据。为导出的数据手动预设文本编码(UTF-8、UTF-16/UCS-2、UTF-32/UCS-4、Latin1、Latin2、Latin5、Latin7 等)
- 保存数据以供将来查看、修改、打印或网络发布
- 易于使用的向导允许您的最终用户快速导出数据
- 每种数据格式的强大导出选项
- 100% 原生 Delphi 代码
- 无需额外的库或软件即可运行
- 详细的帮助系统和演示应用
- 强大的组件和属性编辑器
- 分别设置每个字段的用户格式
- 多语言支持
您可以自己遍历数据并使用来自 Gnostice 的出色的导出 VCL eDocEngine。它还连接到报告工具和其他组件。
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