How to Convert Excel to TXT with C#

Text is one frequently used file format, which is formed with texts in lots of columns. It is attached in Windows operation system. Therefore, it is easy to create and open a TXT file.

Because its simple structure, TXT file is widely used to save text information. It is easy to recover if there are some errors in file. Also, txt file has small size and easy to save. However, there are not so much format tools provided, just including font type, color and size setting.

Sometimes, we need to use txt file to save data to avoid some problems which exists because of format. We can export data to txt file directly or convert other format files which save data to txt. In this post, I will show way to convert Excel to txt with C#.

In my example, I want to convert one Excel file about sales report to txt file. And I use Spire.XLS in this method. So, I add its DLL file as reference.

Sales Report

Steps:

  1. Load the file which I want to convert to txt from my computer and initialize the current worksheet.
  2. Save this file to txt and launch this file.

Coding:

using System;
using Spire.Xls;

namespace ExceltoTXT
{
    class toTXT
    {
        static void Main(string[] args)

        {

            //Load File and Initialize Worksheet
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@”D:\work\My Documents\Sales Report.xlxs”,ExcelVersion.Version2010);
            Worksheet sheet = workbook.Worksheets[0];

            //Save to TXT
            sheet.SaveToFile(“toTXT.txt”, “,”);

            //Launch TXT File
            System.Diagnostics.Process.Start(“toTXT.txt”);
        }
    }
}

Result:

————————————————————————————————————–

Click Here to LEARN MORE about Spire.XLS

Click Here to DOWNLOAD Spire.XLS

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

1 thought on “How to Convert Excel to TXT with C#”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s