Open Source Swift Library to Parse & Search Excel Data
Open Source Swift Library that Allows Developers to Parse and Get Data from Microsoft Excel File Formats (XLSX, XLSM, XLSB) via Swift API.
What is CoreXLSX Library?
For developers working with Excel files in Swift, CoreXLSX offers a streamlined solution as a powerful open source Swift spreadsheet library. It provides the essential ability to create Excel Spreadsheets and efficiently parse Excel spreadsheets in formats like XLSX and XLSM. By leveraging the underlying XML structure of Excel files, the library ensures fast, low-memory operations, making it ideal for handling large documents. A key strength is its capacity to parse style information of XLSX files, giving you access to detailed formatting data such as fonts and cell styles directly within your Swift application.
Beyond basic parsing, CoreXLSX includes robust features for deep data interaction. You can precisely search data in spreadsheets to locate cells by reference and get data from Excel XLSM and other supported formats. The library supports advanced tasks like reading shared strings across worksheets, identifying cell positions, and extracting raw cell data for external analysis in tools like Power BI. Designed for performance and ease of integration, this open-source library is an excellent choice for any Swift project that requires reliable, efficient, and detailed Excel file management.
Getting Started with CoreXLSX
The recommend way to install CoreXLSX is using CocoaPods. Please use the following command for a smooth installation.
Install CoreXLSX via CocoaPods
pod 'CoreXLSX', '~> 0.14.1'
pod install
You can download it directly from GitHub.
Reading Data Excel XLSX File via Swift API
The open source CoreXLSX library allows Swift developers to open an existing Excel XLSX document, read and get data from it using Swift commands. To get data from an Excel file, first you need to load the file and then can access the individual worksheets in the file using the sheet(named:) method. After accessing the sheet you can iterate over its rows and columns to read the cell values. The following example shows how to read the first worksheet in the Excel file at the specified path and prints out the contents of each cell in the worksheet.
How to Read First Worksheet of Excel File via Swift API?
import CoreXLSX
guard let file = XLSXFile(filepath: "path/to/file.xlsx") else {
fatalError("XLSX file not found")
}
do {
let sheet = try file.parseWorksheet(at: 0)
for row in sheet.data?.rows ?? [] {
for cell in row.cells {
print(cell)
}
}
} catch {
print(error.localizedDescription)
}
Search & Find a Cell using Swift API
The open source CoreXLSX library has provided a very useful feature allowing software developers to search out a worksheet’s cell data by using a cell reference inside their Swift application. The library has provided the ‘worksheet.cells’ property allowing developers to access the cells in the worksheet. Then, use the ‘at’ method to get the cell with the specified reference. The following example demonstrates how to access particular cells in the worksheet using Swift code.
How to Access Particular Cells in a Worksheet using Swift?
import CoreXLSX
# Load Excel File
guard let file = XLSXFile(filepath: "/path/to/your/file.xlsx") else {
fatalError("XLSX file at path not found")
}
# Access the worksheet that contains the cell
guard let worksheet = try file.parseWorksheet(at: "/xl/worksheets/sheet1.xml") else {
fatalError("Worksheet not found")
}
# access the cells in the worksheet
guard let cell = worksheet.cells[at: "B3"] else {
fatalError("Cell not found")
}
# Display Cell Value
print(cell.value)
Parse Style Information of XLSX File via Swift API
The open source CoreXLSX library has included a very useful feature for parsing style information of XLSX file since version 0.5.0. Software developers can use the new parseStyles() function to parse style information from the archive inside their Swift application. The library allows developers to fetching a list of fonts used, getting formatting for a given cell, get fill, and border information and so on. The following code shows to get list of fonts used and formatting for a given cell using Swift commands.
How Fetches a List of Fonts Used via Swift Commands?
let styles = try file.parseStyles()
let fonts = styles.fonts?.items.compactMap { $0.name?.value }
//get formatting for a given cell
let styles = try file.parseStyles()
let format = worksheet.data?.rows.first?.cells.first?.format(in: styles)
let font = worksheet.data?.rows.first?.cells.first?.font(in: styles)