1. Products
  2.   Spreadsheet
  3.   Swift
  4.   XlsxReaderWriterSwift
 
  

Free Swift Library to Read and Write Excel XLSX Files

Open Source Robust Swift 5 Library That Empowers Developers to Create and Read XLSX Files, Copy, Remove Sheets, Apply Styles and Formatting and so on for free.

What is XlsxReaderWriterSwift ?

Tired of wrestling with complex spreadsheet formats in your Swift projects? Imagine a scenario where your app needs to generate a detailed report, import user data from a template, or even build a dynamic financial model—all directly within the iOS ecosystem. Manually parsing CSV files is limiting, and relying on external services to handle XLSX files is inefficient. This is where XlsxReaderWriterSwift, a powerful and native open-source library, comes to the rescue. It provides Swift developers with an intuitive and robust toolkit to seamlessly read from and write to Microsoft Excel's modern .xlsx format, right inside your iOS, macOS, and visionOS apps.

XlsxReaderWriterSwift is a robust Swift 5 library that empowers software developers to create and read XLSX files, the standard for modern Excel spreadsheets. The library has provided several important features for working with Microsoft Excel XLSX file, such as reading XLSX file, creating new XLSX files, applying style and formatting to cells, adding and managing image in cells, generating reports, embedding spreadsheets, and many more. Built on top of the C library libxlsxwriter, it brings a comprehensive set of features to the Apple ecosystem. This means you can generate fully-featured, 100% compatible Excel files directly from your iOS or macOS application.

Previous Next

Getting Started with XlsxReaderWriterSwift

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

Install XlsxReaderWriterSwift via CocoaPods

 pod "XlsxReaderWriterSwift"

pod install

Install XlsxReaderWriterSwift via GitHub

git clone https://github.com/mehulparmar4ever/XlsxReaderWriterSwift.git

You can download it directly from GitHub.

Create & Write to a New XLSX File via Swift Library

Creating a new file from scratch and populating it with data is just as easy. The open source XlsxReaderWriterSwift library has provided complete support for creating and managing new Excel XLSX spreadsheet inside Swift applications. Software developers can create new worksheet, rename existing worksheet, copy worksheet data, save document to specific place and so on. The following example shows, how software developers can create a new Excel XLSX worksheet using Swift library.

How to Create a New Excel XLSX Worksheet via Swift Library?

import XlsxReaderWriterSwift

// 1. Create a new, empty document
let document = BRAOfficeDocumentPackage()

// 2. Get the first worksheet and give it a name
guard let worksheet = document.workbook.worksheets.first as? BRAWorksheet else {
    return
}
worksheet.name = "User Data"

// 3. Write different types of data to cells
worksheet.cell(forCellReference: "A1", shouldCreate: true).stringValue = "Name"
worksheet.cell(forCellReference: "B1", shouldCreate: true).stringValue = "Score"
worksheet.cell(forCellReference: "A2", shouldCreate: true).stringValue = "Alice"
worksheet.cell(forCellReference: "B2", shouldCreate: true).integerValue = 95
worksheet.cell(forCellReference: "A3", shouldCreate: true).stringValue = "Bob"
worksheet.cell(forCellReference: "B3", shouldCreate: true).integerValue = 87

// 4. Define a file path to save the document
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentsDirectory.appendingPathComponent("NewReport.xlsx")

// 5. Save the document to the specified path
do {
    let data = document.save()
    try data?.write(to: fileURL)
    print("Spreadsheet saved successfully at: \(fileURL.path)")
} catch {
    print("Failed to save spreadsheet: \(error)")
}

Apply Advanced Cell Styling via Swift

A spreadsheet isn't just about data; presentation matters. The open source XlsxReaderWriterSwift API provides extensive control over cell styling. It goes beyond simple data entry. You can apply a wide range of formatting options, including, cell alignment and number formatting, creating new cells, creating new styles, accessing existing styles, applying a border to a range of cells, and so on. Here is a simple example that show how software developers can Apply advance styles to an Excel cells using Swift commands.

How to Apply Advance Styles to Excel Cells using Swift Library?

// ... (after creating a worksheet and cell)

// Get or create a cell
let headerCell = worksheet.cell(forCellReference: "A1", shouldCreate: true)
headerCell.stringValue = "Sales Report"

// Access the cell's style
let style = headerCell.cellStyle()

// Configure the style
style.font.bold = true
style.font.size = 18
style.fill.foregroundColor = .blue // Background color
style.fill.patternType = .solid
style.alignment.horizontal = .center

// Apply a border to a range of cells
let borderStyle = BRABorderStyle()
borderStyle.lineStyle = .medium
borderStyle.color = .darkGray

let dataRange = worksheet.cells(inRows: 1...3, columns: 1...2)
for cell in dataRange {
    (cell as? BRACell)?.cellStyle().bottomBorder = borderStyle
}

Reading an XLSX File via Swift Library

The open source XlsxReaderWriterSwift library allows software developers to manipulate data inside Excel Spreadsheet using Swift API. The library allows to perform various data manipulation operations, such as sorting, filtering, and calculating values within Excel sheets. This makes it an ideal tool for tasks like data analysis and reporting. Moreover, software developers can easily extract data from specific cells, rows, or columns within an Excel sheet using Swift API. This is incredibly useful when dealing with large datasets and only needing specific information.

How to Read Data from an Excel XLSX File Free Swift API?

import XlsxReaderWriterSwift
// 1. Locate the .xlsx file in your app's bundle
guard let filePath = Bundle.main.path(forResource: "SampleData", ofType: "xlsx") else {
    print("File not found!")
    return
}

// 2. Create a BRAOfficeDocumentPackage object from the file
let document = BRAOfficeDocumentPackage.open(filePath)

// 3. Get the first worksheet
guard let worksheet = document.workbook.worksheets.first as? BRAWorksheet else {
    print("No worksheets found!")
    return
}

// 4. Read data from specific cells
if let cellA1 = worksheet.cell(forCellReference: "A1") {
    let stringValue = cellA1.stringValue() // Gets the value as a String
    print("A1: \(stringValue)")
}

if let cellB2 = worksheet.cell(forCellReference: "B2") {
    let numberValue = cellB2.floatValue() // Gets the value as a Float
    print("B2: \(numberValue)")
}

// 5. Iterate over a range of cells
for row in 1...5 {
    for column in 1...3 {
        let cellReference = "\(UnicodeScalar(64 + column)!)\(row)" // Creates refs like A1, B1, etc.
        if let cell = worksheet.cell(forCellReference: cellReference),
           let value = cell.stringValue() {
            print("\(cellReference): \(value)")
        }
    }
}