?
Solved

Implementing IEnumerable

Posted on 2013-01-23
10
Medium Priority
?
373 Views
Last Modified: 2013-04-02
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
0
Comment
Question by:deanlee17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 38809986
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
 
LVL 14

Expert Comment

by:athomsfere
ID: 38809996
Wouldn't it be just as easy / effective to bind the datagrid to the excel document and use LINQ?
0
 

Author Comment

by:deanlee17
ID: 38810007
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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:deanlee17
ID: 38810022
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
 
LVL 14

Expert Comment

by:athomsfere
ID: 38810043
Looks like a tutorial for LINQ and Excel is of google code.

http://code.google.com/p/linqtoexcel/
0
 
LVL 22

Expert Comment

by:ambience
ID: 38810147
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
 

Author Comment

by:deanlee17
ID: 38810285
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
 
LVL 22

Accepted Solution

by:
ambience earned 2000 total points
ID: 38810566
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
 
LVL 22

Expert Comment

by:ambience
ID: 38810573
OR goto project properties and define an entry point, from the dropdown list.
0
 

Author Comment

by:deanlee17
ID: 38810624
Ok done that and seems to work. Any idea how to add a file location? That example only seems to hold filename?

Thanks.
0

Featured Post

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question