在以编程方式创建的ms-word文件字符编码问题

时间:2015-11-05 08:14:30

标签: c# asp.net oracle character-encoding ms-word

我正在从包含土耳其字符集(ÇçĞğİıÖöŞşÜü)的数据库中检索text / html数据,并且我将此数据作为通过电子邮件附加的Microsoft Word格式发送。使用ms word旧版本(低于2013年)和开放式办公室打开时,附件文件似乎正常,但在ms字2013中,我在上面指定的土耳其字符似乎已损坏(阿拉伯语)。正如您在下面的html代码中看到的,我添加了元字符集UTF-8,ISO-8859-9和WINDOWS-1254,但没有什么新东西。与此同时,由于打开旧办公室版本,我将文件附加为.doc。

HTML

<!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>

C#

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

0 个答案:

没有答案