Lots of reporting tools are available for .NET projects, such as crystal reports. However, you will need to install packages and dependencies to run reports on a local machine.
The following tutorial will show you how to generate a report in a VB.NET application using the web browser control.
First, create a new form, and name it as frmPrintPreview.
In this form, add an AxWebBrowser control, name it as wb. You can adjust the width, height of the web browser to fit in the form.
Switch to the View Code mode, and write the following function:
Function GenerateHTML () As String Dim dt As New DataTable
dt = LoadData
Dim i As Integer
Dim str As String
str += " <table width=100% border='1'>"
str += " <tr>"
str += " <td><b>First Name</b></td>"
str += " <td><b>Last Name</b></td>"
str += " <td><b>Position</b></td>"
str += "</tr>"
For i = 0 To dt.Rows.Count - 1
str += "<tr>"
str += " <td>" & dt.Rows(i).Item("FirstName") & "</td>"
str += " <td>" & dt.Rows(i).Item("LastName") & "</td>"
str += " <td>" & dt.Rows(i).Item("Position") & "</td>"
str += "</tr>"
Next
str += "</table>"
Return str
End Function
The above function generates an html document having a table in it.
Supposing that you already have a function that fill data into the dt DataTable (you can use DataSet or DataReader), a loop will create the dynamic part of the table and place the FirstName, LastName and Positions into cells.
Next, create a function as the following:
Function SaveTextFile(ByVal FilePath As String, ByVal FileContent As String, _
Optional ByVal Append As Boolean = False) As Boolean
Dim sw As System.IO.StreamWriter
Try
sw = New System.IO.StreamWriter(FilePath, Append)
sw.Write(FileContent)
Return True
Catch e As Exception
Return False
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Function
This function will save the HTML file onto your HardDrive using the StreamWriter class.
Then, the form load sub should look like this:
Private Sub frmPrintPreview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim tempFile As String = Path.ChangeExtension(Path.GetTempFileName(), "htm")
SaveTextFile(tempFile, GenerateHTML, False)
wb.Navigate(tempFile)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
It creates an tempFile, then save the content created by the GenerateHTML function into it, then show this tempFile using the WebBrowser control.
Debug your application and make sure there is no error. Based on this example and my Database, the report should look like this :

You can add record counts, logos, header, css design to make this report looks more professional.
To print, simply right click on the content of the report and choose print.
Happy coding!




