Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
Hello Friends

I am trying to read and write excel file with c#. For this I included the Interop.Excel.dll in my project.

I have installed MS Excel in my local machine. This app is working fine there.
But when I put this app at my server, where MS Excel is not installed, this is not generating the file or may be even not reading.

I am confused although I am using dll in my project, does it require MS Excel to be installed there?

I am getting the following exception
===================START=====================
10/27/2012 7:48:52 PM
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at ImpExpExcelFile.Form1.generateExcel(String loginId, String fileName, DataSet newValues)
====================END======================

My Code
private DataSet readExcelFile(string path)
        {
            string excelConn = @"Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + path + "; Extended Properties=Excel 8.0;";
            OleDbConnection oleConn = new OleDbConnection(excelConn);
            DataSet xlsDs = new DataSet();
            try
            {
                if (!string.IsNullOrEmpty(path))
                {
                    string strSQL = "SELECT * FROM [Sheet1$]";
                    OleDbDataAdapter oleDa = new OleDbDataAdapter(strSQL, oleConn);
                    oleDa.Fill(xlsDs);
                }
            }
            catch (Exception ex)
            {
                handleException(ex);
            }
            finally
            {
                oleConn.Close();
                oleConn.Dispose();
            }
            return xlsDs;
            
        }
private bool generateExcel(String loginId, String fileName, DataSet newValues)
{
bool retVal = false;
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            string filePath = @"E:\";
                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                int j = 1;
                for (int i = 0; i < newValues.Tables[0].Rows.Count; i++)
                {
                    xlWorkSheet.Cells[j, 1] = newValues.Tables[0].Rows[i][0].ToString();
                    xlWorkSheet.Cells[j, 2] = newValues.Tables[0].Rows[i][1].ToString();
                    j++;
                }
                if (File.Exists(filePath + fileName))
                {
                    File.Delete(filePath + fileName);
                }
                xlWorkBook.SaveAs(filePath + fileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
                retVal = true;

}
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
Posted

Hi.
I think you can't install it on Server. You need donwload and install: Microsoft Office 2010: Primary Interop Assemblies Redistributable ( You chose Office Version ).

http://www.microsoft.com/en-gb/download/details.aspx?id=3508[^]
 
Share this answer
 
v2
Comments
PurryJoh 4-May-17 3:59am    
You need to install MS Office, or use an alternative, for instance see how to read Excel file in C#.
As an alternative you can use Linqtoexcel[^]. This does not require Excel to be installed. It is based on the default available database drivers on the machine. Here[^] you find good information to help you implementing it.

success
Piet
 
Share this answer
 
Comments
Manish Kumar Namdev 29-Oct-12 5:27am    
Thanks pietvredeveld :)
Yes MS Excel should be installed on the particular computer where your using this application.
 
Share this answer
 
Comments
Manish Kumar Namdev 29-Oct-12 2:20am    
Thanks Anandkumar D. But I have referenced the dll so still do i need to install?
Anandkumar D 29-Oct-12 2:47am    
Yes you need to install it, other wise you have to take the dlls which are used by excel and manually need to be registered.
Hi ,

I instaled above link in my local michine , still same error plz replay me
 
Share this answer
 
Comments
CHill60 31-May-14 7:23am    
If you have a question to ask then post your own question using the "ask a question" link. Don't post as a solution

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