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();
}
}
}
Sam HobbsPosted Jun 7, 2011, 2:17 PM
There are basicly two ways to add a reference for Excel; the newer and the older ways. The older way is done from the COM tab; the newer way is from the .Net tab. I get confused about which is which. My first reply in this thread is consistent with the new way. You are probably too quick to conclude that it does not work.
See Accessing Microsoft Office InterOP Objects using C# 4.0 for a good article about how to do it. Except it uses dynamic and I suggest not using dynamic.
diana oneaPosted Jun 7, 2011, 1:37 PM
Sam HobbsPosted Jun 7, 2011, 1:35 PM
diana oneaPosted Jun 7, 2011, 7:52 AM
Thank you for your reply. I already added that reference. And when I use using Excel = Microsoft.Office.Interop.Excel; I receive this errors :
xlsApp = new Excel.ApplicationClass();
Sam HobbsPosted Jun 7, 2011, 12:10 AM
Do you have a using for "Microsoft.Office.Interop"? If not then put that in. Then add the following line after the using statements:
Please be sure to call xlsApp.Quit() if the xlsApp object is valid because if you don't then an instance of Excel will continue after your program exits.