5

I generate Excel files using XML programmatically. On trying then to open the file, I receive the following message:

"The file you are trying to open, 'MyFile.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"

When the user presses "Yes", the file opens without problems. But this warning and need to make an extra click is annoying. How could I get rid of it?

This is a part of my code:

        var stream = File.Create(path);
        var w = new XmlTextWriter(stream, null); // Creates the XML writer from pre-declared stream.

        //First Write the Excel Header
        w.WriteStartDocument();
        w.WriteProcessingInstruction("mso-application", "progid='Excel.Sheet'");

        w.WriteStartElement("Workbook");
        w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
        w.WriteAttributeString("xmlns", "o", null, "urn:schemas-microsoft-com:office:office");
        w.WriteAttributeString("xmlns", "x", null, "urn:schemas-microsoft-com:office:excel");
        w.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");
        w.WriteAttributeString("xmlns", "html", null, "http://www.w3.org/TR/REC-html40");

        w.WriteStartElement("DocumentProperties");
        w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:office");
        w.WriteEndElement();

        // Creates the workbook
        w.WriteStartElement("ExcelWorkbook");
        w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:excel");
        w.WriteEndElement();
halfer
  • 19,824
  • 17
  • 99
  • 186
David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • Have you tried `.xlsx`? – SQB Nov 15 '13 at 14:18
  • Do you need xls or xlsx file? – Alex Kovanev Nov 15 '13 at 14:20
  • Yes. On this I get another message: "Excel cannot open the file 'MyFile.xlsx' because the file format or file extension is not valid." And file is not opened. – David Shochet Nov 15 '13 at 14:23
  • @Shymep I need any of these, just without that warning. – David Shochet Nov 15 '13 at 14:23
  • 1
    _"When the user presses "Yes", the file opens without problems."_ - that it because Excel tries its best to show what you feed it. That doesn't mean it's a valid Excel file, and that is exactly your problem: you're just writing some tags that _resemble_ an Excel document, but it doesn't confirm to the specifications. Use an Excel library, they're built for this. – CodeCaster Nov 15 '13 at 14:24
  • `*.xlsx` files are zipped so how are you compressing your file/stream? I am not seeing it here. – RBarryYoung Nov 15 '13 at 14:28

2 Answers2

2

Instead of using an XmlTextWriter to create your spreadsheet, you'd probably have better luck using a library specifically written to create excel files. Otherwise, you're going to end up with issues like those that you are seeing - minor mistakes in things like xmlns attributes which cause your file to appear corrupted. A library specifically written to create xls files would already have solved this problem.

EPPlus seems to be well-supported: http://epplus.codeplex.com/

matt
  • 9,113
  • 3
  • 44
  • 46
-1

As you use XmlTextWriter you get a format different from .xls. What you create, yes, it's supported by Excel but every time you'll receive a warning message.
As @matt already mentioned it's much more easier to use a specific library for creating a file in a proper excel format. I would also advise to look at npoi library. It's pretty nice if you work with old .xls format.

Alex Kovanev
  • 1,858
  • 1
  • 16
  • 29