Exports generated crystal report into Excel Format & download.
1.First Create crystal report.
2.Create Interface to display report (i.e aspx Form) in that add Crystal Viewer (CrystalReportViewer1) & Command Button (btnExToExcel).
3.Change report name into CallReport().
4.CrystalReportViewer1 is an ID of Crystal Report Viewer. AND btnExToExcel is an ID of command button.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Web
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
CallReport()
End If
End Sub
Private Sub CallReport()
Dim objDS As New DataSet
Dim EmpReportDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim rptFile As String = Server.MapPath("rptEmployee.rpt")
EmpReportDoc.Load(rptFile)
Session("Emprpt") = EmpReportDoc
''Create Dataset as per requirement
EmpReportDoc.SetDataSource(objDS.Tables(0))
Me.CrystalReportViewer1.ReportSource = EmpReportDoc
CrystalReportViewer1.RefreshReport()
End Sub
#Region " Export To Excel "
Protected Sub btnExToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExToExcel.Click
Dim objEmpDoc As New ReportDocument
objEmpDoc = Session("Emprpt")
Dim oStream As MemoryStream = objEmpDoc.ExportToStream(ExportFormatType.Excel)
With HttpContext.Current.Response
.ClearContent()
.ClearHeaders()
.ContentType = "application/xls"
.AddHeader("Content-Disposition", "attachment; filename=Employee Details.xls")
.Clear()
.BinaryWrite(oStream.ToArray)
.End()
End With
End Sub
#End Region
-Thanks & Regards,
LALIT V.PATIL