nyk0r

ExcelReaderEngine

May 13th, 2011
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data.OleDb;
  5. using System.Reflection;
  6.  
  7. namespace Sync.Core
  8. {
  9.     public class ExcelReaderEngine<T> where T: class, new()
  10.     {
  11.         private const string JET_CONNECTION_STRING =
  12.             "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};Extended Properties=Excel 8.0;;Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;MaxScanRows=0;\"";
  13.         private const string ACE_CONNECTION_STRING =
  14.             "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;MaxScanRows=0;\"";
  15.         private const string SELECT_STATEMENT = "SELECT * FROM [{0}$]";
  16.  
  17.         IEnumerable<FieldInfo> _fields = null;
  18.  
  19.         public ExcelReaderEngine()
  20.         {
  21.             _fields = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public).OrderBy(i => i.MetadataToken);
  22.         }
  23.  
  24.         public IEnumerable<T> ReadData(OleDbConnection conn, OleDbCommand comm)
  25.         {
  26.             List<T> result = new List<T>();
  27.             var reader = comm.ExecuteReader();
  28.             while (reader.Read())
  29.             {
  30.                 var index = 0;
  31.                 var entity = new T();
  32.                 foreach (var field in _fields)
  33.                 {
  34.                     var value = reader.GetValue(index);
  35.                     if (value is DBNull) { value = null; }
  36.                     index++;
  37.  
  38.                     if (field.FieldType == typeof(String))
  39.                     {
  40.                         field.SetValue(entity, value);
  41.                     }
  42.                     else if (field.FieldType == typeof(Int32))
  43.                     {
  44.                         field.SetValue(entity, value != null ? Int32.Parse(value.ToString()) : 0);
  45.                     }
  46.                     else if (field.FieldType == typeof(Double))
  47.                     {
  48.                         field.SetValue(entity, value != null ? Double.Parse(value.ToString()) : 0.0);
  49.                     }
  50.                     else if (field.FieldType == typeof(DateTime))
  51.                     {
  52.                         field.SetValue(entity, value != null ? DateTime.Parse(value.ToString()) : new DateTime());
  53.                     }
  54.                 }
  55.                 result.Add(entity);
  56.             }
  57.             return result;
  58.         }
  59.  
  60.         /// <summary>
  61.         /// Returns OPENED connection to Excel file.
  62.         /// </summary>
  63.         /// <param name="file">File name</param>
  64.         /// <returns>Connection</returns>
  65.         private OleDbConnection Connect(string file)
  66.         {
  67.             OleDbConnection result = null;
  68.             // This doesn't look so nice. But it's the only way to leap over MS Excel driver problems.
  69.             try
  70.             {
  71.                 result = new OleDbConnection(String.Format(ACE_CONNECTION_STRING, file));
  72.                 result.Open();
  73.             }
  74.             catch
  75.             {
  76.                 try
  77.                 {
  78.                     result = new OleDbConnection(String.Format(JET_CONNECTION_STRING, file));
  79.                     result.Open();
  80.                 }
  81.                 catch
  82.                 {
  83.                     throw;
  84.                 }
  85.             }
  86.             return result;
  87.         }
  88.  
  89.         public IEnumerable<T> GetData(string file, string page)
  90.         {
  91.             using (var conn = Connect(file))
  92.             {
  93.                 using (var comm = conn.CreateCommand())
  94.                 {
  95.                     comm.CommandText = String.Format(SELECT_STATEMENT, page);
  96.                     return ReadData(conn, comm);
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment