C# 在以编程方式创建的ms word文件中,字符编码问题
我正在从包含土耳其字符集(çĞĜİıÍŞşÜü)的数据库中检索文本/html数据,并通过电子邮件以microsoft word格式发送此数据。使用ms word旧版本(小于2013年)和open office打开时,附件似乎正确,但在ms word 2013中,我上面指定的土耳其语字符似乎已损坏(阿拉伯语)。正如您在下面的html代码中所看到的,我添加了MetaCharachter集合UTF-8、ISO-8859-9和WINDOWS-1254,但没有什么新内容。同时,由于打开了旧的office版本,我将文件附加为.doc HtmlC# 在以编程方式创建的ms word文件中,字符编码问题,c#,asp.net,oracle,character-encoding,ms-word,C#,Asp.net,Oracle,Character Encoding,Ms Word,我正在从包含土耳其字符集(çĞĜİıÍŞşÜü)的数据库中检索文本/html数据,并通过电子邮件以microsoft word格式发送此数据。使用ms word旧版本(小于2013年)和open office打开时,附件似乎正确,但在ms word 2013中,我上面指定的土耳其语字符似乎已损坏(阿拉伯语)。正如您在下面的html代码中所看到的,我添加了MetaCharachter集合UTF-8、ISO-8859-9和WINDOWS-1254,但没有什么新内容。同时,由于打开了旧的office版
<!DOCTYPE html>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="content-type" content="text/html;charset=iso-8859-9" />
<meta http-equiv="content-type" content="text/html;charset=windows-1254" />
<title></title>
<!--[if gte mso 9]>
<xml><w:WordDocument><w:View>Print</w:View><w:Zoom>112</w:Zoom><w:DoNotOptimizeForBrowser/></w:WordDocument></xml>
<![endif]-->
</head>
<body lang=TR-TR style="tab-interval:.5in;font-family:Arial;">
<table>
<tr>
<td style="border-collapse:collapse;border:none;mso-yfti-tbllook:1184;font-size:10.0pt;font-family:'Cambria','Arial','sans-serif';text-align:left;">
ÇçĞğİıÖöŞşÜü
</td>
</tr>
</table>
</body>
</html>
你确定UTF8是正确的编码吗?对于turkisch字符集,Encoding.Unicode更好吗?byte[]byteArray=Encoding.UTF8.GetBytes(writer.ToString());很遗憾,它已损坏。能否将电子邮件保存到磁盘,以便知道问题所在。在你的html或邮件中
using(DbContext db = new DbContext())
{
StringBuilder sb = new StringBuilder(db.GetDocFile(id));
StringWriter writer = new StringWriter(sb);
HtmlTextWriter textWriter = new HtmlTextWriter(writer);
byte[] byteArray = Encoding.UTF8.GetBytes(writer.ToString());
MemoryStream stream = new MemoryStream(byteArray);
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("from@domain.com");
mail.To.Add("to@domain.com");
mail.Subject = "";
mail.Body = "";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(stream, "mydocfile.doc");
mail.Attachments.Add(attachment);
using (SmtpClient client = new SmtpClient("smtp.domain.com", 25))
{
client.Send(mail);
}
}
}