Create Excel File using ASP.NET C#
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 stepsfirst 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#
You can create excel file with multiple worksheets in C# by using Aspose.Cells for .NET:
http://www.aspose.com/.net/excel-component.aspx