bmurrell30
bmurrell30

Reputation: 715

Trying to modify gridview layout during export to Word Doc

I'm trying to add a function to my code that will export a Gridview to a word document. For reference, here is the code I'm using to export the Gridview:

        Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.word";

        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        HtmlForm frm = new HtmlForm();
        GridView1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(GridView1);
        frm.RenderControl(htmlWrite);

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

For the record, I did not write this code, so if you want to know why something about it is a certain way, I don't know.

Anyway, there are three problem areas in this export that I need to address:

  1. The cells in the table export as only two characters wide; this naturally makes the table unreadable. I suspect the way to fix this involves using either "Gridview1.Styles.Add( ... )" or adding a Css Class to Gridview1, but my attempts at doing so have failed.

  2. On the website, the Gridview has sorting enabled. I'd like to keep these hyperlinks in the column headers from translating to the word document.

  3. On the website, the Gridview has "Edit" and "Delete" functions on each row (far left hand column, if it matters.) I would like to exclude this column from the word doc export.

Any help anyone can give with these issues would be much appreciated.

Upvotes: 0

Views: 709

Answers (1)

Nikita Silverstruk
Nikita Silverstruk

Reputation: 1117

    Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.word";

    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    HtmlForm frm = new HtmlForm();

    /*--------------*/
    GridView1.Columns(0).Visible = false; //HIDE EDIT
    GridView1.Columns(1).Visible = false; //HIDE DELETE
    GridView1.AllowSorting = false; //Disable Sorting
    GridView1.DataBind();
    /*--------------*/

    GridView1.Parent.Controls.Add(frm);
    frm.Attributes["runat"] = "server";
    frm.Controls.Add(GridView1);
    frm.RenderControl(htmlWrite);

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

I'm not quite sure what you are talking about in question 1 but maybe try disabling word wrapping on columns? appropriatecolumn.Wrap = false;

Upvotes: 0

Related Questions