2

I write this code for open an excel file:

    private readonly object _missing = Type.Missing;

    private IEnumerable<CellModel> GetUsedCells(string fileName)
    {
        SetNewCurrentCulture();//set "en-US" culture

        var application = new Application();
        var workbooks = application.Workbooks;
        Workbook workbook = null;

        try
        {
            workbook = workbooks
                .Open(fileName,
                      0, false, _missing, "", "",
                      true, XlPlatform.xlWindows, _missing, false, false,
                      _missing, false, false, XlCorruptLoad.xlNormalLoad
                );
        }
        finally
        {
            //realocate memory

            if(workbook != null)
            {
                ReleaseComObject(workbook);
                workbook = null;
            }

            workbooks.Close();
            ReleaseComObject(workbooks);
            workbooks = null;

            application.Quit();
            ReleaseComObject(application);
            application = null;

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            ResetCurrentCulture();//reset old culture
        }
    }

and in Open method this error has occured : "Exception from HRESULT: 0x800A03EC"

notes:

  • I use vs2012 and Target Framework of my project is 4 and version of Microsoft.Office.Interop.Excel is 14.0.0.0

  • Server info: Windows Server 2008 32-bit, IIS7, Excel 2010

  • I create Desktop folder in "C:\Windows\System32\config\systemprofile" and set permission to "Network Services" and "Users"

  • Change Permissions in DCOM Config in "Microsoft Excel Application" and select "Interactive User"

  • Set permission to "Network Services" and "Users" for "C:\Windows\Temp"

I'm very confused. help me please!

4

1 回答 1

3

Interop is NOT supported in sever-scenarios by MS.

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

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 all MS Office files (including Excel).

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.

于 2013-05-10T17:05:25.130 回答