'Downloading and Processing .xls (Excel) files in Swift
I want to build an Iphone app that can download an Excel file and show the content in a table.
I think i managed to download the file but the data i get looks like this:
<d0cf11e0 a1b11ae1 00000000 00000000 00000000 00000000 3e000300 feff0900 06000000 00000000 00000000 01000000 45000000 00000000 00100000 feffffff 00000000 feffffff 00000000 44000000 ffffffff ffffffff ffffffff ffffffff ........ >
This is the code for the download:
let url = NSURL(string: "https://s3-eu-west-1.amazonaws.com/schooly/handasaim/news/f_1312015203355628490.xls")
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
if error != nil {
println("There was an error")
} else {
println(data)
}
})
If the download code is correct how can i decode the data and access it?
If it's wrong what is the correct way to download an Excel file and access the data?
Solution 1:[1]
I'd recommend you to use Alamofire to download the file which is third-party library like AFNetworking. Then you can have the downloaded file at default DocumentDirectory
.
Here you get the framework Alamofire and set it up with your project.
Download function is like this,
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, "https://s3-eu-west-1.amazonaws.com/schooly/handasaim/news/f_1312015203355628490.xls", destination)
Then you get the hard part. try these. but in Objective-C
Solution 2:[2]
I'm also not answering your question, but if you're just using plain text in your excel file, consider saving your text as a plain text CSV (comma delimited) or a tab delimited text file. Plain text will be easy to parse in iOS, and because there is no SDK for encoding/decoding excel files that I know of you may not want to continue down the path you're on. Excel is a proprietary format.
Once you have downloaded the plain text, you'd split on return characters, then you'd split on tabs (or commas) to get an mxn array of text. For example, something like:
let allRows = text.split {$0 == "\n"}
for row in allRows
{
allColumns = row.split {$0 == "\t"} //if your text file is tab delimited
//.. do something with your columns for this row ..
}
I havent tested this code but hopefully you get the idea
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MacKa |
Solution 2 | user1709076 |