excel 中的错误= new Microsoft.Office.Interop.Excel.ApplicationClass();”当托管在服务器计算机上时..[dcomcnfg]

发布于 2024-11-29 22:55:43 字数 3391 浏览 1 评论 0原文

以下是在服务器 PC 上托管时引发错误的代码。

try
{
    //Variable Declarations
    Microsoft.Office.Interop.Excel._Workbook oWB;
    Microsoft.Office.Interop.Excel.Application oXL = null;

    Microsoft.Office.Interop.Excel.Workbook workbook;
    Microsoft.Office.Interop.Excel.Worksheet NwSheet = null;
    Microsoft.Office.Interop.Excel.Worksheet NwSheetSummary;
    Microsoft.Office.Interop.Excel.Worksheet NwSheetIndividualTotal;
    Microsoft.Office.Interop.Excel.Range ShtRange;
    Microsoft.Office.Interop.Excel.Sheets sheets;

    Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();

    excel.Workbooks.Add(System.Reflection.Missing.Value);

    excel.Quit();
    excel = null;

    //Opening Excel file(myData.xlsx)
    workbook = excel.Workbooks.Open(System.Web.HttpContext.Current.Server.MapPath("~/Template/Template.xlsx"), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    sheets = workbook.Worksheets;
    foreach (Worksheet ws in sheets)
    {
        if (ws.Index == 5)
            NwSheet = ws;
        if (ws.Index == 1)
            NwSheetSummary = ws;
        if (ws.Index == 4)
            NwSheetIndividualTotal = ws;
    }

    //Reading Excel file.
    //Creating datatable to read the containt of the Sheet in File.
    System.Data.DataTable dt = export.Tables[0];

    int rowCount = 8;

    if (dt.Columns.Contains("RowID"))
        dt.Columns.Remove("RowID");
    if (dt.Columns.Contains("Week"))
        dt.Columns.Remove("Week");
    if (dt.Columns.Contains("Year"))
        dt.Columns.Remove("Year");
    foreach (DataRow dr in dt.Rows)
    {

        rowCount += 1;
        for (int i = 1; i < dt.Columns.Count + 1; i++)
        {
            // Add the header the first time through 
            if (rowCount == 8)
            {
                //NwSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
            }
            NwSheet.Cells[rowCount, i] = dr[i - 1].ToString();
        }
    }

    for (int count = 0; count <= dt.Rows.Count; count++)
    {
        // NwSheet.get_Range("A9", Missing.Value).Value2 = dt.Rows[count]["SlNo"].ToString();


    }
    workbook.RefreshAll();
    String path = Convert.ToString(ConfigurationManager.AppSettings["DefaultPath"]);

    CreateFileOrFolder(workbook, week);
    NwSheet = null;
    ShtRange = null;
    workbook.Close(Missing.Value, Missing.Value, Missing.Value);
    workbook = null;
    oXL.Quit();
    excel.Quit();
    //appExl.Quit();
    status = true;
}

现在,它在具有 Interop Excel 12.0 的本地 PC 上运行良好,但在托管于具有相同 12.0 的服务器计算机上时则无法运行。

收到的错误是

由于以下错误,检索 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败:80070005。

在谷歌搜索错误后,我浏览了其他论坛,他们提到了 give dcomcnfg 中 Microsoft Excel 应用程序的网络服务权限。我也做了同样的事情,但没有用。

它没有成功,我得到了同样的错误。 喜欢 :- dcomcnfge 的配置

其中说:-

转到控制面板 -->管理工具-->组件服务-->计算机--> myComputer --> DCOM 配置 -->微软 Excel 应用程序。 -->右键单击以获取属性对话框。转到“安全”选项卡并相应地自定义权限。

我已经尝试给出所有可能的权利,但错误仍然存​​在。!!

请帮忙..

The following is the code which thows error when hosted in Server PC.

try
{
    //Variable Declarations
    Microsoft.Office.Interop.Excel._Workbook oWB;
    Microsoft.Office.Interop.Excel.Application oXL = null;

    Microsoft.Office.Interop.Excel.Workbook workbook;
    Microsoft.Office.Interop.Excel.Worksheet NwSheet = null;
    Microsoft.Office.Interop.Excel.Worksheet NwSheetSummary;
    Microsoft.Office.Interop.Excel.Worksheet NwSheetIndividualTotal;
    Microsoft.Office.Interop.Excel.Range ShtRange;
    Microsoft.Office.Interop.Excel.Sheets sheets;

    Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();

    excel.Workbooks.Add(System.Reflection.Missing.Value);

    excel.Quit();
    excel = null;

    //Opening Excel file(myData.xlsx)
    workbook = excel.Workbooks.Open(System.Web.HttpContext.Current.Server.MapPath("~/Template/Template.xlsx"), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    sheets = workbook.Worksheets;
    foreach (Worksheet ws in sheets)
    {
        if (ws.Index == 5)
            NwSheet = ws;
        if (ws.Index == 1)
            NwSheetSummary = ws;
        if (ws.Index == 4)
            NwSheetIndividualTotal = ws;
    }

    //Reading Excel file.
    //Creating datatable to read the containt of the Sheet in File.
    System.Data.DataTable dt = export.Tables[0];

    int rowCount = 8;

    if (dt.Columns.Contains("RowID"))
        dt.Columns.Remove("RowID");
    if (dt.Columns.Contains("Week"))
        dt.Columns.Remove("Week");
    if (dt.Columns.Contains("Year"))
        dt.Columns.Remove("Year");
    foreach (DataRow dr in dt.Rows)
    {

        rowCount += 1;
        for (int i = 1; i < dt.Columns.Count + 1; i++)
        {
            // Add the header the first time through 
            if (rowCount == 8)
            {
                //NwSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
            }
            NwSheet.Cells[rowCount, i] = dr[i - 1].ToString();
        }
    }

    for (int count = 0; count <= dt.Rows.Count; count++)
    {
        // NwSheet.get_Range("A9", Missing.Value).Value2 = dt.Rows[count]["SlNo"].ToString();


    }
    workbook.RefreshAll();
    String path = Convert.ToString(ConfigurationManager.AppSettings["DefaultPath"]);

    CreateFileOrFolder(workbook, week);
    NwSheet = null;
    ShtRange = null;
    workbook.Close(Missing.Value, Missing.Value, Missing.Value);
    workbook = null;
    oXL.Quit();
    excel.Quit();
    //appExl.Quit();
    status = true;
}

Now it run's fine in my local PC which has Interop Excel 12.0 but not when hosted in server machine which has same 12.0.

the error received is

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

After googling out for the error I had gone through other forums and they had mentioned to give rights to Network Services to Microsoft Excel Application in dcomcnfg. I did the same thing but its of no use.

It didnt worked out and I got the same error.
Like :-
Configuration of dcomcnfge

Which Say's:-

Goto Control-Panel --> Administrative tools-->Component Services -->computers --> myComputer -->DCOM Config --> Microsoft Excel Application. --> Right click to get properties dialog. Goto Security tab and customize permissions accordingly.

I have tried giving all possible right's but still the error persist's.!!

Please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文