HRESULT:0x800A03EC-创建Excel文件时,它在我的机器上有效 - c#

我正在使用来自asp.net应用程序中mssql服务器的数据创建Excel报告。这是我的方法:

[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;
    }

}

当我在vs 2010中打开源代码并按F5时,它在我的计算机和应用程序发布到的服务器上运行良好。但是,如果尝试通过IIS使用浏览器访问我的应用程序,则会收到HRESULT:0x800A03EC错误。

我尝试了以下命令:

appcmd set config -section:asp -enableParentPaths:true

我试图给我的文件夹写权限。

我试图通过组件服务更改我的MS Excel应用程序设置。

但是没办法!我无法正常工作。你有什么主意吗?我的配置有误吗?

提前致谢,

c#大神给出的解决方案

我转载了您的问题。.全部尝试:oWB.SaveAs / oWB._SaveAs / ( oXL.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet ).SaveAs / oWB.Application.ActiveWorkbook.SaveAs全部抛出异常:HRESULT:0x800A03EC ...

但是我看到您尝试使用:oWB.SaveCopyAs(strCurrentDir + strFile);
如果我接下来进行设置,它将起作用:

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

为什么不使用SaveCopyAs

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

将python scikit学习模型导出到pmml - python

我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…

提交表单后显示模式对话框 - php

提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…

DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java

以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…

在Map中,如果我们使用现有键进行修改,则不会获得ConcurrentModificationException - java

我有以下代码,我希望从情况2的情况下抛出ConcurrentModificationException,但它运行成功。据我所知,如果我对地图中的单个键执行相同的操作,则不会抛出异常,因为here但是当我重现这种具有两个案例的多个密钥的场景时,通过新密钥修改。通过现有密钥进行修改。情况1: Map<String,String> mp = new H…