Thursday, September 5, 2013

How to generate genuine excel report using string builder with C# code

How to generate genuine excel report using string builder with Asp.net and Csharp
In this article, I would like to explain "How to generate genuine excel report using string builder with Asp.net and C# code.In previous article i have already explained about PDF generation Generate Random Password for PDF with asp.net and Create Password protected PDF using iTextsharp with asp.net and c# .
Here I am going to explain about "Generation genuine excel report using string builder with Asp.net and C# code ". 

Please follow the steps

Step 1: Open visual studio from start menu. 
Step 2: In Visual studio in file menu create one new project 
Step 3: In Aspx page create on button using below tag


Step 4: Call one method with the GetGenuineExcelreport() btngenuineExcel_Click event as below

 
 protected void btngenuineExcel_Click(object sender, EventArgs e)
 {
  GetGenuineExcelreport("Excelreport"); // here "Excelreport" means your excel file name
 }
Step 5: Under GetGenuineExcelreport( string name) method write following code 
Step 6:Please add the following code in button click event
public void GetGenuineExcelr(string name)
        {
            string fileName = "attachment;fileName=" + name;

            Response.ContentType = "application/vnd.ms-excel";

            fileName = fileName + ".xls";

            Response.AddHeader("Content-Disposition", fileName);

            string[] font = new string[] {"Verdana", "Arial", "Sans-Serif" };

            Response.Charset = "";

            this.EnableViewState = false;

            StringBuilder sb = new StringBuilder(); 
            // Here we can find StringBuilder calss using using System.Text name space
            sb.Append("Write your Text Here");

            StringWriter strwiriter = new StringWriter();

            strwiriter.Write(sb.ToString());

            HtmlTextWriter ohtmltextwriter = new HtmlTextWriter(strwiriter);

            Repeater rt = new Repeater();

            rt.RenderControl(ohtmltextwriter);

            Response.Write(strwiriter.ToString());

            Response.End();
            
        }
Step 7: Now run the application and click on "GenerateGenuineExcel" button the excel report will generate..you can customize your own data or from data set data. 

That's it..Happy coding. 

Please let me know if you any issues..

Labels: , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home