Configuration excel=新建Microsoft.Office.Interop.excel.ApplicationClass()时出错&引用;当托管在服务器计算机中时..[dcomcnfg]

Configuration excel=新建Microsoft.Office.Interop.excel.ApplicationClass()时出错&引用;当托管在服务器计算机中时..[dcomcnfg],configuration,dcom,Configuration,Dcom,以下是在服务器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.Exce

以下是在服务器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;
}
试试看
{
//变量声明
Microsoft.Office.Interop.Excel.\u工作簿oWB;
Microsoft.Office.Interop.Excel.Application oXL=null;
Microsoft.Office.Interop.Excel.Workbook工作簿;
Microsoft.Office.Interop.Excel.Worksheet NwSheet=null;
Microsoft.Office.Interop.Excel.Worksheet摘要;
Microsoft.Office.Interop.Excel.Worksheet NWSheet IndividualTotal;
Microsoft.Office.Interop.Excel.Range ShtRange;
Microsoft.Office.Interop.Excel.Sheets;
Microsoft.Office.Interop.Excel.ApplicationClass Excel=新的Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Add(System.Reflection.Missing.Value);
excel.Quit();
excel=null;
//打开Excel文件(myData.xlsx)
工作簿=excel.Workbooks.Open(System.Web.HttpContext.Current.Server.MapPath(“~/Template/Template.xlsx”),缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value,缺少.Value);
工作表=工作簿。工作表;
foreach(工作表ws-in-sheets)
{
如果(ws.Index==5)
NwSheet=ws;
如果(ws.Index==1)
NwSheetSummary=ws;
如果(ws.Index==4)
NwSheetIndividualTotal=ws;
}
//读取Excel文件。
//创建datatable以读取文件中工作表的内容。
System.Data.DataTable dt=export.Tables[0];
int rowCount=8;
if(dt.Columns.Contains(“RowID”))
dt.Columns.Remove(“RowID”);
if(dt.Columns.Contains(“周”))
dt.列。删除(“周”);
如果(dt列包含(“年”))
dt.列。删除(“年”);
foreach(数据行dr在dt.行中)
{
行计数+=1;
对于(int i=1;i组件服务-->计算机-->我的计算机-->DCOM配置-->Microsoft Excel应用程序。-->右键单击以获取属性对话框。转到“安全”选项卡并相应地自定义权限

我已经试着给出了所有可能的正确答案,但错误依然存在

请提供帮助。

。如果您试图从一项服务中实现自动化,您可能会走向一堵砖墙……这不仅仅是权限问题,作为一项服务运行时,您没有交互式桌面,Excel也不喜欢。例如,关于此主题,这里也有类似的问题。