Search code examples
gridviewdocx

output gridview1 to docx


Hi I tried to use iTextSharp on my hosted .net package but hit a brick wall due to security restrictions. I then decided to output to a word document instead which works however it only seems to allow me to export to .doc and not to .docx which is a bit of a problem. Also is there a way to make the document landscape?

here is the code that I found that works for .doc in portrait.

protected void ButtonWord_Click(object sender, EventArgs e)
{

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",

    "attachment;filename=GridViewExport.doc");

    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;
    GridView1.DataSource = Session["tblConversations"];
    this.GridView1.Columns[1].Visible = false;
    this.GridView1.Columns[2].Visible = false;
    this.GridView1.Columns[3].Visible = false;
    this.GridView1.Columns[0].Visible = false;
    GridView1.DataBind();

    GridView1.RenderControl(hw);

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

    Response.Flush();

    Response.End();

}

Solution

  • I found a solution here https://www.aspforums.net/Threads/889165/Export-Gridview-to-Word-in-Landscape-in-ASPNet/

    it does not export to docx , only doc though.

    protected void ButtonWord_Click(object sender, EventArgs e)
    {
    
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word ";
        Response.Write("<html>");
        Response.Write("<head>");
        Response.Write("<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>");
        Response.Write("<meta name=ProgId content=Word.Document>");
        Response.Write("<meta name=Generator content='Microsoft Word 9'>");
        Response.Write("<meta name=Originator content='Microsoft Word 9'>");
        Response.Write("<style>");
        Response.Write("@page Section1 {size:595.45pt 841.7pt; margin:1.0in 1.25in 1.0in 1.25in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}");
        Response.Write("div.Section1 {page:Section1;}");
        Response.Write("@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}");
        Response.Write("div.Section2 {page:Section2;}");
        Response.Write("</style>");
        Response.Write("</head>");
        Response.Write("<body>");
        Response.Write("<div class=Section2>");
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataSource = Session["tblConversations"];
        GridView1.DataBind();
        this.GridView1.Columns[1].Visible = false;
        this.GridView1.Columns[2].Visible = false;
        this.GridView1.Columns[3].Visible = false;
        this.GridView1.Columns[0].Visible = false;
        GridView1.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.Write("</div>");
        Response.Write("</body>");
        Response.Write("</html>");
        Response.Flush();
        Response.End();
    
    }