hi,
if you're willing to turn the Excel file into tab-separated text, it is easy to read the data into whatever is your data model. assuming your spreadsheet has column titles along its top row, the code below will give you a dictionary for every data row in the spreadsheet, keyed by the column titles
var theDatabase = YourDatabase() // whatever is your database model
// read the file as one big string
var fileContents:String
do {
fileContents = try String(contentsOfFile: destinationPath, encoding: String.Encoding.utf8)
} catch let error {
print("Error reading file: \(error.localizedDescription)")
fileContents = ""
}
guard fileContents.count>0 else {
return theDatabase
}
// split out records (separated by returns)
let records = fileContents.split { $0 == "\r" }
// first record is field names, i.e., column titles
let fieldNames = findFields(String(records[0]))
// all remaining records are data, so match each with field names of record 0
for k in 1..<records.count {
let values = findFields(String(records[k]))
var dictionary = [String:String]()
for n in 0..<values.count {
dictionary[fieldNames[n]] = values[n]
}
theDatabase.append(dictionary) // append would be a method in YourDatabase
}
if theDatabase above is simply an array of dictionaries, then you can get the data in a column labeled YOURLABEL and in row n just by referencing theDatabase[n]["YOURLABEL"].
hope that helps,
DMG