Access to Excel files on IIS is difficult along the journey and is set up in the following steps to work properly in IIS!
1. References and codes:
1). Add the Excel assembly reference in the project (Note: select "Microsoft Excel 1*.0 Object Library" from the System COM component):
2). Set Excel assembly properties: Select the Excel assembly that has been added Microsoft.Office.Interop.Excel, right-click Properties, Copy Local to True, Embed Interop Types to Fa Lse.
3). Excel Access Code:
1 PrivateIcollection<sample> Readexcel (stringfileName)2 {3Application Excel =NewApplicationClass {Visible =false, DisplayAlerts =false };4Workbook Workbook =NULL;5Worksheet sheet =NULL;6 7 Try8 {9Workbook =Excel. Workbooks.Open (fileName);TenSheet =(Worksheet) workbook. ActiveSheet; One A if(Sheet = =NULL) - { - Throw NewException ("Read Excel file failed!"); the } - -Range range =NULL; - intRowIndex =1; + -icollection<sample> list =NewHashset<sample>(); + for(RowIndex =2; RowIndex <= Sheet. UsedRange.Rows.Count; rowindex++) A { at if(! This. HasValue (sheet, rowIndex)) - { - Break; - } - - intColindex =1; in varSample =NewSample (); -Range = (range) sheet. Cells[rowindex, colindex++]; toSample. Name =range. Value;
... other code logic + list. ADD (sample); - } the returnlist; * } $ Catch(Exception ex)Panax Notoginseng { - ThrowEx. Getinnerexception (); the } + finally A { the if(Workbook! =NULL) + { -Workbook. Close (false); $ System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook); $ System.Runtime.InteropServices.Marshal.ReleaseComObject (sheet); - } -Workbook =NULL; the Excel. Workbooks.close (); - Excel. Quit ();Wuyi System.Runtime.InteropServices.Marshal.ReleaseComObject (Excel); theExcel =NULL; - GC. Collect (); Wu GC. WaitForPendingFinalizers (); - File.delete (fileName); About } $}
4. IIS Settings:
1). After the site is published, select authentication, Anonymous authentication, Edit, select Application pool identity, OK.
2). IIS Application Pool Check current application, Advanced Settings, Identity select Network Service-OK
3. COM Build Settings:
1). Start--〉 run--〉cmd 2) command prompt below, enter mmc-32, open 32 of Console 3). In the File menu or right-click the root node, add the Delete snap-in--〉 component Service 4). Locate Microsoft Excel Applica in DCOM configuration tion ", right-click on it, then click on" Properties ", pop Up" Microsoft Excel Application Properties "dialog box 5). Click on the" Identity "tab and select" Interactive User "(the Interactive users) 6 Click on the "Security" tab, click "Customize (Customize)" on "Launch and Activation permissions (Launch and Activition perimissions)" and click on the corresponding "edit" button, in the popup " Security dialog box, add a "NETWORK SERVICE" User (note to select the computer name) and give it the "local start" and "Local Activation" Permissions 7). The Security tab remains in the access perimissions) "Click on" Customize (Customize) ", then click" Edit ", in the pop-up" security "dialog box also add a" NETWORK SERVICE "user, and then give" Local access "permissions. 4. Restart IIS, test through
Questions about Excel access in IIS