How to validate an Excel XLSX Spreadsheet in PHP
For this task, we want to determine if an input Excel XLSX file is indeed a fully-valid Excel file.
First, we need to add a reference to the library:
"require": {
"cloudmersive/cloudmersive_document_convert_api_client": "^1.4",
}
Now, all we need to do is call the validateDocumentXlsxValidation function:
<?php
require_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');$apiInstance = new Swagger\Client\Api\ValidateDocumentApi(
new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/file.xlsx"; // \SplFileObject | Input file to perform the operation on.try {
$result = $apiInstance->validateDocumentXlsxValidation($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ValidateDocumentApi->validateDocumentXlsxValidation: ', $e->getMessage(), PHP_EOL;
}
?>
The result object has a property DocumentIsValid which will be true if the doucment is valid. If the document is not a valid Excel XLSX file then this value will be false and the ErrorsAndWarnings field will be populated with errors (block usage or validity of the file) and warnings (indicate a possible problem but the document is still potentially valid) in the file. Documents with warnings but no errors will still return as valid.
That’s it! That’s all there was to it.