After Thought

Posts Tagged ‘Excel

Now you can insert records into Excel using ExcelMapper

leave a comment »

You can now use ExcelMapper to insert records into Excel as well. Continuing with the same User example, we can insert a User (John Doe) into Excel using ExcelMapper.

ExcelMapper.SetUp();
IRepository repository = ServiceLocator.Current.GetInstance<IRepository>();

List<User> users = new List<User>
{
   new User
   {
       Id = 1,
       LastName = "Doe",
       FirstName = "John
       DateOfBirth  = Convert.ToDateTime("2/2/1990")
   }
};
repository.Put<User>(users);

Note that User DTO was generated using BuildDTOsFromExcel.exe.

repository.Put<User>(User) saves the Users List into Excel. Since we don’t have the concept of unique keys yet, it can only insert and not update.

Advertisements

Written by shashankshetty

July 27, 2009 at 10:11 am

Posted in C#, ExcelMapper, Microsoft Excel

Tagged with , , ,

Using ExcelMapper

with 5 comments

ExcelMapper is a tool to create and access data from excel (*.xls and *.xlsx) as a collection of strongly typed objects.  Here is  brief description on how to use it:

BuildDtosFromExcel.exe is an exe file used to generate an assembly of strongly typed objects. Each class is equivalent to a worksheet in an excel file. For instance, User tab in excel is mapped to User class and each of the columns is mapped to properties in the User class.

UserExcel

To create a User class, run the following command:

BuildDTOsFromExcel User.xlsx

The command generates ExcelToDTOMapper.DTO.dll assembly that contains User class as shown below:

 namespace ExcelToDTOMapper.DTO
 {
   public class User
   {
     public System.Double Id { get; set; }
     public System.String LastName { get; set; }
     public System.String FirstName { get; set; }
     public System.DateTime DateOfBirth { get; set; }
   }
 }

If you want to create an assembly of different name, you can pass an [optional] assembly name. You can get the complete usage of BuildDTOsFromExcel by running BuildDTOsFromExcel /?.

You can now add this assembly as a reference to your project along with ExcelMapper.dll, Microsoft.Practices.ServiceLocation (available in the download) to access data from Excel.

ExcelMapperReference

Finally, add the following piece of code in your application.


  ExcelMapper.SetUp();

  IRepository repository = ServiceLocator.Current.GetInstance<IRepository>();

  foreach (User user in repository.Get<User>("Excel\\Users.xlsx", "User"))
  {
     Console.WriteLine("Hello {0} {1}", user.FirstName, user.LastName);
  }
 

ExcelMapper.SetUp() should be done only once in the whole application. If you are using StructureMap as your IoC tool, then you can add the ExcelMapperRegistry in your BootStrapper code as show belo:

public static void Initialize()
{
   ObjectFactory.Initialize(x =>
      {
          x.Scan(s =>
          {
              s.TheCallingAssembly();
              s.WithDefaultConventions();
          });
          x.AddRegistry<ExcelMapperRegistry>();
      }
   );
}

If your running on a 64 bit machine, remember to compile your code in x86 platform. Executing the above code results in the following output:

ExelMapperOutput

Since repository.Get<User>(“Excel\\Users.xlsx”, “User”) returns IEnumerable<User>, you can use Linq.

Written by shashankshetty

May 9, 2009 at 2:56 pm

Reading excel spread sheet as strongly typed objects

with one comment

Recently, I was working on a prototype application that required data to be read from many different Excel spreadsheets (both xls and xlsx versions) each containing a large number of worksheets.  I started with the usual way of creating one DTO (Data Transfer Object) for each worksheet and then pulling the data from Excel through OleDBReader. It became repetitive to do the same thing over and over for every single worksheet. After quick thought, I decided to write a simple tool that can solve this problem of data access (as I didn’t want to spend a lot of time just building the infrastructure) .

The end result is a tool that can generate an assembly containing Strongly Typed DTO’s by querying the excel (as I didn’t want to create them manually) that can then be included as a reference in our project and a library that provides methods to query the excel data through a single repository class. By abstracting out the DTO generation and accessing data from excel, I can now concentrate on building the prototype application that can deliver some business value.

Code for this is available on Git Hub: https://github.com/mvba/ExcelMapper

Download the BuildDTOsFromExcel.exe and ExcelMapper.dll with an example usage here.

You can read a brief description on how to use ExcelMapper here

We can definitely improve this tool further, but it works if you are looking for a quick solution to query the excel sheets. If you are interested in improving this tool, please feel free to comment.

Written by shashankshetty

May 8, 2009 at 9:25 pm