Exporting grid to excel - asp.net

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

Related

Gridview to Excel Issue

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);

Export Excel from grid not working on Ajax Model PopUp

Hi i am trying to export grid data to excel but it is not working in IE8.I am using a Ajax Modal PopUp dialog box containing 2 buttons 'ok' and 'close'.On Ok click i want to download excel file.it works fine in Mozilla but in IE its not working.I am using below code. please suggest me how to do that?Also when i open file first it show warning before opening file how to handle that?
Response.Clear();
Response.Buffer = true;
string filename = "Checkout";
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".adt");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Sheet1</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
Response.Write("</head>");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView gv = new GridView();
gv.AutoGenerateColumns = true;
gv.DataSource = dt;
gv.DataBind();
gv.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
You get warning because tehnically speaking this is not export to excel, but you send a html with a wrong headers to trick browser to open this content with excel.
Better use native Excel library and generate real Excel file on the server, for example EPPlus, it's open source and doesn't require excel on server.
Here is the example of ashx handler that generates excel file from DataTable
https://stackoverflow.com/a/9569827/351383

Format column for exporting DataSet / Gridview to Excel

I'm exporting data from a DataSet to Excel via GridView. Framework is 2.0. I'm using the following code snippet:
Generate_Sequence_Data_BAL objAttBAL = new Generate_Sequence_Data_BAL();
string PlanId = "";
PlanId = Cryptography.Decrypt(Request.QueryString[0].ToString());
//Get the data from database into datatable
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SequenceData_" + Cryptography.Decrypt(Request.QueryString[0].ToString()) + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataSet ds = objAttBAL.GetData(Convert.ToInt32(PlanId));
GridView GridView1 = new GridView();
GridView1.RowDataBound += GridView1_RowDataBound;
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//Apply text style to each Row
GridView1.Rows[i].Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = #"<style>.textmode{mso-number-format:'\#'}</style>";
Response.Write(style);
Response.Write(sw.ToString());
Response.Flush();
GridView1.Dispose();
Response.End();
I have a column with data like '5E02'. Even after using the above method, it's being exported as '5.00E+02'.
I have tried concatenating (') to it while retrieving from SQL, but then it shows up like ('5E02) instead of (5E02).
Please help. Also suggest any other alternatives for exporting data directly to excel without gridview.
PS: I have already referred this question & it's not working, so it's not a duplicate.
I solved the problem by concatenating Alt+0160 (Non-breaking Space) at the end of the column.
Select <Column Name>+'<Alt+0160>'
Now the column is being exported as '5E02 ' instead of '5.00E+02'.

how to freeze header row while exporting excel sheet in asp.net

string attachment = "attachment; filename=Memberslist.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
completeGV.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(completeGV);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
I'm using the above code for exporting the excel sheet but i need to freeze the header row while exporting the excel sheet. please help me out... this is killing me...

Export Gridview with image to Excel - Problem

I have a Gridview in my ASP.NET C# Page. It has a couple of columns and also one image column. I can export the entire columns to an Excel file, but the image column is blank. By the way, I use the full path.
Any Idea?
Yes in default.aspx (For local Export)
ImageUrl="http://localhost:4056/CSharp/banner.gif"
instead of ImageUrl="~/banner.gif"
or (For remote Export)
http://your ip address here :port/foldername/filename
Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //make sure that the entire output is rendered simultaneously
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView2.GridLines = GridLines.Both;
GridView2.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();

Resources