vb.net如何从datagridview导出pdf

时间:2017-05-18 03:48:40

标签: vb.net pdf datagridview

我设法链接mssql获取数据并生成到pdf。但是现在我有一个问题,我只能得到我的sql数据库标题行,其他数据行无法显示。任何解决这个问题的建议。对不起我的英文,希望可以理解我的问题。

这是我的代码

Public Class Form1
Dim sCommand As SqlCommand
Dim sAdapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim sDs As DataSet
Dim sTable As DataTable

Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
    Dim connectionString As String = "Server= SHQN080-0116; Database= HKT ;Integrated Security=SSPI;"
    Dim sql As String = "SELECT * FROM SalArea"
    Dim connection As New SqlConnection(connectionString)
    connection.Open()
    sCommand = New SqlCommand(sql, connection)
    sAdapter = New SqlDataAdapter(sCommand)
    sBuilder = New SqlCommandBuilder(sAdapter)
    sDs = New DataSet()
    sAdapter.Fill(sDs, "SalArea")
    sTable = sDs.Tables("SalArea")
    connection.Close()
    DataGridView1.DataSource = sDs.Tables("SalArea")
    DataGridView1.ReadOnly = True
    save_btn.Enabled = False
    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

End Sub
Private Sub export_btn_Click(sender As System.Object, e As System.EventArgs) Handles export_btn.Click
    Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
    pdfTable.DefaultCell.Padding = 3
    pdfTable.WidthPercentage = 30
    pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
    pdfTable.DefaultCell.BorderWidth = 1

    'Adding Header row
    For Each column As DataGridViewColumn In DataGridView1.Columns
        Dim cell As New PdfPCell(New Phrase(column.HeaderText))
        cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240)
        pdfTable.AddCell(cell)
    Next

    'Adding DataRow
    For Each row As DataGridViewRow In DataGridView1.Rows
        For Each cell As DataGridViewCell In row.Cells
            pdfTable.AddCell(cell.Value.ToString())
        Next
    Next

    'Exporting to PDF
    Dim folderPath As String = "C:\PDFs\"
    If Not Directory.Exists(folderPath) Then
        Directory.CreateDirectory(folderPath)
    End If
    Using stream As New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
        Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
        PdfWriter.GetInstance(pdfDoc, stream)
        pdfDoc.Open()
        pdfDoc.Add(pdfTable)
        pdfDoc.Close()
        stream.Close()
    End Using
End Sub

结束班

1 个答案:

答案 0 :(得分:1)

对不起,您的回复很晚,希望对您有所帮助。代码中的错误是,当您应引用单元格的.formattedvalue时,您正在引用单元格的.value属性。这是代码

Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
    pdfTable.DefaultCell.Padding = 3
    pdfTable.WidthPercentage = 30
    pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
    pdfTable.DefaultCell.BorderWidth = 1

    'Adding Header row
    For Each column As DataGridViewColumn In DataGridView1.Columns
        Dim cell As New PdfPCell(New Phrase(column.HeaderText))
        cell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
        pdfTable.AddCell(cell)
    Next

    'Adding DataRow
    Dim cellvalue As String = ""
    Dim i As Integer = 0
    For Each row As DataGridViewRow In DataGridView1.Rows
        For Each cell As DataGridViewCell In row.Cells
            cellvalue = cell.FormattedValue
            pdfTable.AddCell(Convert.ToString(cellvalue))
        Next
    Next

    'Exporting to PDF
    Dim folderPath As String = "C:\PDFs\"
    If Not Directory.Exists(folderPath) Then
        Directory.CreateDirectory(folderPath)
    End If
    Using stream As New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
        Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
        PdfWriter.GetInstance(pdfDoc, stream)
        pdfDoc.Open()
        pdfDoc.Add(pdfTable)
        pdfDoc.Close()
        stream.Close()
    End Using

End Sub