简体   繁体   English

Excel无法在C#中打开

[英]Excel is not opening in C#

I tried opening excel but it doesn't work.... Can Anyone help me .... 我尝试打开excel,但无法正常工作。

        string path = Session["dir"].ToString() + "\\" + e.CommandArgument.ToString();
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

The code tries to activate a COM object. 该代码尝试激活COM对象。 One thing to check would be if the user that is configured for the application pool in IIS has Launch and Activation Permisions in the DCOM Config. 要检查的一件事是在IIS中为应用程序池配置的用户在DCOM Config中是否具有启动和激活权限。

Did you try to remove close and quit? 您是否尝试删除关闭并退出? If it is not helping try this: 如果没有帮助,请尝试以下操作:

// Start a new workbook in Excel.
        var excel = new Microsoft.Office.Interop.Excel.Application { Visible = true };
        var excelWorkBooks = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
        var workbookAdd = (Microsoft.Office.Interop.Excel._Workbook)excelWorkBooks.Add(); // XlWBATemplate.xlWBATWorksheet

        var worksheets = (Microsoft.Office.Interop.Excel.Sheets)workbookAdd.Worksheets;
        var sheet = (Microsoft.Office.Interop.Excel._Worksheet)worksheets.Item[1];
        sheet.Visible = XlSheetVisibility.xlSheetVisible;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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