Tuesday, February 2, 2010

Exporting Datagridview data into HTML page in Window Application using VB.Net

Here i am showing how to Exporting Datagridview data into HTML page in Window Application using VB.Net.

Follows following procedure as

Create Database by name HMS.mdb in MS-Access
Now add new table with name “Services” and add field as follows


Now your table Services is successfully Created. Add few records in this table like as


Now, we create Windows Application using VB.Net language for exporting datagridview data into a HTML page.

Design Form like as follows


Add following code in Form1.vb
'Add namespace

Imports System.Data.OleDb

'Global Declaration of variable as follows

Public cmd As OleDbCommand
Public da As OleDbDataAdapter
Private ds As DataSet = New DataSet()

Public con As OleDbConnection

Now add following code on page load event of this form Form1 i.e in Form1.vb as follows

'Form Load and display it containts

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bRetVal As Boolean
        Dim sSql As String
        sSql = "select Channel_Service_ID,Channel_Service,Duration_Of_Service,Charge_For_Service,Service_Notes from Services;"

        Try

            bRetVal = getconnection()
            If bRetVal Then

                cmd = New OleDbCommand(sSql, con)

                da = New OleDbDataAdapter(cmd)

                da.Fill(ds, "tblServices")

                If (ds.Tables(0).Rows.Count > 0) Then

                    DataGridView1.DataSource = ds.Tables("tblServices")

                    Dim columnCount As Integer = ds.Tables(0).Columns.Count
                    Dim gridViewWidth As Integer = DataGridView1.Width
                    Dim i As Integer

                    For i = 0 To DataGridView1.Columns.Count

                        Dim ColumnWidth As Integer = gridViewWidth / columnCount
                        DataGridView1.Columns(i).Width = ColumnWidth
                    Next
                End If

            Else
                MessageBox.Show("Error in establishing connection ")
            End If
        Catch ex As Exception

            MessageBox.Show("There is problem is occurred… ")
        Finally

            con.Close()
        End Try

    End Sub

For above code there is needed connection with database, this is done by getconnection() method. For this add following code in Form1.vb

'getconnection method which is connected with database. This open connection
Function getconnection() As Boolean
        Dim bRetVal As Boolean
        Dim conStr As String
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Application.StartupPath + "\\Database\\HMS.mdb")

        Try
            con.Open()
            'MessageBox.Show("Establishing connection....");
            bRetVal = True
        Catch ex As Exception
            bRetVal = False
        Finally

        End Try
        Return bRetVal
    End Function
Now we add the code for exporting datagridview data into HTML page on button click event in Form1.vb as


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim strIcode, strDescri, strCat, nQty As String
        Dim dCP, dSRP, dTCP, dTSRP As String
        Dim strFName As String = "C:\Sumedh\CMS Files\Services Details.html"
        Dim sHTML As String

        If DataGridView1.RowCount = 0 Then Exit Sub

        sHTML = "<html><head><title>Services Details Report</title></head>"
        sHTML = sHTML & "<body><center><br>"
        sHTML = sHTML + "<font size=5><b>Hospiatal Services</b></font><br>"
        sHTML = sHTML + "<font size=3>Date: " + System.DateTime.Now.Date + "<br><br>"
        sHTML = sHTML + "<table border=1 cellpadding=0 cellspacing=0>"
        sHTML = sHTML + "<tr><td><b>Channel_Service_ID</td><td><b>Channel_Service</td><td><b>Charge_For_Service</td><td><b>Duration_Of_Service</td>" + "<td><b>Service_Notes</td></tr>"

        For i = 0 To DataGridView1.RowCount - 1
            strIcode = DataGridView1.Item("Channel_Service_ID", i).Value
            strDescri = DataGridView1.Item("Channel_Service", i).Value
            strCat = DataGridView1.Item("Charge_For_Service", i).Value
            nQty = DataGridView1.Item("Duration_Of_Service", i).Value
            dCP = DataGridView1.Item("Service_Notes", i).Value

            sHTML = sHTML + "<tr><td>" + strIcode + "</td><td>" + strDescri + "</td><td>" + strCat + "</td>" + "<td>" + nQty + "</td><td>" + dCP + "</td></tr>"

        Next

        sHTML = sHTML & "</table></body></html>"

        If FileIO.FileSystem.DirectoryExists("C:\") Then
            SaveToHtml(sHTML, strFName)
            Process.Start(strFName)
        Else
            MsgBox("Drive C does not exist. Please do make drive C available.", MsgBoxStyle.Information)
        End If
        '// StopProcess()
    End Sub

Function SaveToHtml(ByVal strHTMLCodes As String, ByVal strFullPath As String) As Boolean




        Dim objReader As IO.StreamWriter
        If FileIO.FileSystem.DirectoryExists("C:\Services\Files") = False Then
            FileIO.FileSystem.CreateDirectory("C:\ Services \Files")
        End If
        Try
            objReader = New IO.StreamWriter(strFullPath)
            objReader.Write(strHTMLCodes)
            objReader.Close()
            Return True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information)
            Return False
        End Try
    End Function

Save the entire application and run it. Check out the output result.





















No comments:

Post a Comment