How to create rake task to parse Excel file and insert into table in ROR

In one of my recent project, I am receiving Excel files from external APIs and I have to parse, validate data and then I have to insert into database table. So I have created a task to automate this process, this task will run every time when there is a change in the directory (Whenever I receive new file). In this post, I am not going to share the actual implantation, but I will share a part of it here.

Create Project

Fire up the terminal and run following rails command to create project, here excel-parse is project name:

It will generate a excel-parse-demo rail project; we will discuss it later. Currently, we have to check if the environment is set up or not. Next, use the following command to run the web server on your machine.

Now open your browser and type the following −

It should display a message, something like, “Welcome aboard” or “you’are on Rails”.

Create a Task

Once you are done with setup, lets create a file called parse_excell.rake in app\lib\task(you can also use rails g task command to generate task file.) and copy paste the below code:

Let me go over the code, to read .xlsx file we are using roo gem, and inorder to run this task you have to install this gem, to install run below command,

How to run task

From the root of the project run the task just like another rails task.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets. Protection Status