How to Validate an Excel XLSX File using C/C++
If we’re designing applications to process and store Excel XLSX files, we want to be completely sure our spreadsheets are valid and secure before we commit them to a database.
Thankfully, using the below code, we can easily incorporate an XSLX document validation step which detects errors within the file structure and identifies if the file is password-protected. Equipped with this information, we can root our invalid spreadsheets and potential security concerns before storing such files in a sensitive location.
To structure our API call in C/C++, let’s start by installing libcurl:
libcurl/7.75.0
Then let’s include the below ready-to-run code examples in our file & supply a free-tier Cloudmersive API key to authorize our request:
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/validate/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);
We can now validate up to 800 files per month for free with no commitment. Quick and easy!