简体   繁体   中英

HRESULT: 0x800A03EC - It works on my machine issue when creating Excel files

I am creating an excel report using data coming from mssql server in my asp.net application. Here is my method:

[WebMethod]
public static string ExportToExcel(string sourcetype)
{
    Microsoft.Office.Interop.Excel.Application oXL;
    Microsoft.Office.Interop.Excel._Workbook oWB;
    Microsoft.Office.Interop.Excel._Worksheet oSheet;
    Microsoft.Office.Interop.Excel.Range oRng;

    try
    {
        oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = false;

        oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

        List<ExcelReport> dataToExport = APIClient.GetExcelReportData(Utility.getCurrentFilterId(), sourcetype);

        oSheet.Cells[1, 1] = "Source";
        oSheet.Cells[1, 2] = "UserName";
        oSheet.Cells[1, 3] = "Name";
        oSheet.Cells[1, 4] = "Message";
        oSheet.Cells[1, 5] = "Title";
        //oSheet.Cells[1, 6] = "Date";

        int activeRow = 2;

        for (int i = 0; i < dataToExport.Count; i++)
        {
            oSheet.Cells[activeRow, 1] = dataToExport[i].Source;
            oSheet.Cells[activeRow, 2] = dataToExport[i].UserName;
            oSheet.Cells[activeRow, 3] = dataToExport[i].Name;
            oSheet.Cells[activeRow, 4] = dataToExport[i].Message;
            oSheet.Cells[activeRow, 5] = dataToExport[i].MessageTitle;
            //oSheet.Cells[activeRow, 6] = dataToExport[i].EntityDate;

            activeRow++;
        }

        oSheet.get_Range("A1", "Z1").Font.Bold = true;
        oSheet.get_Range("A1", "Z1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
        oRng = oSheet.get_Range("A1", "Z1");
        oRng.EntireColumn.AutoFit();
        oXL.Visible = false;
        oXL.UserControl = false;

        string strFile = "report" + System.DateTime.Now.Ticks.ToString() + ".xls";
        string strCurrentDir = HttpContext.Current.Server.MapPath(".") + "\\ExcelReports\\";
        oWB.SaveAs(strCurrentDir + strFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, null, null, false,
            false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);
        //oWB.SaveCopyAs(strCurrentDir + strFile);
        oWB.Close(null, null, null);
        oXL.Workbooks.Close();
        oXL.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oRng);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
        oSheet = null;
        oWB = null;
        oXL = null;
        GC.Collect();  // force final cleanup!

        //errLabel.Text = "<A href=http://" + strMachineName + "/ExcelGen/" + strFile + ">Download Report</a>";
        //string result = "<a href=\"~/ExcelReports/" + strFile + ">Raporu İndir</a>";
        string result = "ExcelReports/" + strFile;
        return result;
    }
    catch (Exception theException)
    {
        String errorMessage;
        errorMessage = "Error: ";
        errorMessage = String.Concat(errorMessage, theException.Message);
        errorMessage = String.Concat(errorMessage, " Line: ");
        errorMessage = String.Concat(errorMessage, theException.Source);

        return errorMessage;
    }

}

It works fine in my machine and at the server that application published to when I opened the source code in vs 2010 and press F5. But if I try to access my application using the browser through IIS, I'm getting HRESULT: 0x800A03EC error.

I tried the following command:

appcmd set config -section:asp -enableParentPaths:true

I tried to give write permissions to my folder.

I tried to change my MS Excel application settings from Component Services.

But no way! I could not get it working. Do you have any idea? Am I doing a mistake in configuration?

Thanks in advance,

I reproduced your issue.. tried all: oWB.SaveAs / oWB._SaveAs / ( oXL.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet ).SaveAs / oWB.Application.ActiveWorkbook.SaveAs all throw exception: HRESULT: 0x800A03EC...

But I see you tried with: oWB.SaveCopyAs(strCurrentDir + strFile); and it works if I set next:

oWB.Saved = true;
oWB.SaveCopyAs( strCurrentDir + strFile );

Why you don't use SaveCopyAs ?

If the call to Excel interop is happening on the same thread as ASP.NET -- ie, in response to your UI -- then the Excel needs some sort of userprofile to work. IIS doesn't provide this by default, but as long as some folders exist with the correct permissions then it can work.

  • C:\\Windows\\SysWOW64\\config\\systemprofile\\Desktop (for 64-bit Servers only)
  • C:\\Windows\\System32\\config\\systemprofile\\Desktop (for both 32-bit and 64-bit Servers)

From the final section of https://www.ryadel.com/en/office-interop-dcom-config-windows-server-iis-word-excel-access-asp-net-c-sharp/#0x800A03EC_Cannot_access_the_file

Make a "Desktop" folder like below

C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\Desktop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM