How to Convert Excel to Text in C#, VB.NET

This article aims at introducing an easy solution to convert Excel (xls/xlsx) to Text in C# and VB.NET by a powerful and independent Excel .NET component, called Spire.Xls. Meanwhile, this Excel API supports for converting Excel (xls/xlsx) to PDF, HTML, Image, CSV, XML, etc, without Microsoft Excel installed on the machine. Download it from here to have a try.

Step 1: Create an instance of Spire.Xls.Document.

[C#]
Workbook workbook = new Workbook();

Step 2: Load the file base on a specified file name.

[C#]
workbook.LoadFromFile(@"..\ExceltoTxt.xls");

Step 3: Get the first worksheet of the Worksheet.

[C#]
Worksheet sheet = workbook.Worksheets[0];

Step 4: Save the document object to a Txt file.

[C#]
sheet.SaveToFile("ExceltoTxt.txt", " ", Encoding.UTF8);

Effect Screenshot:

Excel_to_Txt

Excel_to_Txt

Sample code from Excel to Text in C#:

[C#]
using Spire.Xls;
namespace Excel_to_Txt
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\ExceltoTxt.xls");
            Worksheet sheet = workbook.Worksheets[0];
            sheet.SaveToFile("ExceltoTxt.txt", ",", Encoding.UTF8);
        }
    }
}   

Sample code from Excel to Text in VB.NET:

[VB.NET]
Imports Spire.Xls
Namespace Excel_to_Txt
       Friend Class Program
              Shared Sub Main(ByVal args() As String)
                     Dim workbook As New Workbook()
                     workbook.LoadFromFile("..\ExceltoTxt.xls ")
                     Dim sheet As Worksheet = workbook.Worksheets(0)
                     sheet.SaveToFile("ExceltoTxt.txt", ",", Encoding.UTF8)
              End Sub
       End Class
End Namespace