【发布时间】:2022-02-05 05:05:35
【问题描述】:
我想将我的 aspx 页面上的 html 表导出到 MS WORD (.docx) 文件。 我已经有导出 .doc 文件的代码,但我想以 docx 扩展名导出。
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.openxmlformatsofficedocument.wordprocessingml.documet";
Response.AddHeader("Content-Disposition", "attachment; filename=WORK_ORDER.doc");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "";
EnableViewState = false;
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
xx.RenderControl(html);
Response.Write(writer);
Response.End();
}
【问题讨论】:
-
将文件名更改为 WORK_ORDER.docx ?此外,您拼错了内容类型。应该是
application/vnd.openxmlformats-officedocument.wordprocessingml.document -
已经使用了上面的 suugestion 但没有任何改变......我在下载 docx 格式的文件时仍然出错。
-
错误是什么?我尝试使用您的代码和文件名 WORK_ORDER.docx 导出表格,并且成功了。
-
尝试在您的 HTML 内容中添加一些图像 url(带有绝对路径),然后使用 doc 或 docx 扩展名导出文件,并检查图像是否已导出。因为在我的情况下,图像没有在 MS WORD 2007 中导出,但在 MS WORD 2003 中可见