Datagrid导出为pdf

时间:2009-05-22 16:39:47

标签: delphi pdf vcl

如何将DBgrid上显示的数据导出到pdf文件?

5 个答案:

答案 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

  • 数据(数据集)导出为17种最流行的格式:MS Access,MS Excel,MS Word,Open XML格式,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代码
  • 无需额外的库或软件操作
  • 详细的帮助系统和演示应用程序
  • 功能强大的组件和属性编辑器
  • 分别为每个字段设置用户格式
  • 多语言支持

答案 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