Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Saturday, July 11, 2009

Export Data in Word document from ASP.Net With Formating


Hello Frd's

It's easy task to Export Data in Word Document from asp.Net . look at below code.This Code Export Data in Word Document With Bold,Color ect ... Feature Whatever you do with HTML Table Data.



        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=Adfield.doc");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word ";


        //txtad.Value = str;
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        HtmlForm fr = new HtmlForm();
        HtmlTable TLB = new HtmlTable();
        TLB.Style.Add("Border-width", "1px");
        HtmlTableRow hdr = new HtmlTableRow();
        HtmlTableCell cell = new HtmlTableCell();
      
            // Take data From Database and Write with format


            hdr = new HtmlTableRow();
            cell = new HtmlTableCell();
            cell.Style.Add("BACKGROUND-COLOR", "Gainsboro");
            cell.InnerHtml = "Client Name";
            hdr.Cells.Add(cell);
            cell = new HtmlTableCell();
            cell.InnerHtml ="";
            hdr.Cells.Add(cell);
            TLB.Rows.Add(hdr);


     //At last


            fr.Controls.Add(TLBdetail);
           this.Controls.Add(fr);
           fr.RenderControl(hw);
          Response.Output.Write(sw.ToString());
          Response.Flush();
          Response.End();




0 comments:

Post a Comment