我正在从我的 C# (ASP.Net) 代码创建一个 MS-Word 文档。它工作正常,除了一件事。当用户点击浏览器提示中的“打开”按钮时,文件会在 MS Word 中打开,并且标题可以看作是“testList_Amit1.doc”。
但是,当用户从 Ms Word 应用程序功能区的文件菜单中单击“另存为”按钮时,“另存为”对话框在“另存为类型:”中显示“网页 ( .htm; .html)”下拉菜单,而不是“.doc”。
下面是我为此使用的代码片段。
HttpContext.Current.Response.Clear();
//File name for the exported word document
string strFileName = filename + ".doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.ContentType = "application/vndopenxmlformats-officedocument.wordprocessingml.document";
HttpContext.Current.Response.Charset = "UTF-16";
StringBuilder strHTMLContent = new StringBuilder();
String wordHeader = "<html xmlns:o='urn:schemas-microsoft-com:office:office' ";
wordHeader += "xmlns:w='urn:schemas-microsoft-com:office:word' ";
wordHeader += "xmlns='http://www.w3.org/TR/REC-html40'> ";
wordHeader += "<head><title>" + filename + "</title>";
wordHeader += "<!--[if gte mso 9]><xml><w:WordDocument><w:View>Normal</w:View><w:Zoom>100</w:Zoom>";
wordHeader += "<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->";
wordHeader += "<style> @page Section1 {size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; ";
wordHeader += "margin:1.00in 0.75in 1.00in 0.75in; ";
wordHeader += "mso-paper-source:0;} ";
wordHeader += "div.Section1 {page:Section1;} p.MsoFooter, li.MsoFooter, ";
wordHeader += "div.MsoFooter{margin:0in; margin-bottom:.0001pt; ";
wordHeader += "mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; ";
wordHeader += "font-size:8.0pt; } ";
wordHeader += "table.tableHeader {margin:0in; font-size:12.0pt; } ";
wordHeader += "table.tableData {margin:0in; font-size:10.0pt; width:100%; } --></style></head>";
strHTMLContent.Append(wordHeader);
strHTMLContent.Append("<body><div class=Section1>");
.
.
.
.
strHTMLContent.Append("</div></body></html>");
HttpContext.Current.Response.Write(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();