Working with Excel in Swift (iOS/macOS): Library Overview and Code Examples
Processing tabular data is one of the key tasks in modern applications. On Apple platforms (iOS, macOS, watchOS) there is no built-in support for the Excel format (.xlsx), so developers use third-party libraries. In this article, we will look at three main approaches and libraries for working with Excel in Swift: CoreXLSX, SwiftExcel, and XLConnect. You will learn how to read, create, and edit files, and also get ready-made code examples.
1. CoreXLSX — reading and writing .xlsx files
CoreXLSX is the most popular library for working with Excel in Swift. It supports reading and writing files in the Office Open XML (OOXML) format. The library is written in pure Swift and requires no external dependencies.
Installation via Swift Package Manager
// In Package.swift add:dependencies: [ .package(url: "https://github.com/CoreOffice/CoreXLSX.git", from: "0.14.0")]Reading an Excel file
import CoreXLSX
guard let filePath = Bundle.main.path(forResource: "data", ofType: "xlsx") else { print("File not found") return}
let file = try XLSXFile(filepath: filePath)let sharedStrings = try file?.parseSharedStrings()let worksheets = try file?.parseWorksheetPaths()
for worksheetPath in worksheets ?? [] { let worksheet = try file?.parseWorksheet(at: worksheetPath) let rows = worksheet?.data?.rows ?? [] for row in rows { for cell in row.cells { if let sharedStrings = sharedStrings { let reference = cell.reference let value = cell.stringValue(sharedStrings) ?? cell.value ?? "" print("Cell \\(reference): \\(value)") } } }}Creating a new file
import CoreXLSX
let path = FileManager.default.temporaryDirectory.appendingPathComponent("newFile.xlsx")let file = XLSXFile(filepath: path.path)
// Create a worksheetlet worksheet = Worksheet(data: DataSheet(rows: [ Row(cells: [ Cell(reference: "A1", value: "Name"), Cell(reference: "B1", value: "Age") ]), Row(cells: [ Cell(reference: "A2", value: "Anna"), Cell(reference: "B2", value: "25") ])]))
try file?.write(worksheet: worksheet, at: "xl/worksheets/sheet1.xml")2. SwiftExcel — a lightweight alternative
SwiftExcel is a minimalistic library for creating Excel files. It does not support reading existing files, but it is great for generating reports and exporting data.
Installation via CocoaPods
pod 'SwiftExcel'Example of creating a table
import SwiftExcel
let workbook = Workbook()let sheet = workbook.createSheet(named: "Report")
// Fill in headerssheet.write(value: "Product", column: 1, row: 1)sheet.write(value: "Price", column: 2, row: 1)sheet.write(value: "Quantity", column: 3, row: 1)
// Datasheet.write(value: "Laptop", column: 1, row: 2)sheet.write(value: 1500.00, column: 2, row: 2)sheet.write(value: 10, column: 3, row: 2)
// Savelet path = FileManager.default.temporaryDirectory.appendingPathComponent("report.xlsx")try workbook.save(to: path)print("File saved: \\(path)")3. XLConnect — working with Excel via OpenXML (macOS)
XLConnect is a library for macOS that uses the OpenXML format directly. It supports complex operations: merging cells, styles, formulas, and charts.
Installation via Carthage
github "SwiftStudies/XLConnect" ~> 2.0Example with formulas and styles
import XLConnect
let workbook = Workbook()let sheet = try workbook.createShee Also in library
Working with XML in Rust: quick-xml, roxmltree, and serde_xml_rs libraries
JWT tokens in C# (.NET): working with System.IdentityModel.Tokens.Jwt
Object Serialization in TypeScript: A Complete Guide
Authorization and Authentication in C++: Libraries and Implementation
Swift WebSocket Libraries: Starscream and URLSessionWebSocketTask