Working with Excel in Swift on iOS and macOS: Libraries and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Excel in Swift (iOS/macOS): Libraries and Code Examples



Developing applications for iOS and macOS often requires integration with the Excel format (.xlsx, .xls). Apple's native tools do not provide a direct API for working with these files, so developers use third-party libraries. In this article, we will look at three popular options: CoreXLSX, xlsxwriter, and SwiftCSV, and provide practical examples for reading and writing data.



1. CoreXLSX — Reading and Writing .xlsx

CoreXLSX is an open-source library written entirely in Swift. It is based on the internal ZIP format (since .xlsx is a ZIP archive with XML files). It supports reading, writing, and editing sheets, cells, and styles.



Installation: via Swift Package Manager (SPM), add the package https://github.com/CoreOffice/CoreXLSX.git.



Example: Reading Data from Excel

import CoreXLSX

func readExcelExample() { guard let filePath = Bundle.main.path(forResource: "data", ofType: "xlsx"), let data = try? Data(contentsOf: URL(fileURLWithPath: filePath)), let file = try? XLSXFile(data: data) else { print("Failed to load file") return } do { let sharedStrings = try file.parseSharedStrings() let worksheet = try file.parseWorksheet(at: "xl/worksheets/sheet1.xml") for row in worksheet.data?.rows ?? [] { var rowValues: [String] = [] for cell in row.cells { if let stringRef = cell.stringValue(sharedStrings) { rowValues.append(stringRef) } else if let value = cell.value { rowValues.append(value) } else { rowValues.append("") } } print(rowValues.joined(separator: ", ")) } } catch { print("Parsing error: \\(error)") }}


2. xlsxwriter — Creating Excel Files in Swift

xlsxwriter is a wrapper around the libxlsxwriter library (C implementation). It allows creating .xlsx files with formatting, formulas, and charts. Ideal for generating reports on macOS and iOS.



Installation: via CocoaPods (pod 'xlsxwriter') or SPM.



Example: Creating a Simple File

import xlsxwriter

func createExcelExample() { let documentDirectory = FileManager.default.temporaryDirectory let fileURL = documentDirectory.appendingPathComponent("report.xlsx") let workbook = Workbook(path: fileURL.path) let sheet = workbook.addWorksheet(named: "Report") // Headers sheet.writeString("Name", at: column: 0, row: 0) sheet.writeString("Age", at: column: 1, row: 0) sheet.writeString("City", at: column: 2, row: 0) // Data sheet.writeString("Anna", at: column: 0, row: 1) sheet.writeNumber(28, at: column: 1, row: 1) sheet.writeString("Moscow", at: column: 2, row: 1) sheet.writeString("Ivan", at: column: 0, row: 2) sheet.writeNumber(35, at: column: 1, row: 2) sheet.writeString("Saint Petersburg", at: column: 2, row: 2) workbook.close() print("File created: \\(fileURL)")}


3. SwiftCSV — Working with CSV (Excel Alternative)

If you don't need the full .xlsx format, SwiftCSV is a lightweight library for reading and writing CSV files. CSV can be easily opened in Excel and other spreadsheet editors.



Installation: via SPM: https://github.com/swiftcsv/SwiftCSV.git.



Example: Reading and Writing CSV

import SwiftCSV

func csvExample() { // Reading guard let csvURL = Bundle.main.url(forResource: "data", with

Recommendations