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