Export Crystal Report to HTML Document using C# and VB.Net in Windows Application

Last Reply 4 months ago By dharmendr

Posted 4 months ago

I want to Export Crystal Report Data to HTML Documents format

When I click the button the present report should export to HTML Documents format with a specific name.

I want to do it on Desktop applicantion

    Private Sub btnPrintDoc_Click(sender As Object, e As EventArgs) Handles btnPrintDoc.Click
        Dim myReportDocument As New BGLBillBck
        Try
            Dim selectedReport As New ReportClass
            Dim CrFormatTypeOptions As New HTMLFormatOptions
            CrFormatTypeOptions.HTMLBaseFolderName = "C:\Users\Administrator\Desktop\html\New folder"
            CrFormatTypeOptions.HTMLFileName = "htmlformat.htm"
            Dim CrExportOptions As ExportOptions
            CrExportOptions = myReportDocument.ExportOptions
            With CrExportOptions
                .ExportFormatType = ExportFormatType.HTML40
                .FormatOptions = CrFormatTypeOptions
            End With
            myReportDocument.Export()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

 

Posted 4 months ago

Hi nirmal90,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Code

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerId,ContactName,City,Country FROM Customers");
        BindReport(cmd);
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerId,ContactName,City,Country FROM Customers WHERE ContactName LIKE @Name + '%'");
        cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
        BindReport(cmd);
    }

    CustomerReport crystalReport;
    private void BindReport(SqlCommand cmd)
    {
        crystalReport = new CustomerReport();
        Customers dsCustomers = GetData(cmd);
        crystalReport.SetDataSource(dsCustomers);
        this.crystalReportViewer1.ReportSource = crystalReport;
        this.crystalReportViewer1.RefreshReport();
    }

    private Customers GetData(SqlCommand cmd)
    {
        string constr = @"Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=pass@123;";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                sda.SelectCommand = cmd;
                using (Customers dsCustomers = new Customers())
                {
                    sda.Fill(dsCustomers, "DataTable1");
                    return dsCustomers;
                }
            }
        }
    }

    private void btnExport_Click(object sender, EventArgs e)
    {
        crystalReport.ExportToDisk(ExportFormatType.HTML40, @"D:\Files\CustomerReport.htm");
    }
}

VB.Net

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 10 CustomerId,ContactName,City,Country FROM Customers")
        BindReport(cmd)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 10 CustomerId,ContactName,City,Country FROM Customers WHERE ContactName LIKE @Name + '%'")
        cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
        BindReport(cmd)
    End Sub

    Dim crystalReport As CustomerReport
    Private Sub BindReport(ByVal cmd As SqlCommand)
        crystalReport = New CustomerReport()
        Dim dsCustomers As Customers = GetData(cmd)
        crystalReport.SetDataSource(dsCustomers)
        Me.CrystalReportViewer1.ReportSource = crystalReport
        Me.CrystalReportViewer1.RefreshReport()
    End Sub

    Private Function GetData(ByVal cmd As SqlCommand) As Customers
        Dim constr As String = "Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=pass@123;"
        Using con As SqlConnection = New SqlConnection(constr)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                sda.SelectCommand = cmd
                Using dsCustomers As Customers = New Customers()
                    sda.Fill(dsCustomers, "DataTable1")
                    Return dsCustomers
                End Using
            End Using
        End Using
    End Function

    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        crystalReport.ExportToDisk(ExportFormatType.HTML40, "D:\Files\CustomerReport.htm")
    End Sub
End Class

Generated HTML

Refer below article for export details.

 

Export Crystal Report to Word Excel PDF and CSV in Windows Application using C# and VB.Net