简体   繁体   中英

Excel is not opening in C#

I tried opening excel but it doesn't work.... Can Anyone help me ....

        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. 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.

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;

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