【问题标题】:put data in table using iText [duplicate]使用iText将数据放入表中[重复]
【发布时间】:2017-09-18 07:06:17
【问题描述】:

首先,我将数据放入虚拟网格视图中。
数据是在数据库上运行存储过程的结果。
然后我尝试将此数据添加到表中并尝试将数据导出为pdf。

这是我的代码:

 Dim GridView1 As New GridView
    Dim pdfDoc As New iTextSharp.text.Document(iTextSharp.text.PageSize.A1, 10.0F, 10.0F, 10.0F, 0.0F)
    pdfDoc.Open()
    Dim pdfTable As New PdfPTable(3)
    For Each row As GridViewRow In GridView1.Rows
        Dim Val1 As String = row.Cells(0).Text
        Dim Val2 As String = row.Cells(1).Text
        Dim Val3 As String = row.Cells(2).Text
    Next
    pdfDoc.Add(pdfTable)
    pdfDoc.Close()

现在如何将这些值(val1、val2、val3)添加到表中,然后导出为 pdf?

有什么帮助吗?

【问题讨论】:

    标签: gridview itext export-to-pdf


    【解决方案1】:

    这是错误的:

    Dim pdfTable As New PdfPTable(3)
    For Each row As GridViewRow In GridView1.Rows
        Dim Val1 As String = row.Cells(0).Text
        Dim Val2 As String = row.Cells(1).Text
        Dim Val3 As String = row.Cells(2).Text
    Next
    

    这是错误的,因为您只是定义了三个 string 值,而没有对这些值做任何事情。为什么那会奏效?您的代码中什么也没有发生。

    您需要将这些string 值添加到您刚刚定义的table

    Dim pdfTable As New PdfPTable(3)
    For Each row As GridViewRow In GridView1.Rows
        pdfTable.Add(row.Cells(0).Text)
        pdfTable.Add(row.Cells(1).Text)
        pdfTable.Add(row.Cells(2).Text)
    Next
    

    或者更确切地说,正如这个问题的答案中所解释的那样:dataGridView to pdf with itextsharp

    PdfPTable pdfTable= new PdfPTable(3);
    foreach(DataGridViewRow row in dataGridView1.Rows) {
        foreach (DataGridViewCell celli in row.Cells) {
            pdfTable.AddCell(celli.Value.ToString());
        }
    }
    doc.Add(pdfTable);
    

    您需要应用一些小的更改来将此 C# sn-p 移植到 VBA 代码,但这应该不是问题。

    在如何定义PdfPTable 的单元格样式方面也没有很多变化。它们记录在官方网站上。

    【讨论】:

    • 我用过 itext7 ....
    【解决方案2】:

    这是一些用于处理表格的示例 iText 代码。

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(8);
    for(int aw = 0; aw < 16; aw++){
        table.addCell("hi");
    }
    document.add(table);
    document.close();
    

    【讨论】:

    • PS:此代码是通过谷歌搜索“iText 如何创建表格”获得的。我得到的第一个链接是 iText7 的代码示例,第二个链接是 iText5 的示例。请考虑在未来自己付出更多的努力来解决问题。
    猜你喜欢
    • 2016-04-25
    • 2018-04-04
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多