我使用以下代码将html字符串输出到word文档中,扩展名为.docx [not .doc]
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一起使用,而在其他情况下它会破坏文档。
答案 0 :(得分:0)
也许您需要设置输出的内容编码。
当您使用UTF8时,以下情况应该有效:
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.contentencoding.aspx http://msdn.microsoft.com/en-us/library/system.text.encoding.utf8.aspx