2 Replies - 5823 Views - Last Post: 08 December 2011 - 09:06 AM Rate Topic: -----

#1 paul23  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-November 11

read excel file in c#

Posted 08 December 2011 - 06:39 AM

Hi guys,

I feel like quite a noob asking this... because I saw a lot on the internet for accessing excel files through c#. but for some kind of reason it's still not working. When I started with this I saw that I had to do a PIA installation so that I can access Microsoft.office.interop based functions. So I basically want to know if there's still something short in my references... or what else could be the problem.

In short, this is what I did:

using Microsoft.Office.Interop.Excel;

public void import()
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.FileName = "*.xls";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
                   openFileDialog1.FileName, 0, true, 5,
                    "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,0, true);
                Excel.Sheets sheets = theWorkbook.Worksheets;
                Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
                for (int i = 1; i <= 10; i++)
                    Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
                    System.Array myvalues = (System.Array)range.Cells.Value;
                    string[] strArray = ConvertToStringArray(myvalues);

and errors attached.

Attached image(s)

  • Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: read excel file in c#

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: read excel file in c#

Posted 08 December 2011 - 08:05 AM

There is a big list of errors stating problems and exactly what is wrong with them. Have you tried clicking and resolving them at all?

For example "ExcelObj does not exist in the context" error is because you've just used the name without actually creating an object. You can fix these errors yourself, if you're going to copy code from places tutorials don't expect it to just work in your program.

This post has been edited by negligible: 08 December 2011 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: read excel file in c#

Posted 08 December 2011 - 09:06 AM

The objects you are accessing don't exist in the excel worksheet (see the errors, read them carefully)

Also just another hint, you should take care where do you put your code... If you put the same code in two different places it may not compile (since in two different places different variables are present)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1