Downloading file in Excel format or Text format using C#.NET

Downloading file in Excel format or Text format


We can download our output's in Excel sheet or text format.

Methods


1.Created the attachment with file name and file type.
2.documents moved to Response.write function.
3.We can get the response in the format file which you have mentioned in the attachment.

Code snippet - aspx



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>



<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div><asp:LinkButton ID="Excel" runat="server" OnClick="Excel_onclick" >Excel Download
</asp:LinkButton></div>
<div>
<asp:LinkButton ID="Text" runat="server" OnClick="Text_onclick" >Text File Download
</asp:LinkButton>
</div>
</div>
</form>
</body>
</html>


Code snippet - C#



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Excel_onclick(object sender, EventArgs e)
{
string strFileName = "karthi";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=" + strFileName + ".xls");
Response.Write("friendship");
Response.End();
}
protected void Text_onclick(object sender, EventArgs e)
{
string file = "karthi";
Response.AddHeader("Content-Disposition", "attachment;filename=" + file + ".txt");
Response.Write("friendship");
Response.End();
}

}

Output



1.you will download pop up window with file name,file type file size
2.inside the file whatever you have given in the code will be displayed,Here "friendship" will display.


Attachments

Comments

Author: ABitSmart14 Dec 2009 Member Level: Gold   Points : 0

Please format your resource.

Author: karthikeyan14 Dec 2009 Member Level: Silver   Points : 0

can u please review my code now..



  • 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:
    Email: