Implementing IEnumerable

Hi guys,

Im trying to implement the code below and im having trouble. Basically I want to use the excel datareade to rad the excel file then store the values and use them as the itemsource of a datagrid. My code is below...

  public class test : IEnumerable<Employee>
    {

        public IEnumerable<Employee> ReadFile(string filePath)
        {
            var employees = new List<Employee>();
            var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
            // Want to be able to handle .xls or .xlsx file formats
            var excelReader = filePath.Contains(".xlsx")
                                  ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                                  : ExcelReaderFactory.CreateBinaryReader(stream);

            excelReader.IsFirstRowAsColumnNames = true;

            excelReader.Read(); //skip first row
            while (excelReader.Read())
            {
                employees.Add(new Employee
                {
                    Name = excelReader.GetString(0),
                    Address = excelReader.GetString(1),
                    City = excelReader.GetString(2),
                    State = excelReader.GetString(3),
                    PostalCode = excelReader.GetString(4)
                });
            }

            excelReader.Close();

            return employees;
        }

    }

Open in new window


Its highlighting 'Employee' and saying.. The type or namespace 'Employee' could not be found (are you missing a using directive or an assembly reference? Does this actually need its own namespace?

Thanks,
Dean
deanlee17Asked:
Who is Participating?
 
ambienceConnect With a Mentor Commented:
It looks like you have added another source file to the same project and that has Main.

You can temporarily exclude other files from the project or better create another project and add just this file to it.
0
 
ambienceCommented:
The type Employee must be known to the compiler when compiling this code.

If Employee resides in another assembly then make sure you have added a reference to the assembly. (check References under project node)

If Employee has a namespace different than the one that class 'test' has then you need to add a using statement at the top, for example

using MyNamespace.Models;

(assuming Employee has namespace MyNamespace.Models).
0
 
athomsfereCommented:
Wouldn't it be just as easy / effective to bind the datagrid to the excel document and use LINQ?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
deanlee17Author Commented:
athomsfere, maybe? Do you have an example? Baring in mind this could be varying excel documents not just the one.

ambience, getting my head around your post.
0
 
deanlee17Author Commented:
Ok here is my complete code, its the first thing ive tried to do in this project.....

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Excel;

namespace DragDrop
{
 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

    }


    public class test : IEnumerable<Employee>
    {

        public IEnumerable<Employee> ReadFile(string filePath)
        {
            var employees = new List<Employee>();
            var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
            // Want to be able to handle .xls or .xlsx file formats
            var excelReader = filePath.Contains(".xlsx")
                                  ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                                  : ExcelReaderFactory.CreateBinaryReader(stream);

            excelReader.IsFirstRowAsColumnNames = true;

            excelReader.Read(); //skip first row
            while (excelReader.Read())
            {
                employees.Add(new Employee
                {
                    Name = excelReader.GetString(0),
                    Address = excelReader.GetString(1),
                    City = excelReader.GetString(2),
                    State = excelReader.GetString(3),
                    PostalCode = excelReader.GetString(4)
                });
            }

            excelReader.Close();

            return employees;
        }

    }
}

Open in new window

0
 
athomsfereCommented:
Looks like a tutorial for LINQ and Excel is of google code.

http://code.google.com/p/linqtoexcel/
0
 
ambienceCommented:
Looing at your complete code there is no Employee and no import (using statement). Are you missing out something? Could it be that you have to write the Employee class yourself, but havent yet?

Should be simple

public class Employee
{
     public string Name{ get; set; }
     public string Address { get; set; }
     public string City { get; set; }
     ...
}
0
 
deanlee17Author Commented:
Yes you are correct I needed to create the Employee Class.

However im just having a quick look at LinqToExcel as my project will have other LINQ queries as well. My code is below but im getting the errors

'Error      1      Program 'c:\Users\dean.lee\Desktop\DragDropSpreadsheet\DragDropSpreadsheet\obj\Debug\DragDropSpreadsheet.exe' has more than one entry point defined: 'DragDropSpreadsheet.Program.Main(string[])'.  Compile with /main to specify the type that contains the entry point.      C:\Users\dean.lee\Desktop\DragDropSpreadsheet\DragDropSpreadsheet\MainWindow.xaml.cs      31      21      DragDropSpreadsheet
'

and

'Error      2      Program 'c:\Users\dean.lee\Desktop\DragDropSpreadsheet\DragDropSpreadsheet\obj\Debug\DragDropSpreadsheet.exe' has more than one entry point defined: 'DragDropSpreadsheet.App.Main()'.  Compile with /main to specify the type that contains the entry point.      C:\Users\dean.lee\Desktop\DragDropSpreadsheet\DragDropSpreadsheet\obj\Debug\App.g.cs      62      28      DragDropSpreadsheet
'

I have copied the example exactly, any ideas where i have gone wrong with the entry point?...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LinqToExcel;
using Remotion.Data.Linq;



namespace DragDropSpreadsheet
{
    public class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Postcode { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var excel = new ExcelQueryFactory();
            excel.FileName = "WHATEVER";

            var people = from x in excel.Worksheet<Person>()
                         select x;
        }
    }
}

Open in new window

0
 
ambienceCommented:
OR goto project properties and define an entry point, from the dropdown list.
0
 
deanlee17Author Commented:
Ok done that and seems to work. Any idea how to add a file location? That example only seems to hold filename?

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.