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. Export Datagrid to Excel using ASP.Net

Export Datagrid to Excel using ASP.Net

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netcomwindows-admin
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.
  • M Offline
    M Offline
    mullamootil
    wrote on last edited by
    #1

    I tried to export datagrid data to the excel. I got problem while exporting data to excel while working in IIS.It is showing COM file is missing.Please help me out Haripriya

    H S 2 Replies Last reply
    0
    • M mullamootil

      I tried to export datagrid data to the excel. I got problem while exporting data to excel while working in IIS.It is showing COM file is missing.Please help me out Haripriya

      H Offline
      H Offline
      HemJoshi
      wrote on last edited by
      #2

      Hi, Can you explain how you are exporting data to excel....It will help to get ur problem solve quickly.. If u want you can use the below code for exporting the data in excel..... private void btnExportToExcel_Click(object sender, System.EventArgs e) { dataGrid1.DataSource=TT_Reports.getDefaultersList(DateTime.Parse(TxtFromDate.Text),DateTime.Parse(TxtToDate.Text ),ddnTeamList.SelectedValue.ToString()); dataGrid1.DataBind(); Response.Clear(); Response.Buffer= true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); Response.AddHeader("content-disposition", "attachment;filename=DefaultersList_"+DateTime.Today.ToShortDateString()+".xls"); this.ClearControls(dataGrid1); dataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); } private void ClearControls(Control control) { for (int i=control.Controls.Count -1; i>=0; i--) { ClearControls(control.Controls[i]); } if (!(control is TableCell)) { if (control.GetType().GetProperty("SelectedItem") != null) { LiteralControl literal = new LiteralControl(); control.Parent.Controls.Add(literal); try { literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null); } catch { } control.Parent.Controls.Remove(control); } else if (control.GetType().GetProperty("Text") != null) { LiteralControl literal = new LiteralControl(); control.Parent.Controls.Add(literal); literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null); control.Parent.Controls.Remove(control); } } return; }

      M 1 Reply Last reply
      0
      • M mullamootil

        I tried to export datagrid data to the excel. I got problem while exporting data to excel while working in IIS.It is showing COM file is missing.Please help me out Haripriya

        S Offline
        S Offline
        Sneha Bisht
        wrote on last edited by
        #3

        Use This Code: protected void bttbexcel_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter stringWrite); GridView1.RenderControl(htmlWrite); this.EnableViewState = false; GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.Attributes["runat"] = "server"; Response.Write(stringWrite.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { }

        C 1 Reply Last reply
        0
        • S Sneha Bisht

          Use This Code: protected void bttbexcel_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter stringWrite); GridView1.RenderControl(htmlWrite); this.EnableViewState = false; GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.Attributes["runat"] = "server"; Response.Write(stringWrite.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { }

          C Offline
          C Offline
          calf2008
          wrote on last edited by
          #4

          Sneha Bisht wrote:

          Use This Code: protected void bttbexcel_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter stringWrite); GridView1.RenderControl(htmlWrite); this.EnableViewState = false; GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.Attributes["runat"] = "server"; Response.Write(stringWrite.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { }

          The Excel is not true excel.

          1 Reply Last reply
          0
          • H HemJoshi

            Hi, Can you explain how you are exporting data to excel....It will help to get ur problem solve quickly.. If u want you can use the below code for exporting the data in excel..... private void btnExportToExcel_Click(object sender, System.EventArgs e) { dataGrid1.DataSource=TT_Reports.getDefaultersList(DateTime.Parse(TxtFromDate.Text),DateTime.Parse(TxtToDate.Text ),ddnTeamList.SelectedValue.ToString()); dataGrid1.DataBind(); Response.Clear(); Response.Buffer= true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); Response.AddHeader("content-disposition", "attachment;filename=DefaultersList_"+DateTime.Today.ToShortDateString()+".xls"); this.ClearControls(dataGrid1); dataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); } private void ClearControls(Control control) { for (int i=control.Controls.Count -1; i>=0; i--) { ClearControls(control.Controls[i]); } if (!(control is TableCell)) { if (control.GetType().GetProperty("SelectedItem") != null) { LiteralControl literal = new LiteralControl(); control.Parent.Controls.Add(literal); try { literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null); } catch { } control.Parent.Controls.Remove(control); } else if (control.GetType().GetProperty("Text") != null) { LiteralControl literal = new LiteralControl(); control.Parent.Controls.Add(literal); literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null); control.Parent.Controls.Remove(control); } } return; }

            M Offline
            M Offline
            mullamootil
            wrote on last edited by
            #5

            Ya i used this code which u have pasted but it is not for multisheets of excel.Thats y i tried like this Dim obj_xl As New Excel.Application() Try obj_xl = GetObject(, "Excel.Application") Catch ex As Exception obj_xl = GetObject("", "Excel.Application") End Try obj_xl.Application.Visible = True obj_xl.Workbooks.Add() obj_xl.Sheets("Sheet1").Select() With obj_xl For i = 0 To dt.Rows.Count - 1 For j = 0 To dt.Columns.Count - 1 If IsDBNull(dt.Rows(i).Item(j)) Then .ActiveCell(i + 4, j + 1) = String.Empty Else .ActiveCell(i + 4, j + 1) = dt.Rows(i).Item(j) End If Next Next obj_xl.Dialogs(Excel.XlBuiltInDialog.xlDialogSaveAs).Show() End With This was the method i using to export values from data table. I used some COM components for excel This worked fine in my system.When i try to run in IIS its showing error as COM file missing

            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