Callum Williams
Callum Williams

Reputation: 121

Use Data from Excel Sheet in Swift Xcode

For my app, I want to be able to import an excel sheet into Xcode and then use swift to programatically extract relevant pieces of data. I want to be able to access the data like a 2d-array.

I basically want a 2d array but just one then I can pre-import from an excel file.

I want to be able to call the piece of data in a particular cell, so for example I can get the data in cell A5.

I'm using Xcode 9.2, swift 4.

Upvotes: 4

Views: 9501

Answers (1)

Max Desiatov
Max Desiatov

Reputation: 5565

There is an open-source library CoreXLSX that can be imported into your project either with CocoaPods or Swift Package Manager. After it's integrated with your project you can import its module and use it this way:

import CoreXLSX

guard let file = XLSXFile(filepath: "./categories.xlsx") else {
  fatalError("XLSX file corrupted or does not exist")
}

for path in try file.parseWorksheetPaths() {
  let ws = try file.parseWorksheet(at: path)
  for row in ws.data?.rows ?? [] {
    for c in row.cells {
      print(c)
    }
  }
}

Upvotes: 2

Related Questions