Convert Multiple CSV Files to Excel in Swift

Cloudmersive
2 min readAug 2, 2021

--

Excel and CSV are two of the most popular data storage formats, but that is essentially where the similarities end. Due to its straightforward design, CSV is often the primary choice for developers during projects. However, if you need more complex features such as formulas and graphs, Excel is the go-to program; Excel is also easier for the average user to understand. By utilizing the following API in Swift, you will be able to instantly convert multiple CSV files to a single XLSX spreadsheet, with one worksheet corresponding to each file.

Let’s get the function call rolling by adding the CSV files (up to 10) and the API key to the below example code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = [
[
"key": "inputFile1",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile2",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile3",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile4",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile5",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile6",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile7",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile8",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile9",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile10",
"src": "/path/to/file",
"type": "file"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if param["contentType"] != nil {
body += "\r\nContent-Type: \(param["contentType"] as! String)"
}
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
body += "\r\n\r\n\(paramValue)\r\n"
} else {
let paramSrc = param["src"] as! String
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data
let fileContent = String(data: fileData, encoding: .utf8)!
body += "; filename=\"\(paramSrc)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/convert/csv/multi/to/xlsx")!,timeoutInterval: Double.infinity)
request.addValue("<string>", forHTTPHeaderField: "worksheetNames")
request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

Your new XLSX file will be ready for download in no time! If you need to obtain an API key, you can do so by registering for a free account on the Cloudmersive website; this provides 800 calls/month across our extensive library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet