Write and Create Excel Files in Swift

Emmanuel Orvain
3 min readSep 21, 2021

--

Creating Excel file is not en easy operation. In this article, I will show you how to create Excel files from scratch, add images to your sheet, and add style to your cells. The drawback of the library is that we can’t read an Excel sheet, we can only create Excel files.

Configure the project

Create the Podfile

If you create a project from scratch, then you must create a podfile at the root of your project. Copy the following line in your console.

pod init

Edit the Podfile

Now that the Podfile is created, you will be able to edit the Podfile. Add the libxlsxwriter library to your Podfile.

target 'xlsx_writer' douse_frameworks!# Pods for xlsx_writerpod 'libxlsxwriter'end

Install the library

The Podfile is correctly edited. Copy the following line in your console. It will automatically install the library in your project.

pod install

Xcode

Thanks to the previous operation, Pod will create or update the xcworkspace file of your project. Open it with your favorite IDE, Xcode.

It’s really easy to use the library, just import the library like this :

import xlsxwriter

How to create xlsx files

Configure the workbook

Now that you have well configured your project with cocoapods. Let’s create our export service. The first step is to create a workbook, and to create it we have to declare a pathfile where we have permission to create a new file. In this case we use the local filesystem of the application.

Now that we have created the workbook, we can add our worksheet. A workbook can contain several worksheets. In our case, we only use one worksheet.

With xlsxwriter, we can also, add some style to our workbook, that we will use later in the article. There is one style for the header, one style for the odd rows, and we use the default style for even rows.

We use a property flag (needWriterPreparation) for our class, to know when the xlsx properties are safe to be used. As soon as the workbook is closed, all the properties related to it (worksheet, formats, …) are in a corrupted state. Use them in this state will crash the application.

Create our table data

We use the minimal data to create a sheet with a header, 2 rows, and images. Don’t spend too much time to analyse this code, the goal is to focus on significant code.

Build the sheet header

The sheet header is at line 0. We use a counter to know at which line we are currently writing. In our case, a product is composed of 3 properties, a picture, a name, and a quantity. The column 1 is assigned to product pictures, the column 2 is assigned to names, and the column 3 is assigned to quantity.

We also set the header format to each header cell (bold).

Building product row

The product list begins at line 1. This line has a grey background to contrast with the header and the next product line.

Building the picture column

In the export method, we can see that we are copying image from our program memory to the Excel output file. Transformation are done on the UIImage, so that they will fill the destination cell. Each UIImage are transformed to a valid image buffer for xlsxwriter library.

Inspect your Excel file

Now that we are able to create an Excel file, we want to open it for inspection.

Start the project with a simulator, go the console, you will see an output that begin with : “open …/…/…”

Copy this line in your console, Finder will open the directory, and then you will be able to open the generated file.

If you enjoyed this post, please leave some claps. 👏👏👏

You can clap up to 50 times, so get clicking/tapping! 😉

--

--

Emmanuel Orvain

French iOS / Android experimented developper from Toulouse in south of France. https://occirama.com/scanandfile/