Click here to Skip to main content
15,902,112 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Questionany example on webparts and personalisation of webparts using db as oracle Pin
lakshmichawala28-Jun-09 21:21
lakshmichawala28-Jun-09 21:21 
AnswerRe: any example on webparts and personalisation of webparts using db as oracle Pin
Ravi Mori28-Jun-09 23:23
Ravi Mori28-Jun-09 23:23 
QuestionAjax Pin
senthilsstil28-Jun-09 21:10
senthilsstil28-Jun-09 21:10 
AnswerRe: Ajax Pin
Christian Graus28-Jun-09 21:16
protectorChristian Graus28-Jun-09 21:16 
QuestionRegrading Excel Generation. Pin
harish mehra28-Jun-09 20:10
harish mehra28-Jun-09 20:10 
AnswerRe: Regrading Excel Generation. Pin
N a v a n e e t h28-Jun-09 20:22
N a v a n e e t h28-Jun-09 20:22 
AnswerRe: Regrading Excel Generation. Pin
Christian Graus28-Jun-09 20:29
protectorChristian Graus28-Jun-09 20:29 
AnswerRe: Regrading Excel Generation. Pin
Niladri_Biswas30-Jun-09 3:39
Niladri_Biswas30-Jun-09 3:39 
Try this example. I did this in one of my project. I generated the excel after reading the values from a GridView.

STEP 1:Create a function call DownLoad() to download the excel file

/* Function Name: Download()
        Purpose: Downloads the Excel file*/

     private void Download()
    {
        /* Set necessary properties to for the response object to render excel document.*/

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.ContentType = "application/vnd.xls";
        Response.Charset = "";
        this.EnableViewState = false;

        /* Creating a string writer object */
        System.IO.StringWriter objStringWriter = GenerateExcelBody();

        /* Content written to the HtmlTextWriter (StringWriter) is written to the Response object.*/

        Response.Write(objStringWriter.ToString());
        Response.End();

    }


STEP 2:Create a function called GenerateExcelBody() to generate the excel boby

/* Function Name: GenerateExcelBody()
      Purpose: Creates the Excel body */

    private System.IO.StringWriter GenerateExcelBody()
    {
        /* Instantiate a HtmlTextWriter with a System.IO.StringWriter object */

        System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);

	string strVal =string.Empty;

       
            /* Page wise Looping thru the grid */
            for (int i = 0; i < grEims.Rows.Count; i++)
            {
               
                    if (i % 2 == 0)
                    {
                        strVal += "<tr bgcolor='white'><td>" + grEims.Rows[i].Cells[0].Text + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[1].Text + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[2].Text.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[3].Text.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[4].Text.ToString() + "&nbsp;" + grEims.Rows[i].Cells[5].Text.ToString() + "</td></tr>" + Environment.NewLine;

                    }
                    else
                    {
                        strVal += "<tr bgcolor='white'><td>" + grEims.Rows[i].Cells[0].Text + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[1].Text + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[2].Text.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[3].Text.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + grEims.Rows[i].Cells[4].Text.ToString() + "&nbsp;" + grEims.Rows[i].Cells[5].Text.ToString() + "</td></tr>" + Environment.NewLine;
                    }
            }
       

       

        /* Formating the Excel body */

        objHtmlTextWriter.WriteLine("<table width='100%'>" +
            "<tr><td align=center bgcolor='#ccccff'><h4>List Of Interviews Conducted </h4></td></tr>" +
            "<tr><td bgcolor='gray'><b>NO &nbsp; EMP NO.&nbsp;FIRST NAME&nbsp;LAST NAME&nbsp;RECORD RECEIVED ON &nbsp;&nbsp;INTERVIEW</b></td>" +
            "</tr>" + strVal + "</table>");

        return objStringWriter;
    }


STEP 3: Call the Download() function from the control's event you want

e.g.
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            Download();
            
        }
}


Hope this helps.

Let me know in case of any concern.
Smile | :)

Niladri Biswas

GeneralRe: Regrading Excel Generation. Pin
harish mehra2-Jul-09 21:22
harish mehra2-Jul-09 21:22 
Questionstop data saved twice when page is refreshed Pin
nitin_ion27-Jun-09 22:17
nitin_ion27-Jun-09 22:17 
AnswerRe: stop data saved twice when page is refreshed Pin
dan!sh 27-Jun-09 23:02
professional dan!sh 27-Jun-09 23:02 
AnswerRe: stop data saved twice when page is refreshed Pin
nitin_vatsus28-Jun-09 8:16
nitin_vatsus28-Jun-09 8:16 
AnswerRe: stop data saved twice when page is refreshed Pin
harish mehra6-Jul-09 19:26
harish mehra6-Jul-09 19:26 
QuestionCountry Drop Down List in Asp.net Pin
rajanji27-Jun-09 21:33
rajanji27-Jun-09 21:33 
AnswerRe: Country Drop Down List in Asp.net Pin
nitin_vatsus28-Jun-09 8:21
nitin_vatsus28-Jun-09 8:21 
QuestionJavascript regular expressions Pin
benams27-Jun-09 20:26
benams27-Jun-09 20:26 
AnswerRe: Javascript regular expressions Pin
Manas Bhardwaj28-Jun-09 8:33
professionalManas Bhardwaj28-Jun-09 8:33 
AnswerRe: Javascript regular expressions Pin
Niladri_Biswas28-Jun-09 18:13
Niladri_Biswas28-Jun-09 18:13 
QuestionWebClient method and browser giving different response Pin
Member 210280427-Jun-09 19:32
Member 210280427-Jun-09 19:32 
AnswerRe: WebClient method and browser giving different response Pin
nitin_vatsus28-Jun-09 8:25
nitin_vatsus28-Jun-09 8:25 
AnswerRe: WebClient method and browser giving different response Pin
Abhijit Jana28-Jun-09 8:36
professionalAbhijit Jana28-Jun-09 8:36 
GeneralRe: WebClient method and browser giving different response Pin
Member 210280428-Jun-09 16:19
Member 210280428-Jun-09 16:19 
QuestionHow to Use DataView.Select property Pin
meeram39527-Jun-09 19:27
meeram39527-Jun-09 19:27 
AnswerRe: How to Use DataView.Select property [modified] Pin
Niladri_Biswas1-Jul-09 1:39
Niladri_Biswas1-Jul-09 1:39 
GeneralRe: How to Use DataView.Select property Pin
meeram3952-Jul-09 1:06
meeram3952-Jul-09 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.