DNK Gif

Dot Net Knowledge

Labels

Tuesday, 7 July 2015

Use Excel Sheet as DataBase

How to use Excel sheet as DataBase?

Excel sheet can be used as DataBase. Here it goes.
Latest version of Aspose.cells needed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;

namespace BirthDay
{
    public class Decider
    {
        public static string GetRecord()
        {
            string nameList = string.Empty;
            int startColumnDOB = 2;
            int startRowDOB = 1;
            int countExit = 0;
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory;
            string fileName = System.Configuration.ConfigurationSettings.AppSettings["ExcelFileName"];          
            filePath = filePath + fileName;
            Workbook wb = new Workbook(filePath);
            Worksheet ws = wb.Worksheets[0];
            Cells cells = ws.Cells;
            IList<Person> personList = new List<Person>();
            int r = startRowDOB;
            int c = startColumnDOB;
            while (countExit < 5)
            {
                string cellValue = cells[r, c].StringValue;
                if (string.IsNullOrWhiteSpace(cellValue))
                {
                    countExit++;
                    continue;
                }
                string[] dob = cellValue.Split('/');
                int month = 0; 
                int.TryParse(dob[0], out month);
                int day=0;
                int.TryParse(dob[1], out day);
                DateTime dateToday = DateTime.Now.Date;
                if ((month == dateToday.Month) && (day == dateToday.Day))
                {
                    Person person = new Person();
                    person.Id = decimal.Parse(string.IsNullOrWhiteSpace(cells[r, c - 2].StringValue) ? "0" : cells[r, c - 2].StringValue);
                    person.Name = cells[r, c - 1].StringValue;
                    person.DOB = dateToday;
                    nameList += person.Name + Environment.NewLine;
                }
                r++;
            }
            return nameList;
        }
    }
}

For security purpose password protected ExcelSheet can be used.
Download the Sample Project here.

No comments:

Post a Comment