Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Implementing IEnumerable

Posted on 2013-01-23
10
Medium Priority
?
376 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

783 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