HI

I want to read Excel file from C#,below I menation the coding I use,
I add Microsoft Excel 12.0 object library ,but Once I complie the coding it gave me error Meassage.The Error Meassage is
1."Type of Namespace for Application class Doesnt exsits"
2.Excel.Application.Workbook(object) is a method,which not given valid in the given context.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel;
namespace ExcelTest2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Excel.Application  xl = new Excel.ApplicationClass();

            Excel.Workbook wb = xl.Workbooks.Open("D:\\Test\\Test.xls", 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false, false, false);

        }
    }
}

Dani AI

Generated

Good catch by — the error here is a namespace/interop mismatch rather than a runtime bug in Excel. The root causes people hit are: (a) referencing the wrong namespace or an ambiguous using Excel;, and (b) trying to construct a type (ApplicationClass) that isn’t present in the generated interop for your Office version. Use the Excel interop types from the Microsoft.Office.Interop.Excel assembly (or fully qualify the Microsoft.Office.Interop.Excel namespace) and prefer the Application interface/type the PIA exposes.

Practical checklist

  • In Visual Studio: Project → Add Reference → COM → “Microsoft Excel x.x Object Library” (this creates the Microsoft.Office.Interop.Excel assembly).
  • Add using Microsoft.Office.Interop.Excel; (or fully qualify types). Remove any ambiguous using Excel;.
  • If Office on your machine is 32-bit, set Project → Build → Platform target = x86 to avoid class-not-registered errors.
  • Avoid Excel Interop on servers/Windows services/ASP.NET (Microsoft does not support automating Office on server). For server-side processing use Open XML, OLE DB/ACE, EPPlus/NPOI/ExcelDataReader, etc.

Safe cleanup pattern (release COM objects to avoid orphan Excel.exe processes)

// assume 'app' and 'wb' are your Application and Workbook objects
using System.Runtime.InteropServices;

// after finishing:
if (wb != null) { wb.Close(false); Marshal.ReleaseComObject(wb); wb = null; }
if (app != null) { app.Quit(); Marshal.ReleaseComObject(app); app = null; }
GC.Collect();
GC.WaitForPendingFinalizers();

If you still see “type or namespace … does not exist” check the actual reference name in References (it should be Microsoft.Office.Interop.Excel or the Excel COM entry), remove any conflicting non-PIA references, and rebuild. Thanks to for spotting the namespace issue and to for confirming the fix — the remaining work is just cleanup and the deployment considerations above.

Recommended Answers

All 4 Replies

Add using Microsoft.Office.Interop.Excel;

HI

I add that reference also but still error meassage is exists
."The type or namespace name 'ApplicationClass' does not exist in the namespace 'Excel' "

Thanks
Tank50

Application not ApplicationClass!
== Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();

HI
Great it works now,Thanks Ramy Mahrous.

Tank50

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.