Gridview to Excel Issue - asp.net

I have a chunk of code which extracts data from Grid View into excel file. But instead of extracting the grid view data it is copying entire data present in the html view into my excel as below:
How can I get only my Grid View data into excel?
My excel generation code is below. I don't use Response.End() as it throws ThreadAbortException.
protected void btnExcelGenerator_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridViewRefurb.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Help is appreciated, thanks in advance

Do some changes
1) ASPX Page : Gridview within Panel
<asp:panel id = "panel_export" runat = "server" >
<asp:Gridview id = "Gridview" runat = "server">
</Gridview>
</asp:Panel>
2) ON Excel Export render panel instead of Gridview
panel_export.RenderControl(htmlWrite);

Related

How to remove a Gridview from an ASPX page?

I have an Aspx page that displays a GridView. The gridview would appear with data that was depending on which catergory was selected from a drop down menu. The user has the option to then download this as a CSV file (export to CSV). I now want the Gridview not to appear (beacuse it is so large it often just hangs) but instead to be able to download a CSV file with the data from the gridview. I have successfully built the button allowing for the download of this data by clicking the button. However, I now cannot open the page without the gridView appearing. I tried to comment out the Gridview code - this just broke the page. I tried to set the gridview as Visible="False" but this did not work either. What else can I do to prevent the gridview from appearing and the user to be taken straight to a download dialogue box?
wrap your Gridview to a div and set it to display:none in asp.net code –
Ex: <div id="divhidegrid" runat="server" style="display:none;" > <asp:GridView ID="gvtest" runat="server" > </div> like this
You can load your data in DataSet or DataTable from Code Behind and then export it directly to Excel/CSV without assigning it to GridView such as in the following example to export it to Excel:
Public Sub ExportToExcel(dt As DataTable)
If dt.Rows.Count > 0 Then
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim dgGrid As New DataGrid()
dgGrid.DataSource = dt
dgGrid.DataBind()
'Get the HTML for the control.
dgGrid.RenderControl(hw)
'Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=Data.xls")
Me.EnableViewState = False
Response.Write(tw.ToString())
Response.[End]()
End If
End Sub
If you get errors like control must be placed in inside of form tag you may also be required put the following code in your backend (Reference: export-data-to-excel-from-datatable-gridview-aspnet-csharp)
Public Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
C# Version of above code:
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0) {
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=Data.xls");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
public void VerifyRenderingInServerForm(Control control)
{
// Verifies that the control is rendered
}

Export sqldatasource to excel not just list or gridview

I'm trying to export my listview, but not just the visible listview, I want to export the entire contents of the sqldatasource. The query returns 20 columns, only a small set (5) are displayed on the listview. Is there a way i can export the entire 20 columns to excel file?
I have this so far:
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.EnableViewState = false;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
//I want to chagne this:
this.ResultsListView.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
instead of this line:
this.ResultsListView.RenderControl(htmlTextWriter);
I would like to do something like:
this.sqldatasource.RenderControl(htmlTextWriter);
But obviously SqlDataSource doesn't have a RenderControl method. Is there a clean way of going about this?
You can create a DataTable from the DataView returned by the SqlDataSource and export that to Excel. The resulting DataTable will contain all the columns.
Example:
DataTable dt = ((DataView) this.sqldatasource.Select(DataSourceSelectArguments.Empty)).ToTable();
Now take this DataTable and export that. Google export DataTable to Excel.
If you are fine with using third-party libraries, look into EPPLUS. You can export a DataTable to Excel in, literally, 2 lines of code. The rest would be the code to set the headers and flush the Response.
Here's an example: http://epplus.codeplex.com/wikipage?title=WebapplicationExample
I prefer to use CSV rather than excel for plain data, but the new version of Excel does have the advantage of compression.
You can use the OpenXMLSDK to directly export to XLSX or the FileHelpers library to export to CSV.

export gridview to Excel : making changes to gv before it its rendered to control

Im using the following code in order to export a gridview to excel
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
HtmlForm frm = new HtmlForm();
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
EnableViewState = false;
Controls.Add(frm);
frm.Controls.Add(this.myGridView);
frm.RenderControl(hw);
Source for above exporting code
Everything works like a charm.
Except I now have to make changes to the gridview before it gets exported to Excel.
E.g. One change is that I needed to remove a column.
Yet I cant do this:
//I can't remove columns here, since it has not been rendered yet and has 0 columns
frm.Controls.Add(this.myGridView);
frm.RenderControl(hw);
Is there a way to edit the Gridview before it is exported? e.g .Remove a column

Export pie chart to powerpoint using c#

i am trying to Export a pie chart to powerpoint slide uisng c# but not found any similar article which can help me...
please provide me some example or provide some useful link which can help me in achieving this functionality easily...
thanks,
Aman
we are using hipdf library but this is paid and we are converting html to image first and then showing this image on ppt slide. You can also do it by using inkscape exe file
In button_click event write following code.
GridView1.AllowPaging = false; //write this code only if paging is enabled.
GridView1.DataBind(); //write this code only if paging is enabled.
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.ppt");//for text file write FileName.txt
Response.Charset = "";
// If you want the option to open the Excel file without saving than comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ppt";\\for text file write vnd.txt
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
GridView1.AllowPaging = true; //write this code only if paging is enabled.
GridView1.DataBind();//write this code only if paging is enabled.
You will have to override function of VerifyRenderingInServerForm like this
public override void VerifyRenderingInServerForm(Control control)
{
//Keep it empty
}
And to avoid exception you have to set property of
<%# Page Language="C#" EnableEventValidation="false" %>

Exporting grid to excel

I used this code for exploring grid to excel.I got this code from net.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
When I am using this code,grid data get display in excel,but that does not have vertical and horizontal lines.It shows gridview column name as link not normal text.I have not used any theme to gridview and columns are autogenerated.One column say col3 contain more description so it's data get displayed at top when second row start,still it's discription shown in cell2.So I wanted to add vertical and horizontal lines,in that.So it is easy to come to know which data of belong to which id.I want to format col 1 also which is of number format only and decimal places zero.
1 col1 col2 col3
hjhhjhjhjhjhjhjhjhjhjbbv
gfgfgfuiuiuiuiui
2 9.12E+11 gkjj etwtrtrwqtrtttwwtretqwfe
dear member
gffgf
3 565E+11 hghgh
Bind grid data on page load event and then try to export again.. follow this link's code.. may be it will be good rather than others...
Exporting GridView Data to Excel
Code Snippet:
private void ExportGridView()
{
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Note: There are some comments also posted which also have some information..
To Format GridView according to grid controls when you are exporting data to excel then follow the below link:
ASP.Net 2.0: Export GridView to Excel

Resources