我想将 GridView 中的数据输入到 Word 文档中并从客户端下载它

发布于 2025-01-01 04:44:02 字数 1366 浏览 1 评论 0原文

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();
}

错误(换行):

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>
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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2025-01-08 04:44:02

试试这个:

Response.ContentType = "application/octet-stream"

Try this:

Response.ContentType = "application/octet-stream"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文