Asp.net 输出word文档作为docx扩展,仅在firefox中工作
我正在使用以下代码将html字符串输出到扩展名为.docx[not.doc]的word文档中Asp.net 输出word文档作为docx扩展,仅在firefox中工作,asp.net,ms-word,httpcontext,Asp.net,Ms Word,Httpcontext,我正在使用以下代码将html字符串输出到扩展名为.docx[not.doc]的word文档中 private void ExportBodyAsDoc(string strBody) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "Application/msword"; HttpContext.Current.Response.AddHeader("C
private void ExportBodyAsDoc(string strBody) {
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "Application/msword";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
var repo = new System.IO.MemoryStream();
var stringBytes = System.Text.Encoding.UTF8.GetBytes(strBody);
repo.Write(stringBytes, 0, strBody.Length);
HttpContext.Current.Response.BinaryWrite(repo.ToArray());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
}
它只与firefox一起工作,而在另一种情况下,它会破坏文档。也许您需要设置输出的内容编码 鉴于您正在使用UTF8,以下各项应该可以工作:
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;