Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Regrading Excel Generation.

Regrading Excel Generation.

Scheduled Pinned Locked Moved ASP.NET
csharpasp-net
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harish mehra
    wrote on last edited by
    #1

    When i generate excel in asp.net it will generate but when i open & then saved this excel it showed "save as type" "web page".

    N C N 3 Replies Last reply
    0
    • H harish mehra

      When i generate excel in asp.net it will generate but when i open & then saved this excel it showed "save as type" "web page".

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      harish mehra wrote:

      when i open & then saved this excel it showed "save as type" "web page

      Yes. That's how web works. What is your question?

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • H harish mehra

        When i generate excel in asp.net it will generate but when i open & then saved this excel it showed "save as type" "web page".

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Hard to know exactly what you're doing from this gibberish ( it's not even a question ), but, I suspect you're failing to set the MIME type when you write your file down to the client.

        Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

        1 Reply Last reply
        0
        • H harish mehra

          When i generate excel in asp.net it will generate but when i open & then saved this excel it showed "save as type" "web page".

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          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
          
          H 1 Reply Last reply
          0
          • N Niladri_Biswas

            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
            
            H Offline
            H Offline
            harish mehra
            wrote on last edited by
            #5

            It is useful for generating excel but u can see when we open that excel and than save this excel it displayed save as type web page. can it display default save type as microsoft Excel?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups