Question

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;
            app.Workbooks.Add();

            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)
            {
                row++;
                worksht.Cells[row, "A"] = usr.Col1;
                worksht.Cells[row, "B"] = usr.Col2;
                worksht.Cells[row, "C"] = usr.Col3;
            }

            worksht.SaveAs("Test.xls");

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

Was it helpful?

Solution 2

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

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

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

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grid.RenderControl(htw);
        Response.Write(htw.InnerWriter);

        Response.End();

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top