以编程方式保存MSWord文档"另存为"对话框将文件类型显示为.htm而不是.doc

时间:2017-03-21 19:24:34

标签: c# asp.net export-to-word

我正在使用C#(ASP.Net)代码创建一个MS-Word文档。除了一件事,它工作正常。当用户点击"打开"浏览器上的按钮提示文件在MS Word中打开,标题可以看作" testList_Amit1.doc"。

enter image description here

enter image description here

但是,当用户点击"另存为" Ms Word应用程序功能区中的“文件”菜单中的按钮,"另存为"对话框显示"网页( .htm; .html)"在"另存为类型:"下拉菜单,而不是" .doc"。

enter image description here

以下是我正在使用的代码段。

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

0 个答案:

没有答案