Showing posts with label Crystal Report. Show all posts
Showing posts with label Crystal Report. Show all posts

Thursday, April 7, 2011

Create CSV/Excel file in C#


Export GridView data in Excel

Export datatable's data in Excel

Export Data in Excel using Asp.net

using below code you can export data in .csv file which can be opened in excel.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

/// 
/// Summary description for csvexport
/// 
public class CSVExporter
{
    
    public static void WriteToCSV(List personList)
    {
        string attachment = "attachment; filename=PersonList.csv";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.AddHeader("Pragma", "public");
        WriteColumnName();
        foreach (Person person in personList)
        {
            WriteUserInfo(person);
        }
        HttpContext.Current.Response.End();
    }

    private static void WriteUserInfo(Person person)
    {
        StringBuilder stringBuilder = new StringBuilder();
        AddComma(person.name , stringBuilder);
        AddComma(person.address , stringBuilder);       
        HttpContext.Current.Response.Write(stringBuilder.ToString());
        HttpContext.Current.Response.Write(Environment.NewLine);
    }

    private static void AddComma(string value, StringBuilder stringBuilder)
    {
        stringBuilder.Append(value.Replace(',', ' '));
        stringBuilder.Append(", ");
    }

    private static void WriteColumnName()
    {
        string columnNames = "RegistrationType, Value";
        HttpContext.Current.Response.Write(columnNames);
        HttpContext.Current.Response.Write(Environment.NewLine);
    }
}
public class Person
{
    public string name;
    public string address;
    public Person()
    {

    }
    public Person( string name , string address)
    {
        this.name = name;
        this.address = address;
    }
}


Solution By: Rajesh Rolen

Read More

Wednesday, August 4, 2010

Show Text Vertical in Crystal Reports in Asp.Net

sometimes we need to show our text or we can say header text in vertical. and we try using rotate property but it not works.. so to do so we have got a perfect solution to make browser fool.

we can do be using below steps:
1. take a label/text.
2. set your font size as required
3. now set make its height large and set width in such a manner so only one character can be displayed.
4. go to format font->spacing->character spacing exactly: and enter something like 20/25

and done....

solution by: Rajesh Rolen
Read More

Crystal Report in asp.net

To create crystal report in asp.net follow below steps
1. take a crystal report viewer on page (i have given name "crv" to it)
then write below code in code behind file.

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

private void showreport()
{
ReportDocument report = new ReportDocument();
report.Load(Server.MapPath("MyReportName.rpt"));
report.SetDataSource(objrep.ReportDataSource);
SetTableLocation(report.Database.Tables);
crv.ReportSource = report; //crv is your Crystal report viewer's name.
}


private void SetTableLocation(Tables tables)
{
ConnectionInfo connectionInfo = new ConnectionInfo();

connectionInfo.ServerName = @"DBSERVER2\SQL2005";
connectionInfo.DatabaseName = "dbMEAVer5web";
connectionInfo.UserID = "sa";
connectionInfo.Password = "admin55";

foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
tableLogOnInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogOnInfo);
}
}



Solution by: Rajesh Rolen
Read More

Monday, September 28, 2009

Custom Page Size Crystal Report by Code

Dim oRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
oRpt.Load(Server.MapPath("CrystalReport.rpt"))
oRpt.PrintOptions.PaperOrientation = [Shared].PaperOrientation.Portrait
oRpt.PrintOptions.PaperSize = [Shared].PaperSize.PaperEnvelope10

CrystalReportViewer1.ReportSource = oRpt
Read More
Powered By Blogger · Designed By Seo Blogger Templates