Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class WebForm3 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim rptDoc As ReportDocument = New ReportDocument Dim ds As DataSet1 = New DataSet1 Dim dt As DataTable = New DataTable ' Just set the name of data table dt.TableName = "Crystal Report Example" dt = getAllOrders 'This function is located below this function ds.Tables(0).Merge(dt) ' Your .rpt file path will be below rptDoc.Load(Server.MapPath("CrystalReport4.rpt")) 'set dataset to the report viewer. rptDoc.SetDataSource(ds) rptDoc.ExportToDisk(ExportFormatType.Excel, Server.MapPath("myExcelFile.xls")) CrystalReportViewer1.ReportSource = rptDoc End Sub Public Function getAllOrders() As DataTable 'Connection string replace 'databaseservername' with your db server name Dim sqlCon As String = ("myConnectionString") Dim Con As SqlConnection = New SqlConnection(sqlCon) Dim cmd As SqlCommand = New SqlCommand Dim ds As DataSet = Nothing Dim adapter As SqlDataAdapter Try Con.Open() 'Stored procedure calling. It is already in sample db. cmd.CommandText = "Select Top 23 X,Y From myTable" cmd.CommandType = CommandType.Text cmd.Connection = Con ds = New DataSet adapter = New SqlDataAdapter(cmd) adapter.Fill(ds, "Users") Catch ex As Exception Throw New Exception(ex.Message) Finally cmd.Dispose() If (Con.State <> ConnectionState.Closed) Then Con.Close() End If End Try Return ds.Tables(0) End Function End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)