Resources » Code Snippets » File Operations

Create Excel File using ASP.NET C#


Posted Date: 06-May-2010  Last Updated:   Category: File Operations    
Author: Member Level: Silver    Points: 8


Create Excel File using ASP.NET C# , by writing only some code of lines ,this is very easy code for generating Reporting using without third party control.



Create Excel File



Hi, Friends

this code is help to Creating a MS Excel file using ASP.NET C#
I have not using any third party control , only use of some ASp.NET Controls and
using basic HTML code

lets follows some easy steps

first Step



1. Create a ASP.NET C# website
2. Add a webform
3. place two command buttons and one Literal Control on the web form




first command button for showing report and another for exporting into Excel File
and Literal Control fpr printing report on the page.




Second Step



// in the .aspx.cs file add assembly referance



using System.Text;



Third Step


//lets start code in .cs file




public partial class Print_Excel : System.Web.UI.Page
{

//create objects of StringBuilder for adding htmls

StringBuilder str = new StringBuilder();
StringBuilder str1 = new StringBuilder();

//create a simple HTML Table using basic HTML Tags
//append all tags into string variable

protected void Page_Load(object sender, EventArgs e)
{
str.Append("<table align='center' border='1' bordercolor='#00aeef' width='99%' class='reporttable1' cellspacing='0' cellpadding='0' style='font-size:10;'>");
str.Append("<tr>");
str.Append("<td>");
str.Append("<b>S.No</b>");
str.Append("</td>");
str.Append("<td>");
str.Append("<b>Name</b>");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td>");
str.Append("1");
str.Append("</td>");
str.Append("<td>");
str.Append("Shubhang Mathur");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td>");
str.Append("2");
str.Append("</td>");
str.Append("<td>");
str.Append("Shubhang Sahai Mathur");
str.Append("</td>");
str.Append("</tr>");
str.Append("</table>".ToString());
}

//for exporting excel file
//this code indicates the report will export into excel file

protected void Button2_Click(object sender, EventArgs e)
{
str1.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Time</title>");
str1.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div class=Section1>");
str1.Append("<DIV style='font-size:12px;'>");
str1.Append(str.ToString());
str1.Append("</div></body></html>");
string strFile = "Text_Excel.xls";
string strcontentType = "application/excel";
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = strcontentType;

//file open mode with file name

Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
Response.Write(str1.ToString());
Response.Flush();
Response.Close();
Response.End();

}




//for printing report on page


protected void Button1_Click(object sender, EventArgs e)
{
Literal1.Text = str.ToString();
}
}



Fourth Step


//Run Your page code

// and Enjoy your Self


Reference: Create Excel File using ASP.NET C#
Related Resources:


Read related articles: Export to excel    Create excel file    String Builder    

Did you like this resource? Share it with your friends and show your love!

Responses to "Create Excel File using ASP.NET C#"
Guest Author: bhavana     12 Nov 2013
You can create excel file with multiple worksheets in C# by using Aspose.Cells for .NET:

http://www.aspose.com/.net/excel-component.aspx



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Phagu Mahato
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India