How to validate an Excel XLSX Spreadsheet in Java

Any time you work with large quantities of Excel files, it is always prudent to vet these files for problems before you feed them into any sort of system. Manually accomplishing this is generally too cost prohibitive, while setting up a validation system from scratch is quite an undertaking. Instead, how about we look at how to harness an API to quickly accomplish this task in a matter of minutes.

First off, let us reference our repository in our Maven POM file:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, reference the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v2.32</version>
</dependency>
</dependencies>

And now we are ready to call validateDocumentXlsxValidation:

// 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/file.txt"); // 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();
}

We will be returned with whether the document is valid and any potential errors if it is invalid.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store