locked
How to Export and Print Crystal report record. RRS feed

  • Question

  • User-526882573 posted

    Hai,

    I'm using VS2010 and SQL Server2005.

    I'm creating Salary Slip using Crystal Report in my project. Its run successfully and display the record in Crystal Report what I Needed.

    and the Next thing is i want to take print out from that report and Export That record to what i needed fromat(Pdf 0r Word or Execl).

    See below my code for displaying crystal report:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    fillddl();
    }
    }


    public void fillddl()
    {
    SqlConnection con = new SqlConnection("Data Source=SERVICETEAM-PC;Initial Catalog=salaryReg;User ID=sa;Password=kavi");
    con.Open();
    SqlCommand com = new SqlCommand("select empName from SalaryEarnings ", con);
    SqlDataReader dr = com.ExecuteReader();
    ddlUserName.Items.Clear();
    while (dr.Read())
    {
    ddlUserName.Items.Add(new ListItem(dr[0].ToString()));
    }
    ddlUserName.Items.Insert(0, new ListItem("--Select--", "0"));
    con.Close();
    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
    btnExport.Visible = true;
    dssample ds = new dssample(); // .xsd file name
    DataTable dt = new DataTable();
    dt.TableName = "Crstal Report";
    dt = GetAllOrders();
    ds.Tables[0].Merge(dt);
    if (ds.Tables[0].Rows.Count > 0)
    {
    rptDoc.Load(Server.MapPath("~/CrystalReport.rpt"));

    rptDoc.SetDataSource(ds);
    CrystalReportViewer1.ReportSource = rptDoc;
    CrystalReportViewer1.RefreshReport();
    }
    else
    {
    Response.Write("<script type=\"text/javascript\">alert('No record found for the month & year!...');</script>");
    }
    }


    public DataTable GetAllOrders()
    {
    SqlConnection cn = new SqlConnection("Data Source=SERVICETEAM-PC;Initial Catalog=salaryReg;User ID=sa;Password=kavi");
    SqlCommand cmd = new SqlCommand("Select * from SalaryEarnings where empName='" + ddlUserName.Text + "' and Salary_Year='"+ddlSalaryYear.Text+"' and Salary_Month='"+ddlSalayMonth.Text+"'", cn);
    DataSet ds = null;
    SqlDataAdapter da;
    try
    {
    cn.Open();
    ds = new DataSet();
    da = new SqlDataAdapter(cmd);
    da.Fill(ds, "User");
    }
    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }
    finally
    {
    cmd.Dispose();
    if (cn.State != ConnectionState.Closed)
    cn.Close();
    }
    return ds.Tables[0];
    }

     Note: I want to know how to Export the report and take print out of that record....

    Thursday, January 30, 2014 8:02 AM

All replies

  • User-1312722619 posted

    This is how to export report into Excel and write to the disc.

    http://forums.asp.net/t/1746293.aspx?How+to+write+SSRS+report+into+server+folder+as+Excel+File

    If you need to send excel file to client via browser, then you should use this code

    Public Sub RenderReportToExcel(ByVal _ReportPath As String)
    
        ReportViewerCostReport.Visible = True
        ReportViewerCostReport.Reset()
        ReportViewerCostReport.ServerReport.ReportPath = "/myReportFolder/reports/" + _ReportPath
        ReportViewerCostReport.ServerReport.ReportServerUrl = New System.Uri("http://localhost/ReportServer")
        ReportViewerCostReport.ServerReport.ReportServerCredentials = New MyReportServerCredentials2()
    
        Dim myparamProjectID As ReportParameter
        Dim myparamsProjectID As New List(Of ReportParameter)
        myparamProjectID = New ReportParameter("ProjectID", DropDownListPrj.SelectedValue)
        myparamsProjectID.Add(myparamProjectID)
        ReportViewerCostReport.ServerReport.SetParameters(myparamsProjectID)
    
        Dim warnings As Warning
        Dim streamIds As String
        Dim mimeType As String = String.Empty
        Dim encoding As String = String.Empty
        Dim extension As String = "xls"
    
        Dim bytes As Byte() = ReportViewerCostReport.ServerReport.Render("Excel", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
    
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ContentType = mimeType
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + _ReportPath + "." + extension)
        HttpContext.Current.Response.BinaryWrite(bytes)
        HttpContext.Current.Response.Flush()
    
      End Sub

    Thursday, January 30, 2014 8:16 AM
  • User-526882573 posted

    Oh Sorry Mezzanine, I'm Using C#, I dont have much knowledge in VB.NET...

    Thursday, January 30, 2014 8:27 AM
  • User-1312722619 posted

    Hello, You can use converters. http://www.developerfusion.com/tools/convert/vb-to-csharp/

    public void RenderReportToExcel(string _ReportPath)
    {
    	ReportViewerCostReport.Visible = true;
    	ReportViewerCostReport.Reset();
    	ReportViewerCostReport.ServerReport.ReportPath = "/myReportFolder/reports/" + _ReportPath;
    	ReportViewerCostReport.ServerReport.ReportServerUrl = new System.Uri("http://localhost/ReportServer");
    	ReportViewerCostReport.ServerReport.ReportServerCredentials = new MyReportServerCredentials2();
    
    	ReportParameter myparamProjectID = default(ReportParameter);
    	List<ReportParameter> myparamsProjectID = new List<ReportParameter>();
    	myparamProjectID = new ReportParameter("ProjectID", DropDownListPrj.SelectedValue);
    	myparamsProjectID.Add(myparamProjectID);
    	ReportViewerCostReport.ServerReport.SetParameters(myparamsProjectID);
    
    	Warning warnings = default(Warning);
    	string streamIds = null;
    	string mimeType = string.Empty;
    	string encoding = string.Empty;
    	string extension = "xls";
    
    	byte[] bytes = ReportViewerCostReport.ServerReport.Render("Excel", null, null, null, null, null, null);
    
    	HttpContext.Current.Response.Buffer = true;
    	HttpContext.Current.Response.Clear();
    	HttpContext.Current.Response.ContentType = mimeType;
    	HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + _ReportPath + "." + extension);
    	HttpContext.Current.Response.BinaryWrite(bytes);
    	HttpContext.Current.Response.Flush();
    
    }
    

    Thursday, January 30, 2014 8:39 AM