How to Validate an Excel Document using Java
Data validation serves an important role during the accumulation of objects for storage. It helps us ensure that invalid documents don’t accidentally make it to our database as corrupt files that can’t be opened, and it can create a layer of security by the same token, ensuring various scripts, macros and executables don’t sneak by as invalid files.
Our Excel Validation API will specifically check that all XLSX files are valid, and it will provide useful information about any invalid files including an error count, warning count, and a description of both. Further, it will let you know if the document was password protected, which is always cause for concern. Below, I’ll demonstrate how you can easily structure an API call in Java to take advantage of this validation service.
Let’s begin by installing the SDK with Maven. First, include a reference in the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, include a reference in the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Finally, include the following imports at the top of your file, then call the validation function:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ValidateDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentXlsxValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentXlsxValidation");
e.printStackTrace();
}
You’ll only need to satisfy two parameters here, which include your input file for the operation, and your Cloudmersive API key. You can get a key by registering a free account on our website, which will provide you with a limit of 800 API calls per month.