How to Convert CSV to Excel in Swift

2 min readJun 11, 2021


The Comma-Separated Values (CSV) format is an excellent option for data storage; its simple design allows for creation in almost any text editor, and it is also easy to parse. However, if you frequently have CSV files that need to be shared with partners or customers, converting the CSV file into the more presentable Excel format may be the best move. In this brief tutorial, we will demonstrate how you can use an API in Swift to instantly convert CSV to XLSX; no additional code-writing needed.

To call the API function, you will need to input the CSV file and API key into the following code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
var semaphore = DispatchSemaphore (value: 0)let parameters = [
"key": "inputFile",
"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 = .utf8)
var request = URLRequest(url: URL(string: "")!,timeoutInterval: Double.infinity)
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))
print(String(data: data, encoding: .utf8)!)

That’s it! You can retrieve the API Key from the Cloudmersive website at no cost and with no commitment; this will provide you with 800 monthly calls across any of our APIs.




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