How to Split Excel XLSX Files into Separate Spreadsheet Documents using C/C++
Retrieving worksheets from an Excel document programmatically is pretty simple: we just need the right library or API solution to access the contents of the file correctly.
Using the below code, we can take advantage of a free API that does exactly that. This service creates one Excel document per page of the original file and returns those documents as their own separate objects.
We can structure our API call in two quick steps (one, really). Let’s start by installing libcurl in our C/C++ project:
libcurl/7.75.0
Then we can copy the ready-to-run code examples below into our file:
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/split/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, "returnDocumentContents: <boolean>");
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);
We can complete our requests by supplying a free-tier API key in the authorization field. All set!