Panel Export to Excel

Posted by Gouthamnila under ASP.NET on 8/13/2012 | Points: 10 | Views : 5868 | Status : [Member] | Replies : 5
I need to Export a panel in a page which is also containing two grid views.
But i need to Export a panel along with the controls which are present inside the panel.
So please give me a solution for this.

This is the codes that am using:

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

PanelPurchaseRequestFormat.RenderControl(hw);

Response.Write(sw.ToString());
Response.End();

While Using this i got this following Error:

Control 'ContentPlaceHolder1_txttextbox' of type 'TextBox' must be placed inside a form tag with runat=server.
Please let me if any one had suggestion.....




Responses

Posted by: Jayakumars on: 9/29/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

Control 'ContentPlaceHolder1_txttextbox' of type 'TextBox' must be placed inside a form tag with runat=server.

your textbox placed out of the form tag


this is wrong

<asp:TextBox ID="txt1" runat="server" ></asp:TextBox>
<form runat="server">
</form>


change like this

<form runat="server">
<asp:TextBox ID="txt1" runat="server" ></asp:TextBox>
</form>


Mark as Answer if its helpful to you

Gouthamnila, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/29/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Please mark as answer if it helpful to you.

Mark as Answer if its helpful to you

Gouthamnila, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rimi1289 on: 2/2/2013 [Member] Starter | Points: 25

Up
0
Down
Using "StringWriter" is not advisable, because of its restriction in encoding data into latest "Excel" versions. I'll suggest you to check the below link for exporting data to all versions of excel. It can also autoformat the excel sheet.

Rimi

Gouthamnila, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhanubysani on: 2/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Gouthamnila,

Please use the below code

attachment = "attachment; filename=" + fileName + ".xls"; //Setting the attachment name.
Response.ClearContent(); //clears all content output from the buffer stream.
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
using (HtmlForm frm = new HtmlForm())
{
this.pnl1.Parent.Controls.Add(frm);
frm.Controls.Add(this.pnl1);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

}
}

If u want to move css style sheet to the excel Please use this code

public void ExportPanelToExcel(Panel pnl, string fileName)
{
try
{
string attachment = "attachment; filename=" + fileName + ".xls"; //Setting the attachment name.
HttpContext.Current.Response.ClearContent(); //clears all content output from the buffer stream.
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
using (HtmlForm frm = new HtmlForm())
{
pnl.Parent.Controls.Add(frm);
frm.Controls.Add(pnl);
frm.RenderControl(htw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(Constants.Constants.Contstant_CssPath));
string s = sr.ReadToEnd();
HttpContext.Current.Response.Write("<HEAD><STYLE>");
HttpContext.Current.Response.Write(s.ToString());
HttpContext.Current.Response.Write("</STYLE></HEAD>");
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}

}
}
}
catch (Exception ex)
{
throw ex;
}

}

For writing to an Excel file on stream since this will give you the following message when you open the excel file.

It will prompt an error message click on yes.it will open the excel.

Some clients will not accept this solution.Please find the attached document best way do it.

Regards,
Bhanu
 Download source file

Regards,
Bhanu Prakash Bysani

Gouthamnila, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Megan00 on: 2/4/2013 [Member] Starter | Points: 25

Up
0
Down
You can try this excel api in codeplex: http://spreadsheet.codeplex.com/ , It can export textbox, log file, database data to excel, but I am not sure whether it must can help your problem. You can do more consult.

Never give up! Smile to the world!
http://excelcsharp.blog.com/

Gouthamnila, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response