Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Monday, January 31, 2011

Exporting HTML Table in Excel

  protected void Button1_Click(object sender, EventArgs e)
    {
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=myexcel.xls");
        HttpContext.Current.Response.Charset = "";
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        table1.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
   }

Here is the code sample for table
     <table id="table1" runat="server">
        <tr><td></td></tr>
         ...
         ......
        <tr><td></td></tr>
   </table>
set  EnableEventValidation="false" to your page directive

ex :
   <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToExcel.aspx.cs" Inherits="ToExcel"     EnableEventValidation="false" %>

Put This Method Inside a page :
public override void VerifyRenderingInServerForm(Control control)
    {

    }

Saturday, January 1, 2011

Problem of asking to type username and passoword for http://localhost.

1. Open firefox and type in the address bar about:config
2. type ntlm in the textbox.
3. Double click on network.automatic-ntlm-auth.trusted-uris and type localhost there.
4. Click on OK.

You are done.



It work's only in Mozila.