Esempio n. 1
0
    protected void Bexport_Click(object sender, EventArgs e)
    {
        //Clear the Response object
        Response.Clear();
        //set Response header to the Excel filename required (.xls for excel, .doc for word)
        Response.AddHeader("content-disposition", "attachment;filename=ReportOuput.doc");
        Response.Charset = "";
        // If you want the option to open the Excel
        // file without the save option then un-comment out the line below
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //FOR EXCEL
        //Response.ContentType = "application/vnd.xls";
        //FOR WORD
        Response.ContentType = "application/vnd.doc";
        //FOR PDF
        //Response.ContentType = "application/.pdf";
        //Declare new stringwriter and html writer
        StringWriter   stringWrite = new System.IO.StringWriter();
        HtmlTextWriter htmlWrite   = new HtmlTextWriter(stringWrite);

        //Strip out controls from FormView ?takes data too?
        PrepareFormViewForExport(FormView1);
        //render controls data as HTML
        FormView1.RenderControl(htmlWrite);
        //Clean out the Javascript postbacks etc.
        string html = stringWrite.ToString();

        html = Regex.Replace(html, "</?(a|A).*?>", "");
        StringReader reader = new StringReader(html);

        //Write xls document
        Response.Write(html);
        //end current Response
        HttpContext.Current.Response.End();
    }