Excel operation permissions in ASP. NET

Source: Internet
Author: User
Tags asp net
I recently opened a previously written webpage. Program When running, the Excel operation permission problem occurs, specifically in Code When you call the following statements related to Excel operations,

Application curexcelapp = new applicationclass ();

The system prompts that the permission is insufficient. The details are as follows:

Failed to retrieve components whose CLSID is {00024500-0000-0000-c000-000000000046} in the com class factory because of the following error: 80070005.

Note: An error occurred while executing the current Web request. Check the stack trace information for details about the error and the source of the error in the code. Exception details: system. unauthorizedaccessexception: failed to retrieve components whose CLSID is {00024500-0000-0000-c000-0000000000000046} in the com class factory because of the following error: 80070005. ASP. NET is not authorized to access the requested resources. Consider granting the ASP. NET Request identity the permission to access this resource. ASP. NET has a base process ID used when the application is not simulated (typically, {MACHINE} \ ASPnet on IIS 5 and Network Service on IIS 6 ). If the application is simulating through , the identity will be an anonymous user (usually IUSR_machinename) or an authenticated user. To grant ASP. NET access permissions to a file, right-click the file in resource manager, select "properties", and select the "Security" tab. Click Add to add an appropriate user or group. Highlight the ASP. NET account and select the corresponding access permission box. Source error:

Row 533: // create an Excel file
Line 534: Object missing = system. reflection. Missing. value;
Line 535: Application curexcelapp = new applicationclass ();
Line 536: curexcelapp. application. displayalerts = false;
Line 537: Workbook curworkbook = curexcelapp. workbooks. Add (xlwbatemplate. xlwbatworksheet );

 

I have encountered this problem because of the insufficient permissions of the Excel DCOM component. Therefore, follow the steps below:

1. Install the Excel software for office on the server;

2: Enter dcomcnfg.exe in "start"-> "run" to start "component service ";

3: Double-click "component service"> "computer"> "my computer"> "DCOM configuration ";

4. In "DCOM configuration", find "Microsoft Excel application", right-click it, and click "properties". The "Microsoft Excel application properties" dialog box is displayed;

5. Click the "ID" tab and select "Interactive User ";

6. Click the "Security" tab, click "Custom" on "Start and activate Permissions", and then click the corresponding "edit" button, in the pop-up "security" dialog box, add a "Network Service" user (Be sure to select the computer name) and grant it the "local start" and "Local activation" permissions. Add the ASPNET user to the XP system and the 2000 system;

7: the "Security" label remains. Click "Custom" on "Access Permissions", and then click "edit ", in the pop-up "security" dialog box, add a "Network Service" user and grant "local access" permission.

Note: Step 1 is required; otherwise, the following error occurs:

Microsoft Office Excel cannot open or save any documents due to insufficient memory or disk space. ? To get more available memory, close the workbooks or programs that are no longer in use. ? To release disk space, delete unnecessary files on the disk.

 

Note: An unhandled exception occurs during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code. Exception details: system. runtime. interopservices. comexception: the memory or disk space is insufficient. Microsoft Office Excel cannot open or save any documents again. ? To get more available memory, close the workbooks or programs that are no longer in use. ? To release disk space, delete unnecessary files on the disk. Source error:

Line 535: Application curexcelapp = new applicationclass ();
Line 536: curexcelapp. application. displayalerts = false;
Line 537: Workbook curworkbook = curexcelapp. workbooks. Add (xlwbatemplate. xlwbatworksheet );
Line 538: Worksheet curworksheet = (worksheet) curworkbook. Sheets. get_item (1 );
Row 539:

 

After setting the program according to the above steps, re-execute the program, and there is still a problem of insufficient permissions. I don't know where the problem is. I carefully analyzed the prompt information and code and finally found the problem, it was originally on the web. the following settings are made in config:

<Identity impersonate = "true"/>

The purpose of this section is to simulate the client for each request. According to the explanation in the prompt message, because username is not set, the currently executed user is IUSR_machinename, this user does not have the permission to operate the Excel component. to verify whether the analysis is correct, perform the following verification:

Delete this sentence and execute the program. Everything is normal;

Add the current login user to the Excel operation permission and perform the following settings: <identity impersonate = "true" username = "***" Password = "***"/>: Execute the program and everything works properly;

Add all users to the Excel operation permission and set as follows: <identity impersonate = "true"/>. If the program is executed, an error occurs.

Through the above verification, we can conclude that when the client is not simulated, the Asp.net program calls the Excel component to use the network service user (in XP and 2000, the user is an ASP net user. When the user sets <identity impersonate = "true"/> for client simulation, the IUSR_machinename user is used, but the user does not have the permission to call the Excel component, this does not work even if the user's related permissions are set; when you use <identity impersonate = "true" username = "***" Password = "***"/> to simulate a client, you only need to set the permissions of the relevant users, you can call the Excel component smoothly, but note that the user's password cannot be blank due to Asp.net restrictions.

When dealing with this problem, I found a useful document on the Internet and shared it with you as follows:

Solve the Problem of excel.exe process deployed on window Server 2003

After the operating process is finished, the execl.exe process will be retained every time. In any case, it cannot be closed, including garbage collection. I do not know the reason, but this problem does not exist in other server operating systems and XP operating systems.

To solve this problem, you can refer to the method of killing the process, but there are some risks. The specific method is as follows:

The operation process has certain risks. All operations must first have such permissions and add permission methods on the web. add permissions in config <identity impersonate = "true" username = "Logon Name" Password = "password"/>

Killing Processes

Publicstaticvoid killprocess (string processname)
{
System. Diagnostics. Process myproc = new system. Diagnostics. Process ();
// Obtain all opened Processes
Try
{
Foreach (process thisproc in process. getprocessesbyname (processname ))
{
If (! Thisproc. closemainwindow ())
{
If (thisproc! = NULL)
Thisproc. Kill ();
}
}
}
Catch (exception exc)
{
Throw exc;
}
}

 

After you complete the Excel operation, you can call the method to kill the process and the next call will be normal.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.