【发布时间】:2009-05-22 16:39:46
【问题描述】:
如何将 DBgrid 上显示的数据导出为 pdf 文件?
【问题讨论】:
如何将 DBgrid 上显示的数据导出为 pdf 文件?
【问题讨论】:
嗯,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(包括 2009)的 Scalabium 导出套件支持多种导出格式,包括 PDF 和其他带有和不带有 OLE 的办公格式。导出组件可以与 TDBGrid 和 TDataSet 后代一起使用。
它可以用于非可视化,但也提供可配置的导出向导。我们在从 Delphi 7 到 2009 的应用程序套件迁移中成功使用了它。
【讨论】:
试试 EMS 高级数据导出 VCL
http://sqlmanager.net/en/products/tools/advancedexport
【讨论】:
您可以自己遍历数据并使用出色的导出 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
【讨论】: