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.


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="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<div><asp:LinkButton ID="Excel" runat="server" OnClick="Excel_onclick" >Excel Download
<asp:LinkButton ID="Text" runat="server" OnClick="Text_onclick" >Text File Download

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.AddHeader("content-disposition", "attachment;filename=" + strFileName + ".xls");
protected void Text_onclick(object sender, EventArgs e)
string file = "karthi";
Response.AddHeader("Content-Disposition", "attachment;filename=" + file + ".txt");


Output 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.



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: