11 Replies - 15356 Views - Last Post: 10 June 2011 - 03:51 AM Rate Topic: -----

#1 daya88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-June 11

c# import Excel Files - error references

Posted 06 June 2011 - 10:34 AM

Hello

I have a small problem. I want to import Excel files using C# but when i declare a variable like this xlsApp = new Excel.ApplicationClass() I receive this error : The type or namespace name 'ApplicationClass' does not exist in the namespace 'Excel' (are you missing an assembly reference?.

I added the following references : Microsoft Excel 12.0 Object Library, Microsoft Excel 5.0 Object Library and Microsoft.Office.Interlop.Excel and it stil dosen`t work.

This is my code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using Excel;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PC_Management3
{
    public partial class BrowseExcel : Form
    {
        public BrowseExcel()
        {
            InitializeComponent();
        }

       
        private void button1_Click(object sender, EventArgs e)
        {
            //open file

            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.InitialDirectory = @"d:\";
            fdlg.Filter = "All files (*.xls)|*.xls|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fdlg.FileName;
            }

            //excel stuff
            Excel.Application xlsApp;
            Excel.Workbook xlsWorkbook;
            Excel.Worksheets xlsWorksheet;

            xlsApp = new Excel.ApplicationClass();


        }

        
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: c# import Excel Files - error references

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: c# import Excel Files - error references

Posted 06 June 2011 - 10:46 AM

Ok, part of your problem is that the namespace Excel doesn't exist. There's Microsoft.Office.Interop.Excel, but not one just called Excel.

Now, if you're using .NET 4 (Visual Studio 2010 or C# Express 2010), I suggest following this guide for office interop.

Otherwise, use this one.
Was This Post Helpful? 0
  • +
  • -

#3 daya88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-June 11

Re: c# import Excel Files - error references

Posted 06 June 2011 - 10:47 AM

Thanks a looot. I will try this as soon as I can.
Was This Post Helpful? 0
  • +
  • -

#4 daya88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-June 11

Re: c# import Excel Files - error references

Posted 07 June 2011 - 05:03 AM

Thank you for your reply. I read that article but it stil dosen`t work. I already added that reference. And when I use using Excel = Microsoft.Office.Interop.Excel; I receive this errors :

1. Namespace '<global namespace>' contains a definition conflicting with alias 'Excel'[/b] - 4 times for the following lines

Excel.Application xlsApp;
            Excel.Workbook xlsWorkbook;
            Excel.Worksheets xlsWorksheet;

            xlsApp = new Excel.ApplicationClass();


2. Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead. - for this line :

xlsApp = new Excel.ApplicationClass();



And if I write like this :

using Microsoft.Office.Interop.Excel;

Microsoft.Office.Interop.Excel.Application xlsApp;
Microsoft.Office.Interop.Excel.Workbook xlsWorkbook;
Microsoft.Office.Interop.Excel.Worksheets xlsWorksheet;

xlsApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlsApp.Quit();



I get this error :

Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.

This post has been edited by Sergio Tapia: 07 June 2011 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: c# import Excel Files - error references

Posted 07 June 2011 - 05:41 AM

From now on please:

:code:

That way we can easily copy and paste your code to help you debug your problems. :)
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: c# import Excel Files - error references

Posted 07 June 2011 - 06:17 AM

Why are you using ApplicationClass? The tutorials tell you to use Application.
Was This Post Helpful? 0
  • +
  • -

#7 daya88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-June 11

Re: c# import Excel Files - error references

Posted 07 June 2011 - 06:20 AM

I found in many tutorials ApplicationClass. Also, if i write only xlsApp = new Excel.Application(); I receive another error :
Cannot create an instance of the abstract class or interface 'Excel.Application'
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: c# import Excel Files - error references

Posted 07 June 2011 - 06:33 AM

Well, you've got something different going on. I have no idea what else you've done in your project. I suggest starting a new project (one that's completely clean), and then following one of the two tutorials I've linked. Which one to follow depends on the version of .NET you're using.

Other than that, I can't say.
Was This Post Helpful? 0
  • +
  • -

#9 daya88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-June 11

Re: c# import Excel Files - error references

Posted 07 June 2011 - 06:37 AM

Thank you for your help. I`m at my 3rd project already and it`s the same problem. I`m using .NET Framework 4. I made all the steps in that article. And I used this projects on 2 computers. It`s probably a small thing and I can`t detect it.

Thank you again.
Was This Post Helpful? 0
  • +
  • -

#10 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: c# import Excel Files - error references

Posted 09 June 2011 - 11:52 AM

Try this code, worked good on my Excel 2007 (student release),
.NET Framework 3.5
perhaps in the higher version it is needs to use another syntax though...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.Globalization;
using System.Runtime.InteropServices;
namespace PC_Management3
{
    public partial class BrowseExcel : Form
    {
        private OpenFileDialog openFileDialog1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        CultureInfo oldcult = System.Threading.Thread.CurrentThread.CurrentCulture;
        object missing = Type.Missing;
        public BrowseExcel()
        {
            InitializeComponent();
        }

       
 

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(295, 40);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(367, 22);
            this.textBox1.TabIndex = 1;
            // 
            // BrowseExcel
            // 
            this.ClientSize = new System.Drawing.Size(398, 81);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "BrowseExcel";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.BrowseExcel_FormClosing);
            this.Load += new System.EventHandler(this.BrowseExcel_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //open file

            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.InitialDirectory = @"d:\";
            fdlg.Filter = "All files (*.xls)|*.xls|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() != DialogResult.OK)
                return;
            if (fdlg.FileName == "")
                return;
           string xlFileName=fdlg.FileName;
            this.textBox1.Text = fdlg.FileName;


     
            	try 
	{
        //excel stuff
        //' Open Excel
        Excel.Application xlApp = new Excel.Application();
        // Or get the current instance of Excel application:
        //xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");

        xlApp.Visible = true;
        xlApp.UserControl = true;
        xlApp.DisplayAlerts = false;

        Excel.Workbooks xlWorkBooks = (Excel.Workbooks)xlApp.Workbooks;
		Excel.Workbook xlWorkBook = default(Excel.Workbook);
		Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);
		Excel.Range xlRange = default(Excel.Range);
		Excel.Range xlUsedRange = default(Excel.Range);

		// Or get the current instance of Excel application:
		//xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");



        // Or Open workbook
        xlWorkBooks.Open(xlFileName, true, false, missing, "", missing, false, missing, missing, true, missing, missing, missing, missing, missing);
        
		xlWorkBook = xlApp.ActiveWorkbook;
        // access other members e.g.
		xlWorkSheet = (Excel.Worksheet)xlApp.ActiveSheet;
		xlRange = (Excel.Range)xlWorkSheet.Cells;
		xlUsedRange=(Excel.Range)xlWorkSheet.UsedRange;
		
		// convert used range to table, see Google
        //Test only:

        MessageBox.Show("Active Workbook: " + xlWorkBook.FullName + "\n" + 
                        "Active Sheet Name: "+ xlWorkSheet.Name + "\n" +
                       "Used Range Rows Count: "+  xlUsedRange.Rows.Count.ToString() + "\n"+
                      "Used Range Columns Count: "+  xlUsedRange.Columns.Count.ToString() );
        //// Say move to Sheet3:
        //((Excel._Worksheet)xlWorkBook.Sheets[3]).Activate(); //<-Underscore character is important in this case!
       // then get the active sheet
       // xlWorkSheet = (Excel.Worksheet)xlApp.ActiveSheet;

        ((Excel._Workbook)xlWorkBook).Close(false, xlWorkBook.FullName, null);//<-Underscore character is important in this case!
        xlApp.Quit();
        releaseObject(xlWorkBook);
        releaseObject(xlWorkSheet);
        releaseObject(xlApp);
    }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
        }

        private void BrowseExcel_Load(object sender, EventArgs e)
        {
            
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
           //  See http://support.microsoft.com/kb/320369
            // add your logic here

        }

        private void BrowseExcel_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = oldcult;
        }

        public static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
                obj = null;
            }
            catch (System.Exception ex)
            {

                System.Diagnostics.Debug.Print(ex.ToString());
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }

        
    }
}


This post has been edited by fixo: 10 June 2011 - 10:28 AM

Was This Post Helpful? 1
  • +
  • -

#11 daya88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-June 11

Re: c# import Excel Files - error references

Posted 10 June 2011 - 12:25 AM

thanks a lot :D. It works >:D<...
Was This Post Helpful? 0
  • +
  • -

#12 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: c# import Excel Files - error references

Posted 10 June 2011 - 03:51 AM

View Postdaya88, on 10 June 2011 - 12:25 AM, said:

thanks a lot :D. It works >:D<...

You're welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1