tags:

views:

2731

answers:

4

I've found ExcelPackage, a better library than Excel Interop API to create and mantain programatically excel sheets, but they are generated in .xlsx. Most of people that will see the files have only office 2003 installed, so I need to convert, in my C# code, the final result into a .xls file.

Do you know any way to do it in C# code?

** UPDATE I'm trying to use SaveAs method, but it doesn't work, it just doesn't do anything, or return the error 0x800A03EC .

+1  A: 

You can try to use Microsoft.Office.Interop.Excel. You will need to have Excel installed on the machine that is trying to do the conversion. You can add the reference from the COM tab and use the Microsoft Excel 12.0 Object Library component.

Basically, you will open up the existing workbook using Workbook.Open(), create a new Worksheet and copy over the existing data. You can then use the Workbook.SaveAs() method, this will let you set the file format in the 2nd parameter.

Scott Lance
This however requires you to have Excel installed on the machine you execute this on - this might not be the case on a e.g. web server. An easy solution - but not really workable in an enterprise setting.
marc_s
+3  A: 

I suspect this won't be a popular answer, but I don't believe that its desirable to convert the files to .xls from .xlsx (I was going to suggest that it wasn't necessary, but, unfortunately, that's a generalisation too far).

The "Microsoft Office Compatibility Pack" is free to download and adds support for the new formats to Office XP and Office 2003 - far better therefore, at least in the general instance, to persuade your users to bring their systems up to spec than to mire yourself in having to deal with office interop (which is basically going to cause you, and quite possibly your users, a lot of pain). Similarly I believe there is support for the new formats in Open Office 3.

I do appreciate that there are circumstances where people will not be allowed to add this capability to their system but for the most part adding the tools as above will make people's lives easier since it will reduce the friction between those using Office 2007 and those using older versions.

Murph
Yes, I gave up from converting by now, because of this compatibility pack I didn't know before, thanks!
Victor Rodrigues
A: 

Try this code:

        try
        {
            Microsoft.Office.Interop.Word.ApplicationClass oWord = new ApplicationClass();
            object oMissing = Type.Missing;
            object fileName = @"c:\test.docx";
            Document oDoc = oWord.Application.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            object fileName2 = @"c:\test2.doc";

            object fileFormat = WdSaveFormat.wdFormatDocument97;
            oDoc.SaveAs(ref fileName2, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            oWord = null;
            Console.WriteLine("Done");

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        Console.Read();
foson
Your code compile OK, but it raises runtime error. Maybe it`s because of I`m running it on the IIS (I have ASP.NET application). line number 4. OS my server is running is WinServ 2008 64bit
chester89
foson
A: 

hi i need to convert Excel 2007 files to Excel2003 format. So can anybody provide VBScript for that

kumar