Click here to Skip to main content
14,641,793 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 4

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[^]
   
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#.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   
Comments
Manish Kumar Namdev 29-Oct-12 5:27am
   
Thanks pietvredeveld :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

Yes MS Excel should be installed on the particular computer where your using this application.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 5

Hi ,

I instaled above link in my local michine , still same error plz replay me
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100