the file format and extension don't match if excel attachment

telliot79
telliot79 used Ask the Experts™
on
Experts,

I'm sending an excel file as an attachment. the file is generated from an HTML table and when I open it, I receive a warning message stating: "the file format and extension don't match....."

here's my code:

string html = "<table><tr><td>Test</td></tr></table>";

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("application/vnd.ms-excel");
Attachment a = Attachment.CreateAttachmentFromString(html.ToString(), ct);
a.ContentDisposition.FileName = "Test.xls";
message.Attachments.Add(a);
client.Send(message);

Am I missing something in this snippet?

Thanks in advance for the help,

thanks inadvance for the help .
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
If I understand the problem properly, everything is right.

You attach a text (html) content but indicate that this content is MS Excel file. But this is not correct!
The code Attachment.CreateAttachmentFromString(html.ToString(), ct)  does not generate an excel file.
See https://msdn.microsoft.com/en-us/library/ms144617(v=vs.110).aspx
Of course when you are trying to open this text as an xls file it produces an error.

Author

Commented:
thanks anarki_jimbel.

The file does actually open and render correctly. So, what I really should have asked was can excel render html without giving this message. Which the answer appears to be - no. Or at least, I couldn't find a solution after a day of digging. I used openXML to generate the spreadsheet I want which works wonders (and only took me a couple of hours.)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial