Convert a CSV File to XLSX in C/C++
Creating CSV files is a straightforward way to input large data sets without any formatting frills or restrictive program specifications. However, if this data needs to be shared with clients or partners, CSV does not provide the most user-friendly experience. In this brief tutorial, we will demonstrate how you can automatically convert a CSV file to the easy-to-read and familiar XLSX format by utilizing an API in C/C++.
Our first step is to Install libcurl into our C/C++ project:
libcurl/7.75.0
Next, we can call the conversion function by inputting the CSV file and your API key into the following code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/csv/to/xlsx");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
This will provide you with a shareable and edit-friendly Excel document. If you need to obtain an API key, visit the Cloudmersive website to register for a free account; this provides 800 monthly calls across our library of APIs.