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

/ By Arjun / Published on February 22, 2018

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 a 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 the setup, let’s create a file called parse_excell.rake in app\lib\task(you can also use the rails g task command to generate a 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.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x