C#/VB.NET: Convert Word or Excel Files to Text
Compared with Word or Excel files, text files are smaller in size because they only consist of plain text without formatting. Besides, text files are also cross-platform as they are compatible with almost all applications. Due to such reasons, you may need to convert Word or Excel files to Text. In this article, I will introduce how to convert Word or Excel files to text using C# and VB.NET.
Installation
To convert Word and Excel files to text, this article uses Spire.Office for .NET.
You can install the API via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then executing the following command:
PM> Install-Package Spire.OfficeConvert Word Doc or Docx to Text using C# and VB.NET
Spire.Office provides a Document class to work with Word documents. This class exposes a Document.SaveToTxt() method to convert a Word Doc or Docx document to text.
The following steps show you how to convert a Word document to text:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Save the document as a text file using Document.SaveToTxt() method.
C#
using Spire.Doc;
using System.Text;
namespace ConvertWordToText
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load the Word document
document.LoadFromFile("Sample.docx");
//Convert the document to text
document.SaveToTxt("WordToText.txt", Encoding.UTF8);
}
}
}VB.NET
Imports Spire.Doc
Imports System.Text
Namespace ConvertWordToText
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load the Word document
document.LoadFromFile("Sample.docx")
'Convert the document to text
document.SaveToTxt("WordToText.txt", Encoding.UTF8)
End Sub
End Class
End NamespaceThe input Word file:
The converted text file:
Convert Excel XLS or XLSX to Text using C# and VB.NET
The Workbook class is used to work with Excel files. You can convert a specific worksheet or all worksheets in an Excel file to text by getting the specific worksheet or looping through each worksheet in the file, then call Worksheet.SaveToFile() method to convert the worksheet to text.
Get Alice Yang’s stories in your inbox
Join Medium for free to get updates from this writer.
The following steps show you how to convert a specific Excel worksheet to text:
- Initialize an instance of Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the desired worksheet by its index through Workbook.Worksheets[sheetIndex] property.
- Convert the worksheet to text using Worksheet.SaveToFile() method.
C#
using Spire.Xls;
using System.Text;
namespace ConvertExcelToText
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile("Sample.xlsx");
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Save the worksheet to text
worksheet.SaveToFile("ExcelToText.txt", " ", Encoding.UTF8);
}
}
}VB.NET
Imports Spire.Xls
Imports System.Text
Namespace ConvertExcelToText
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Workbook instance
Dim workbook As Workbook = New Workbook()
'Load the Excel file
workbook.LoadFromFile("Sample.xlsx")
'Get the first worksheet
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Save the worksheet to text
worksheet.SaveToFile("ExcelToText.txt", " ", Encoding.UTF8)
End Sub
End Class
End NamespaceThe input Excel file:
The converted text file:









