Working with Excel in Swift: Libraries and Tools for iOS and macOS
When developing applications for iOS and macOS, there is often a need to import, export, or process data in Excel format (XLSX, CSV). The Apple platform does not have built-in support for Excel formats, so developers use third-party libraries. This article covers three popular tools: CoreXLSX, XLSXWriter, and SwiftCSV, and provides practical code examples in Swift.
1. CoreXLSX — Reading and Writing XLSX
CoreXLSX is a lightweight library for reading and creating XLSX files. It is based on the Open XML standard and supports strings, numbers, dates, formulas, and styles.
Installation
// Swift Package Managerdependencies: [ .package(url: "https://github.com/CoreOffice/CoreXLSX.git", from: "0.14.0")]Example: Reading Data from Excel
import CoreXLSX
func readExcel(path: String) { guard let file = XLSXFile(filepath: path) else { print("Error opening file") return } do { for path in try file.parseWorksheetPaths() { let worksheet = try file.parseWorksheet(at: path) let rows = worksheet.data?.rows ?? [] for row in rows { let values = row.cells.compactMap { $0.value } print(values.joined(separator: "\\t")) } } } catch { print("Read error: \\(error)") }}Example: Creating a New XLSX File
import CoreXLSX
func createExcel() { let header = ["Name", "Age", "City"] let data = [ ["Anna", "25", "Moscow"], ["Boris", "30", "Saint Petersburg"] ] var sharedStrings = SharedStrings() let headerRefs = header.map { sharedStrings.append(string: $0) } let dataRefs = data.map { row in row.map { sharedStrings.append(string: $0) } } let headerRow = Row(cells: headerRefs.enumerated().map { (index, ref) in Cell(reference: "\\(Character(UnicodeScalar(65 + index)!))1", type: .string, value: ref) }) let dataRows = dataRefs.enumerated().map { (rowIndex, row) in Row(cells: row.enumerated().map { (colIndex, ref) in let colRef = Character(UnicodeScalar(65 + colIndex)!) return Cell(reference: "\\(colRef)\\(rowIndex + 2)", type: .string, value: ref) }) } let worksheet = Worksheet(data: Data(rows: [headerRow] + dataRows)) let file = XLSXFile(sharedStrings: sharedStrings, worksheets: [worksheet]) do { try file.write(to: URL(fileURLWithPath: "/tmp/example.xlsx")) print("File saved") } catch { print("Write error: \\(error)") }}2. XLSXWriter — Fast Writing of Large Files
XLSXWriter is a library focused on write performance. It is suitable for exporting thousands of rows and supports styles, cell merging, and charts.
Installation
// Via CocoaPodspod 'XLSXWriter'Example: Writing Data with Formatting
import XLSXWriter
func writeFormattedExcel() { let workbook = Workbook() let sheet = workbook.addSheet(named: "Report") // Styles let headerStyle = CellStyle() headerStyle.font = Font(name: "Helvetica", size: 14, bold: true) headerStyle.fill = Fill(color: .lightGray) let dataStyle = CellStyle() dataStyle.font = Font(name: "Helvetica", size: 12) dataStyle.border = Border(top: .thin, bottom: .thin) // Headers sheet.write(row: 0, column: 0, value: "Product", style: headerStyle) sheet.write(row: 0, column: 1, value: "Price", style: headerStyle) sheet.write(row: 0, column: 2, value: "Quantity"