Naidu AMMAN

Naidu AMMAN

  • NA
  • 37
  • 35.1k

code for export datagrid to pdf using vb Code

Aug 5 2014 1:06 AM
hi i write code for export data grid to excel in vb code its working successfully and how to write for export data grid to PDF in same vb code format. Please help me as soon as possible.
---------------------------------------------------------------------------------------------
Public Class frmStillPendingVehicles


    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        Dim strsql As String = ""
        strsql = "select trasactionID,VehicleNumber,VehicleType,format(INDATE,'dd/MM/yyyy') as VehicleINDATE  ,format(INDATE,'HH:mm:ss') as  VehicleINTIME from tblcollection where OUTDATE is NULL" ' and INDATE>=#" & DtFrom.Value.Date & "# and INDATE<#" & DTto.Value.AddDays(1).Date & "# "
        Dim m_ds As New DataSet
        Dim m_db As New CDataAccess
        m_ds = m_db.getdata(strsql, strconnection)
        If Not m_ds Is Nothing Then
            DataGridView1.DataSource = m_ds.Tables(0)
        Else
            MsgBox("No Records found", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim m_excel As String = ""
        If SfdlgDownload.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ExportDataToExcel(SfdlgDownload.FileName, DataGridView1)
        End If
      
    End Sub

    Public Sub ExportDataToExcel(ByVal FilePath As String, ByVal DataGrid As DataGridView)
        Dim _mFileStream As New IO.StreamWriter(FilePath, False)
        Try
            _mFileStream.WriteLine("<?xml version=""1.0""?>")
            _mFileStream.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
            _mFileStream.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
            _mFileStream.WriteLine("    <ss:Styles>")
            _mFileStream.WriteLine("        <ss:Style ss:ID=""1"">")
            _mFileStream.WriteLine("           <ss:Font ss:Bold=""1""/>")
            _mFileStream.WriteLine("           <ss:FontName=""Courier New""/>")
            _mFileStream.WriteLine("        </ss:Style>")
            _mFileStream.WriteLine("    </ss:Styles>")
            _mFileStream.WriteLine("    <ss:Worksheet ss:Name=""Sheet1"">")
            _mFileStream.WriteLine("        <ss:Table>")
            For x As Integer = 0 To DataGrid.Columns.Count - 1
                _mFileStream.WriteLine("            <ss:Column ss:Width=""{0}""/>", DataGrid.Columns.Item(x).Width)
            Next
            _mFileStream.WriteLine("            <ss:Row ss:StyleID=""1"">")
            For i As Integer = 0 To DataGrid.Columns.Count - 1
                _mFileStream.WriteLine("                <ss:Cell>")
                _mFileStream.WriteLine(String.Format("                   <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGrid.Columns.Item(i).HeaderText))
                _mFileStream.WriteLine("</ss:Cell>")
            Next
            _mFileStream.WriteLine("            </ss:Row>")
            For intRow As Integer = 0 To DataGrid.RowCount - 2
                _mFileStream.WriteLine(String.Format("            <ss:Row ss:Height =""{0}"">", DataGrid.Rows(intRow).Height))
                For intCol As Integer = 0 To DataGrid.Columns.Count - 1
                    _mFileStream.WriteLine("                <ss:Cell>")
                    _mFileStream.WriteLine(String.Format("                   <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGrid.Item(intCol, intRow).Value.ToString))
                    _mFileStream.WriteLine("                </ss:Cell>")
                Next
                _mFileStream.WriteLine("            </ss:Row>")
            Next
            _mFileStream.WriteLine("        </ss:Table>")
            _mFileStream.WriteLine("    </ss:Worksheet>")
            _mFileStream.WriteLine("</ss:Workbook>")
            _mFileStream.Close()
            _mFileStream.Dispose()
            _mFileStream = Nothing
        Catch ex As Exception
            _mFileStream.Close()
            _mFileStream.Dispose()
            _mFileStream = Nothing
            MessageBox.Show("Error While Exporting Data To Excel. Error : " & ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

Answers (1)