Skip to content

Excel formatting with R

Excel formatting with R

Many times there is a demand from end users to deliver results in a form of Excel files. The bigger and more complex the files grow, the more difficult it is to properly interpret them. This is when nice formatting can help you out.
Recently I was doing quite complex processing in R and was needing an easy to use package for Excel formatting.

My initial attempt to produce nice and colourful Excel reports was with xlsx package. Unfortunately xlsx is a java based package and with 3MB files I quickly started getting java out of memory error messages. After little more digging I came across openxlsx package, which was exactly what I needed. openxlsx is based on C++, which makes it much faster than xlsx and saves you from java memory dependency.

Here I show how easy and pleasant it is to do Excel formatting with openxlsx R package.



First, as with any R package, you need to have it installed and loaded.

Then you can create Excel workbook and add some sheet into it.

Let’s say you want to add some preprocessed data (my_data) starting from the second row. In the first row let’s do some column merging and colouring.


We can apply some nice formatting. First you need to define style and then apply it with addStyle function.


Now you can simply save the output. When you create workbook from the scratch you don’t have to set overwrite flag. It is required though when you want to append some data into already created workbook.

Voila, those are the basics which in a very simple way allow to nicely pump up the report. You can find more openxlsx features in the docs. There are options to insert images, comments, plots, to freeze panels, set footers and more.


Let’s use well know iris build-in dataset. In this data we have 3 types of iris flowers, let’s save data from each species into separate Excel files.

Created file:

Leave a Reply

Your email address will not be published. Required fields are marked *