Take the 2-minute tour ×

I'm writing a simple application which has Export to Excel functionality. Here is my code.

using  Microsoft.Office.Interop.Excel;

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Visible = true;

            Microsoft.Office.Interop.Excel._Worksheet worksht = app.ActiveSheet;

            worksht.Cells[1, "A"] = "Col1";
            worksht.Cells[1, "B"] = "Col2;
            worksht.Cells[1, "C"] = "Col3";

            int row = 1;
            lstUsers= GetUsers();

            foreach (Users usr in lstUsers)
                worksht.Cells[row, "A"] = usr.Col1;
                worksht.Cells[row, "B"] = usr.Col2;
                worksht.Cells[row, "C"] = usr.Col3;


This code works fine in my local box, but not in the dev server. My local box has microsoft office, the dev server doesn't have MS office.

I tried registering the Microsoft.Office.Interop.Excel.dll in GAC. But it didn't work. Any idea?

Dev Server Software Details: Windows 2003/IIS 6/ .Net 4.0

share|improve this question
Use a 3rd party library like NPOI for Excel 2003 or EPPlus for Excel 2007+ –  banging Aug 8 '13 at 18:00
Sorry, forgot to mention that 3rd party is not an option –  Yass Aug 8 '13 at 18:05
Server side office automation is not Supported by MS.Take a look here support.microsoft.com/kb/257757 –  Karthik Aug 8 '13 at 18:06
any specific reason you cant go for other #rd party? –  Karthik Aug 8 '13 at 18:07
Isn't Interop.Excel a 3rd party library (to .NET) anyway?? It's just slower, makes more problems, especially with memmory cleaning, and require Excel to be installed on the machine that's using it. Banging provided 2 good solutions, your own solution from below hasn't got much in common with real Excel file - it pretends Excel file, but compare its source in notepad to real excel. The same way you could hace just export to CSV. –  mj82 Aug 8 '13 at 21:29
show 4 more comments

2 Answers

Interop is NOT supported in sever-scenarios (like IIS) by MS.

AFAIK there is no option without any library to achieve what you want in a server-scenario!

There are many options to read/edit/create Excel files without Interop/installing Excel on the server:

I read in the comments that 3rd-party libraries are against company policy BUT I think since the OpenXML library is from MS itself AND free it might be an option

MS provides the free OpenXML SDK V 2.0 - see http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx (XLSX only)

This can read+write MS Office files (including Excel).

The other options are in case you get an exception from company policy and/or someone else comes looking for a solution to a similar problem

Another free option see http://www.codeproject.com/KB/office/OpenXML.aspx (XLSX only)

IF you need more like handling older Excel versions (like XLS, not only XLSX), rendering, creating PDFs, formulas etc. then there are different free and commercial libraries like ClosedXML (free, XLSX only), EPPlus (free, XLSX only), Aspose.Cells, SpreadsheetGear, LibXL and Flexcel etc.

share|improve this answer
Thank you @Yahia, appreciate your help. –  Yass Aug 8 '13 at 21:14
add comment
up vote 0 down vote accepted

Ok guys I found alternative way to do the same functionality which doesn't require any server side components. Here is the code.

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("Content-Disposition", "attachment;filename=UserList.xls");

        DataGrid grid = new DataGrid();
        grid.DataSource = <<DataList>>;

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);


It allows to export to XLS or XLSX depends on your need.

share|improve this answer
add comment

Your Answer


