Question

I have a dataset that I have modified into an xml document and then used a xsl sheet to transform into an Excel xml format in order to allow the data to be opened programatically from my application. I have run into two problems with this:

  1. Excel is not the default Windows application to open Excel files, therefore when Program.Start("xmlfilename.xml") is run, IE is opened and the XML file is not very readable.

  2. If you rename the file to .xlsx, you receive a warning, "This is not an excel file, do you wish to continue". This is not ideal for customers.

Ideally, I would like Windows to open the file in Excel without modifying the default OS setting for opening Excel files. Office interop is a possibility, but seems like a little overkill for this application. Does anyone have any ideas to make this work?

The solution is in .Net/C#, but I am open to other possibilities to create a clean solution.

Was it helpful?

Solution

Process.Start(@"C:\Program Files\Microsoft Office\Officexx\excel.exe", "yourfile.xml");

That being said, you will still get the message box. I suppose that you could use the Interop, but I am not sure how well it will work for you.

OTHER TIPS

If you insert the following into the 2nd line of your XML it directs Windows to open with Excel

<?mso-application progid="Excel.Sheet"?>

What if you save the file as an xlsx, the extension for XML-Excel?

As Sam mentioned, the xlsx file extension is probably a good route to go. However, there is more involved than just saving the xml file as xlsx. An xlsx is actually a zip file with a bunch of xml files inside folders. I found some good sample code here which seems to give some good explanations although I haven't personally given it a try.

Apologies in advance for plugging a third party library, and I know it's not free, but I use FlexCel Studio from TMS Software. If you're looking to do more than just dump data (formatting, dynamic cross-tabs, etc) it works very well. We generate hundreds of reports a week using it.

FlexCel accepts strongly-typed datasets, it can group data according to relationships, and the generated Excel file looks so much cleaner than what you can get from a Crystal Reports excel export. I've done the crystal reports thing, and the OLE automation thing. FlexCel is a steal at $125 EU.

Hope this helps.

OpenXML in MSDN - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.openxml(v=office.11).aspx

using Excel = Microsoft.Office.Interop.Excel;

string workbookPath= @"C:\temp\Results_2013Apr02_110133_6692.xml";

            this.lblResultFile.Text = string.Format(@" File:{0}",workbookPath);
            if (File.Exists(workbookPath))
            {
                Excel.Application excelApp = new Excel.Application();
                excelApp.Visible = true;
                Excel.Workbook excelWorkbook = excelApp.Workbooks.OpenXML(workbookPath, Type.Missing, Excel.XlXmlLoadOption.xlXmlLoadPromptUser);
            }
            else
            {
                MessageBox.Show(String.Format("File:{0} does not exists", workbookPath));
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top