Create Excel Workbook, Add Sheets & Images via Free Swift Library

Open Source Swift Library That allows Software Developers to Create & Export Excel Workbook. Add New Worksheets, Embed Pixel-Perfect Images, CSV/TSV Data, Apply Cell Formatting & Styling for Free.

What is XLKit ?

In the fast-paced world of iOS development, efficiency is king. Writing the same boilerplate code for common tasks like responsive grids, device detection, or haptic feedback can slow down even the most experienced developers. Enter XLKit, an open-source Swift package created by Amitabh Pandey that aims to supercharge your SwiftUI workflow with a powerful, curated toolkit of extensions and components. One of the standout features of XLKit is its effortless API. Designed with developer experience in mind, it offers a fluent and chainable interface that makes your code clean, readable, and highly maintainable. Say goodbye to convoluted syntax and hello to a more natural way of building your spreadsheets.

XLKit is a modern, open-source Swift library designed to simplify creation and manipulation of Excel (.xlsx) files on macOS and iOS. With a fluent, chainable API, image embedding, CSV/TSV import/export, type-safe formatting, and high Excel compliance, XLKit empowers software developers to produce professional-quality spreadsheets with ease. It’s built in pure Swift (Swift 6.0+) targeting macOS 12+ and iOS 15+, and prioritizes security, performance, and maintainable design. XLKit is a testament to the power of the open-source community in solving common development challenges. It doesn't try to be a massive, all-encompassing framework but instead focuses on providing a set of well-designed, practical tools that SwiftUI developers will actually use every day.

Previous Next

Getting Started with XLKit

The recommend way to install XLKit is using CocoaPods. Please use the following command for a smooth installation.

Install XLKit via CocoaPods

  // Add XLKit to your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/TheAcharya/XLKit.git", from: "1.0.8")
]

Install XLKit via GitHub

 git clone https://github.com/TheAcharya/XLKit.git

You can download it directly from GitHub.

Create and Work with Excel Spreadsheets via Swift

The open source XLKit library has provided powerful features for create and managing Excel spreadsheet files inside Swift applications. First you need to create a Workbook object, which will contain all your worksheets. A workbook can contain multiple worksheets. You can create one and start adding data in a grid-like fashion using a two-dimensional array ([[Cell]]). The following example demonstrates, how easily software developers can create a new workbook and then add worksheets inside it using Swift code.

How to Create New Excel Workbook and Add Worksheet inside It via Swift Library?

import XLKit

// Create a new Excel workbook
let workbook = Workbook()

// Add a worksheet named "Sales Report"
let sheet = workbook.addWorksheet("Sales Report")

// Define your data as a 2D array of Cell objects
let data: [[Cell]] = [
    [Cell("Product"), Cell("Q1 Sales"), Cell("Q2 Sales"), Cell("Total")],
    [Cell("Widget A"), Cell(2450), Cell(3105), Cell(5555)],
    [Cell("Widget B"), Cell(1800), Cell(2100), Cell(3900)],
    [Cell("Widget C"), Cell(975), Cell(1225), Cell(2200)]
]

// Add the data to the worksheet starting at cell A1
try? sheet.addData(data, startCell: CellReference(column: 0, row: 0))

Pixel-Perfect Image Embedding via Swift

The XLKit library takes image handling to the next level with its perfect image embedding capabilities. It automatically preserves the aspect ratio of your images, ensuring they look sharp and professional in your spreadsheets. Even better, the library features auto cell sizing, which automatically adjusts column widths and row heights to perfectly fit your images. The following example demonstrates, how easily software developers can embed a GIF into a spreadsheet cell using Swift API.

How to Embed a GIF Image into a Spreadsheet Cell via Swift Library?

let gifData = try Data(contentsOf: URL(fileURLWithPath: "alice.gif"))
try await sheet.embedImageAutoSized(gifData, at: "B2", of: workbook)

Comprehensive Cell Formatting via Swift Library

A professional-looking spreadsheet is all about the details. The XLKit library provides a rich set of cell formatting features, allowing software developers to control everything from font colors and background fills to borders and text alignment. This enables users to create visually appealing and easy-to-read reports and data exports. Here is a useful example that shows how to apply formatting to a worksheet cell inside Swift applications.

How to Apply Formatting to Sheet Cells inside Swift Apps?

sheet.setCell("A1", string: "Name", format: CellFormat.header())
sheet.setCell("B1", string: "Photo", format: CellFormat.header())
sheet.setCell("C1", string: "Age", format: CellFormat.coloredText(color: "#FF0000"))

Import and Export CSV/TSV Data via Swift Library

In today's data-driven world, the ability to work with various data formats is crucial. The open source XLKit library has included several important features for loading and working CSV and TSV data using Swift commands. The library comes with built-in support for importing and exporting CSV and TSV data, making it incredibly easy to integrate your Excel workflows with other data sources and applications.