I want to enter data from a GridView into a Word document and have it downloaded from client

StackOverflow https://stackoverflow.com/questions/9080363

  •  21-04-2021
  •  | 
  •  

質問

var gd = new GridView
{
    DataSource = li,
    AutoGenerateColumns = true,
};

gd.DataBind();

if (gd.Rows.Count > 0)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + "List" + DateTime.Now.ToString("dd.MM.yyyy") + ".doc");
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
    HttpContext.Current.Response.ContentType = "application/ms-word";
    HttpContext.Current.Response.Charset = "UTF-8";
    HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());

    var oStringWriter = new System.IO.StringWriter();
    var oHtmlTextWriter = new HtmlTextWriter(oStringWriter);

    gd.RenderControl(oHtmlTextWriter);
    HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
}

ERROR (lines wrapped):

Sys.WebForms.PageRequestManagerParserErrorException: 
Sys.WebForms.PageRequestManagerParserErrorException:
The message received from the server could not be parsed.' when calling method:
[nsIDOMEventListener::handleEvent]
[Break On This Error]   

Filtered chrome url chrome://firebug/content/net/spy.js

spy.js (line 807)
<System>
役に立ちましたか?

解決

Try this:

Response.ContentType = "application/octet-stream"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top