How to Convert an Excel XLSX Spreadsheet to Plain Text in C# .NET Framework
Plain text content is simple and gives us a lot of flexibility. Unfortunately, it can be surprisingly hard to extract plain text from rich formatted documents without writing an excessive amount of code.
Thankfully, using the below code, we can take advantage of a free API that handles plain text extraction for us. We can use this service to get plain text content from our Excel XLSX spreadsheets; our response object will contain a simple TextResult that holds all text contents from our spreadsheet (includes all worksheets).
We can begin by installing the SDK. Let’s run the following command in our Package Manager console to install via NuGet:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Next, let’s get ready to authorize our API call by obtaining a free-tier API key. These allow a limit of 800 API calls per month with no commitments (our total will simply reset the following month once we reach that limit).
Finally, let’s use the ready-to-run code examples below to structure our request, and let’s paste our API key in the appropriate snippet. We can use multipart/form-data in this request:
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 ConvertDocumentXlsxToTxtExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
try
{
// Convert Excel XLSX Spreadsheet to Text (txt)
TextConversionResult result = apiInstance.ConvertDocumentXlsxToTxt(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentXlsxToTxt: " + e.Message );
}
}
}
}
That’s all there is to it — now we can easily extract plain text content from our Excel spreadsheets using minimal code.