System.Net.Mail Excel xml附件未作为excel文件打开

时间:2013-10-22 15:42:50

标签: c# wcf excel system.net.mail

我对System.Net.Mail有一个奇怪的用例,因为我不想在我的事务中的任何一点保存物理文件。

我有一个wcf网络服务,充当邮件的路由器。您提出了发送电子邮件的简单请求,并且工作正常。您也可以向它发送一个字符串文件路径,它将获取文件并正确发送。

什么是无效的是尝试获取有效的excel XML字符串的方法(我知道它是有效的,因为如果您使用文件编写器将其保存到文件,它会按预期打开),将其转换为字节然后将它发送到Web服务,该服务将创建一个附件并将其添加到MailMessage。当我获得附件时,它只是作为excel工作表打开,每行都是XML文档的一行。

这是当前的输出:

Row1: <?xml version="1.0"?>
Row2: <?mso-application progid="Excel.Sheet"?>
Row3: <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
Row4: xmlns:o="urn:schemas-microsoft-com:office:office"
Row5: xmlns:x="urn:schemas-microsoft-com:office:excel"
Row6: xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
Row7: xmlns:html="http://www.w3.org/TR/REC-html40">
Row8: <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">

当我在Notepad ++中打开文件时,我发现在行的末尾只有LF元素,而不是我通常所期望的LF和回车。

以下是我在客户端的代码:

byte[] data = new byte[reportXml.Length * sizeof(char)];
System.Buffer.BlockCopy(reportXml.ToCharArray(), 0, data, 0, data.Length);

var result = emailClient.SendMailfromBinaryData(to, from, cc, subject, body, data, "attachment.xlsx");

以下是WCF服务中的代码,它将附件添加到MailMessage:

 if (argFileStream != null && !string.IsNullOrEmpty(argFileName))
 {
     var stream = new MemoryStream(argFileStream);
     var attachment = new Attachment(stream, argFileName, "text/plain");
     attachment.NameEncoding = new UTF8Encoding();
     message.Attachments.Add(attachment);
 }

并非我也尝试将MIME类型更改为excel:

     var attachment = new Attachment(stream, argFileName, "application/vnd.ms-excel");

有什么想法吗?我没有收到任何错误,只是excel根本不会认识到XML实际上是一个excel文件为XML。

0 个答案:

没有答案
相关问题