0

I'm having issues with saving an excel spreadsheet when running on the server, when running locally, it works beautifully!

Has anyone ever come across this before or can point me in the right direction?

The app pool is running under the 'LocalSystem' identity.

Thanks!

I keep getting the following error/ stack trace:

Error saving the excel sheet: 
Exception from HRESULT: 0x800A03EC   at     Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, 
Object     Password, Object WriteResPassword, Object ReadOnlyRecommended, Object 
CreateBackup,     XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object 
AddToMru, Object     TextCodepage, Object TextVisualLayout, Object Local)
   at MiFiveDayReport.ExportToExcel.SaveAs(String filepath, XlFileFormat type) in
G:\Development\MI Development\Technical     Development\MIPortal\MIPortal\Old_App_Code\ExportToExcel.cs:line 411

Here is the code I use to try and save the excel doc:

try
        {
            if (log.IsInfoEnabled) log.Info("Saving...");
            if (string.IsNullOrEmpty(filepath))
                //Sets the filepath as the default
                excelBook.SaveAs(this.filepath, type);
            else
                excelBook.SaveAs(filepath, type);
            if (log.IsInfoEnabled) log.Info("Saved!");
            success = true;
        }
        catch (Exception ex)
        {
            //SendEmail e = new SendEmail("Error saving excel sheet: " + ex.Message);
            //SendEmail e1 = new SendEmail("Error saving excel sheet: " + ex.StackTrace);
            if (log.IsErrorEnabled) log.Error("Error saving the excel sheet: "
                                     + ex.Message
                                     + ex.StackTrace);
            throw;
        }

Updated New Error:

Error creating excel application: Retrieving the COM class factory for component with
CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 
80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, 
Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at MiFiveDayReport.ExportToExcel..ctor(String filepath) in G:\Development\MI Development\Technical Development\MIPortal\MIPortal\Old_App_Code\ExportToExcel.cs:line 94

On the following line:

try
        {
            if (log.IsInfoEnabled) log.Info("Creating an excel application...");
            excelApp = new Application();
            if (log.IsInfoEnabled) log.Info("Excel application created");
        }
        catch (Exception ex)
        {
            if (log.IsErrorEnabled) log.Error("Error creating excel application: " + ex.Message + ex.StackTrace);
            throw;
        }
donpisci
  • 259
  • 3
  • 8
  • 23
  • It sounds like a permissions issue. Make sure the user has rights to the folder where you're trying to save the Excel document. – jfrankcarr Jun 14 '13 at 10:50
  • @jfrankcarr do you mean the user accessing the website? – donpisci Jun 14 '13 at 10:53
  • The IIS user account has to have permissions to write the file. See http://stackoverflow.com/questions/5729264/what-are-all-the-user-accounts-for-iis-asp-net-and-how-do-they-differ – jfrankcarr Jun 14 '13 at 11:01
  • Thanks for that- I've added IIS_IUSRS and given it full control over the folder. I'm now getting another error which I've added to the main question – donpisci Jun 14 '13 at 13:37
  • Did you also give the IIS_IUSRS account permission to run Excel? – jfrankcarr Jun 14 '13 at 17:29

2 Answers2

2

1) start -> run -> mmc -32 -> press enter new window will open 2) file-> add remove snap in-> from avilable snap ins select component services -> press add> button 3) expand component services-> expand computers -> expand mycomputer -> expand DCOM config -> right click on Microsoft Excel application -> properties -> security tab 4) in launch and activation permission section -> click on customize radio button and then click on edit button 5) click add button -> add your user and provide all the access to your user and press ok button (service account with you are executing your app)

after this please create the below mention folder

If we are running in Windows Server 2008 64-bit/R2, · Navigate to C:\Windows\SysWOW64\config\systemprofile folder and create the following directory “Desktop” If we are running in Windows Server 32-bit · Navigate to C:\Windows\System32\config\systemprofile\Desktop folder and create the following directory “Desktop”

This will solve this issue.

Raj
  • 351
  • 2
  • 13
  • 30
0

I have gone to hell and back with Microsoft.Office.Interop.Excel.Workbook and I found that the fix to not use the C:\ drive on my AWS windows system but the D:\ drive. Something about the how the C: drive was mounted was breaking the Microsoft.Office.Interop.Excel.Workbook SaveAs2 function call. I tried a lot of other common fixes found online but this was the only thing that worked in my case.