0

我正在尝试使用以下代码导出填充的 datagridview:

Imports System.Data.OleDb
Imports Microsoft.Office.Interop.Excel
Imports System.Drawing
Imports DGVPrinterHelper
Imports iTextSharp
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.Data.Odbc
Imports System.IO     

Private Sub Export_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    SaveFileDialog1.ShowDialog()
    If SaveFileDialog1.FileName = "" Then
        MsgBox("Enter Filename to create PDF")
        Exit Sub
    Else
        ExportDataToPDFTable()
        MsgBox("PDF Created Successfully")
    End If
End Sub

Private Function GetDataTable() As DataTable
    Dim dataTable As New Data.DataTable("MyDataTable")
    'Create another DataColumn Name
    For column As Integer = 1 To DataGridView1.ColumnCount - 1
        Dim dataColumn_1 As New DataColumn(DataGridView1.Columns(column).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_1)
        'Now Add some row to newly created dataTable
        Dim dataRow As DataRow
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            dataRow = dataTable.NewRow()
            ' Important you have create New row
            dataRow(DataGridView1.Columns(column).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(column).Value.ToString()
            dataTable.Rows.Add(dataRow)
        Next i
    Next column
    dataTable.AcceptChanges()
    Return dataTable
End Function

Private Sub ExportDataToPDFTable()
    Dim paragraph As New Paragraph
    Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
    Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(SaveFileDialog1.FileName + ".pdf", FileMode.Create))
    doc.Open()

    Dim font12BoldRed As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.UNDERLINE Or iTextSharp.text.Font.BOLDITALIC, BaseColor.RED)
    Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
    Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

    Dim p1 As New Phrase
    p1 = New Phrase(New Chunk("PDF From Datagridview Data", font12BoldRed))
    doc.Add(p1)

    'Create instance of the pdf table and set the number of column in that table
    Dim PdfTable As New PdfPTable(5)
    PdfTable.TotalWidth = 490.0F
    'fix the absolute width of the table
    PdfTable.LockedWidth = True
    'relative col widths in proportions - 1,4,1,1 and 1
    Dim widths As Single() = New Single() {1.0F, 4.0F, 1.0F, 1.0F, 1.0F}
    PdfTable.SetWidths(widths)
    PdfTable.HorizontalAlignment = 1 ' 0 --> Left, 1 --> Center, 2 --> Right
    PdfTable.SpacingBefore = 2.0F

    'pdfCell Decleration
    Dim PdfPCell As PdfPCell = Nothing

    'Assigning values to each cell as phrases
    PdfPCell = New PdfPCell(New Phrase(New Chunk("Taxcode", font12Bold)))
    'Alignment of phrase in the pdfcell
    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
    'Add pdfcell in pdftable
    PdfTable.AddCell(PdfPCell)
    PdfPCell = New PdfPCell(New Phrase(New Chunk("Tax Name", font12Bold)))
    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
    PdfTable.AddCell(PdfPCell)
    PdfPCell = New PdfPCell(New Phrase(New Chunk("Cess Tax", font12Bold)))
    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
    PdfTable.AddCell(PdfPCell)
    PdfPCell = New PdfPCell(New Phrase(New Chunk("Sales Tax", font12Bold)))
    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
    PdfTable.AddCell(PdfPCell)
    PdfPCell = New PdfPCell(New Phrase(New Chunk("Other Tax", font12Bold)))
    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
    PdfTable.AddCell(PdfPCell)

    Dim dt As DataTable = GetDataTable()
    If dt IsNot Nothing Then
        'Now add the data from datatable to pdf table
        For rows As Integer = 0 To dt.Rows.Count - 1
            For column As Integer = 0 To dt.Columns.Count - 1
                PdfPCell = New PdfPCell(New Phrase(dt.Rows(rows)(column).ToString(), font12Normal))
                If column = 0 Or column = 1 Then
                    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                Else
                    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
                End If
                PdfTable.AddCell(PdfPCell)
            Next
        Next
        'Adding pdftable to the pdfdocument
        doc.Add(PdfTable)
    End If
    doc.Close()
End Sub

当我运行程序时,单击按钮会提示我保存文件对话框,但是一旦我单击保存,我就会收到错误“无法将'System.Data.DataTable'类型的对象转换为'Microsoft.Office.Interop.Excel'。数据表'。” 在私有函数 GetDataTable() 中的“返回数据表”行。谁能帮我解决这个问题?请随时指出我的代码中的其他错误(如果有的话)

4

0 回答 0