Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
 private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
 private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
 private static Microsoft.Office.Interop.Excel.Application oXL;
 public void ReadExistingExcel()
 {
    // string path = @"F:\\tempExcel.xls";
     string fileName = "../Admin/Excel/tempExcel.xls";
     string path = Server.MapPath(fileName);

     oXL = new Microsoft.Office.Interop.Excel.Application();
     oXL.Visible = true;
     oXL.DisplayAlerts = false;
     mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
     //Get all the sheets in the workbook
     mWorkSheets = mWorkBook.Worksheets;
     //Get the allready exists sheet
     mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
     Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
     int colCount = range.Columns.Count;
     int rowCount = range.Rows.Count;
     for (int index = 1; index < 15; index++)
     {
         mWSheet1.Cells[rowCount + index, 1] = rowCount + index;
         mWSheet1.Cells[rowCount + index, 2] = "New Item" + index;
     }

 }


I have used this code in my web application in asp.net. For that i include an dll file microsoft.office.interop.excel.dll. It is working fine in local . But when hosting online it shows error.Please help me..

What I have tried:

Once i removed the following line from web config
<add assembly="stdole, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A">
the site is working but cant read and write on excel
Posted
Updated 11-Mar-16 0:20am

You've forgotten to tell us what the error is. The most likely culprit would be error 80040154, which means that Office is not installed on the server.

You should read the following Microsoft knowledgebase article:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

There are various ways to read and create Excel spreadsheets on the server without using Office interop. For example:
 
Share this answer
 
When you deploy an word/excel (automation dll) on server it needs an extra permission on server to run COM component
I have some work around that you can try out
Is it II6 or 7 you get error ? please explain with error code and exception message so that I will help you more
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900