locked
permission issues in opening a excel file RRS feed

  • Question

  • User2031860760 posted

    hello everyone i m stuck with some code.....

    i am attaching the code.......when i m running by vs2010 its running fine but when i am trying to by IIS in window 7....its giving me exception..... the bold line giving the exception which i am metioning below:-

    Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). 

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

    To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

    Source Error: 

    Line 23:             string Path = @"E:\hrd\attendence.xls";
    Line 24:             // Initialize the Excel Application class
    Line 25: Excel.ApplicationClass app = new Excel.ApplicationClass(); Line 26:             // Create the workbook object by opening the excel file.
    Line 27:             Excel.Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);


    Source File: c:\inetpub\wwwroot\cdacb.in\html\hrd\ChangeExcelForHRD.aspx.cs    Line: 25 

    Stack Trace: 

    [UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).]
       ChangeExcelForHRD.submit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\cdacb.in\html\hrd\ChangeExcelForHRD.aspx.cs:25
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
    
    FileUpload1.SaveAs("C://hrd//attendence.xls");
          
                string Path = @"E:\hrd\attendence.xls";
                // Initialize the Excel Application class
                Excel.ApplicationClass app = new Excel.ApplicationClass();
                // Create the workbook object by opening the excel file.
                Excel.Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                // Get the active worksheet using sheet name or active sheet
                Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
    
                // This row,column index should be changed as per your need.
                // i.e. which cell in the excel you are interesting to read.
                int row = 1;
                int col = 1;
    
                int userow = 1;
                int usecol = 1;
                
                
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet = null;
                object misValue = System.Reflection.Missing.Value;
    
                xlApp = new Excel.ApplicationClass();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
    
                Excel.Range selectedRange = (Excel.Range)xlApp.Selection;
    
                selectedRange.Columns.AutoFit();
    
                foreach (Excel.Range column in selectedRange.Columns)
                {
                    column.ColumnWidth = (double)column.ColumnWidth + 5;
                }
                Stopwatch timer = new Stopwatch();
                timer.Start();
                string start_month = ((Excel.Range)workSheet.Cells[3,10]).Value2.ToString();
                string end_month = ((Excel.Range)workSheet.Cells[3, 13]).Value2.ToString();
                string printed_on = ((Excel.Range)workSheet.Cells[4, 19]).Value2.ToString();
                string emp_id = ((Excel.Range)workSheet.Cells[8, 3]).Value2.ToString();
                string emp_name = ((Excel.Range)workSheet.Cells[8, 7]).Value2.ToString();
    
                while (row<=39)
                {
                    
                    
                    col= 2 ;
                    usecol = 2;
                    while (col <= 19)
                    {
                        try
                        {
                            // Read the Cells to get the required value.
                            if (row == 2)
                            {
                                xlWorkSheet.Cells[row, col] = "                                         Daily Attendence system(Detailed Summary Report)";
                                xlWorkSheet.get_Range("b2","s2").Merge();
    
                            }
                            if (row >= 9 && row <= 38)
                            { 
                                switch(col)
                                {
                                    case 2:
                                    case 3:
                                    case 4:
                                    case 12:
                                    case 14:
                                    case 15:
                                    case 17:
                                        string firstName = ((Excel.Range)workSheet.Cells[row, col]).Value2.ToString();
                                        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                                        
                                        xlWorkSheet.Cells[row, usecol] = firstName;
                                        
                                        usecol++;
                                        break ;
                                }
                            }
                            col++;
                        }
                            
                        catch (Exception ex)
                        {
                            col++;
                            // xlWorkSheet.Cells[row, col] = "aaaa";
                        }
                    }
                    row++;
                }
    
                selectedRange = xlWorkSheet.get_Range("b9", "b9");
                selectedRange.ColumnWidth = 12;
    
                selectedRange = xlWorkSheet.get_Range("g9", "g9");
                selectedRange.ColumnWidth = 10;
    
                selectedRange = xlWorkSheet.get_Range("e8", "e8");
                selectedRange.ColumnWidth = 10;
    
                selectedRange = xlWorkSheet.get_Range("f8", "f8");
                selectedRange.ColumnWidth = 10;
    
                xlWorkSheet.get_Range("a2", "s2").Merge(false);
                selectedRange = xlWorkSheet.get_Range("a2", "s2");
                selectedRange.Font.Bold = "true";
                selectedRange.Font.Size = "16";
                //selectedRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
               // xlWorkSheet.Range["a2"].Text = "Center";
                xlWorkSheet.Cells[2, 1] = "                      Daily Attendance Report (Detailed Summary Report)";
    
    
    
                selectedRange = xlWorkSheet.get_Range("D3", "D3");
                selectedRange.ColumnWidth = 13;
    
                xlWorkSheet.Cells[3, 4] = start_month;
                xlWorkSheet.Cells[3, 5] = " "+end_month;
    
                xlWorkSheet.Cells[5, 2] = "Company:";
                xlWorkSheet.Cells[5, 3] = "C-DAC";
    
                xlWorkSheet.Cells[6, 2] = "Department:";
                xlWorkSheet.Cells[6, 3] = "HRD";
    
                xlWorkSheet.Cells[8, 2] = "Emp ID:";
                xlWorkSheet.Cells[8, 3] = emp_id;
    
                xlWorkSheet.Cells[8, 5] = "Emp Name:";
                xlWorkSheet.Cells[8, 6] = emp_name;
    
                selectedRange = xlWorkSheet.get_Range("b9", "b38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
    
                selectedRange = xlWorkSheet.get_Range("c9", "b38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
    
                selectedRange = xlWorkSheet.get_Range("d9", "b38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
    
                selectedRange = xlWorkSheet.get_Range("e9", "b38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
    
                selectedRange = xlWorkSheet.get_Range("f9", "b38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
    
                selectedRange = xlWorkSheet.get_Range("g9", "b38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
    
                selectedRange = xlWorkSheet.get_Range("b9", "h38");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));
                
    
                selectedRange = xlWorkSheet.get_Range("b9", "h9");
                selectedRange.BorderAround(Excel.XlLineStyle.xlContinuous,
                Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick,
                Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(133, 132, 233)));           
                
    
                timer.Stop();
                string address_to_save_file = @"C:\HRD\" + emp_name + emp_id + "";
                
                xlWorkBook.SaveAs(address_to_save_file, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
    
                workBook.Close(true, misValue, misValue);
                app.Quit();

    Friday, April 27, 2012 5:45 AM

Answers

  • User-356869594 posted

    try to give the  E:\hrd\attendence.xls  files permission  network service and IIS_IUSRS and IUSRS

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 10, 2012 4:47 AM