How to validate an Excel XLSX Spreadsheet using C# in .NET Framework

We have two possibilities for today’s post: We could spend hours going through the code necessary to validate spreadsheets from scratch, or we could spend a couple minutes accomplishing the same goal using a free API. I think we will go with the second option. Let’s dive in.

First we need to install the API client using the Package Manager console and the following snippet:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.3.4

Next, simply call ValidateDocumentXlsxValidation with the file in question:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ValidateDocumentXlsxValidationExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ValidateDocumentApi();
var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.
try
{
// Validate a Excel document (XLSX)
DocumentValidationResult result = apiInstance.ValidateDocumentXlsxValidation(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ValidateDocumentApi.ValidateDocumentXlsxValidation: " + e.Message );
}
}
}
}

Seriously, it’s that easy. Your output will look like this:

{
"DocumentIsValid": true,
"ErrorCount": 0,
"WarningCount": 0,
"ErrorsAndWarnings": [
{
"Description": "string",
"Path": "string",
"Uri": "string",
"IsError": true
}
]
}

Our API also supports validating DOCX and PPTX files as well using similar functions, along with format conversion, file editing, and much more.

Image for post
Image for post

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