DEV Community

Pilalo Jovanitho
Pilalo Jovanitho

Posted on

C#: How to Export Excel Data to Text Files

As data migration demands surge due to AI integration and cloud migrations, developers frequently need to C# Excel to TXT for legacy systems or text-only pipelines. Imagine processing thousands of financial spreadsheets into plain text for compliance audits—traditional methods fail under scale. This guide delivers a precise solution using Spire.XLS for .NET to convert Excel to a text file efficiently.


The Need for Excel to TXT Conversion

Converting Excel (.xlsx/.xls) to TXT is essential for:

  • Data Interoperability: Legacy apps, scripts, or databases requiring delimited text.
  • Analytics Prep: Feeding into tools like Python's pandas or R without format dependencies.
  • Batch Automation: ERP migration projects requiring bulk TXT exports have seen significant growth.

Key Challenges:

  • Retaining formulas, merged cells, and Unicode.
  • Scalability for files >500MB amid .NET 8's memory optimizations.
  • Avoiding Excel Interop's server instability.

Libraries like Spire.XLS for .NET resolve these without Excel installation.


Spire.XLS for .NET: Top Choice for C# Excel to TXT

Spire.XLS for .NET provides native TXT export, supporting Excel 97-, macros, and streaming. Compare with peers:

Feature/Library Spire.XLS EPPlus NPOI ClosedXML
TXT Export Native Custom None None
Free Limits 150 rows 10 rows None None
Large Files Stream Partial Weak Fair
.NET 8 Full Full Partial Full
Macros Support Yes No Limited No

Install via NuGet: dotnet add package Spire.XLS—ideal for production C# Excel to TXT.


Step-by-Step: Convert Excel to Text File in C

Prerequisites

Target .NET 8 console app for optimal performance.

dotnet new console -n ExcelToTxtApp
cd ExcelToTxtApp
dotnet add package Spire.XLS
Enter fullscreen mode Exit fullscreen mode

1. Basic Single-Sheet Conversion

using Spire.Xls;
using System.Text;

class Program
{
    static void Main()
    {
        Workbook workbook = new Workbook();
        workbook.LoadFromFile("input.xlsx");

        Worksheet sheet = workbook.Worksheets[0];
        sheet.SaveToFile("output.txt", " ", Encoding.UTF8);  // Space-delimited

        workbook.Dispose();
        Console.WriteLine("C# Excel to TXT complete.");
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Multi-Sheet and Custom Delimiters

Workbook workbook = new Workbook();
workbook.LoadFromFile("multi.xlsx");

for (int i = 0; i < workbook.Worksheets.Count; i++)
{
    Worksheet sheet = workbook.Worksheets[i];
    string fileName = $"Sheet{i + 1}.txt";
    sheet.SaveToFile(fileName, ",", Encoding.UTF8);  // CSV-style
}

workbook.Dispose();
Enter fullscreen mode Exit fullscreen mode

3. Stream Large Files (Memory-Efficient)

using (FileStream fs = new FileStream("large.xlsx", FileMode.Open))
{
    Workbook workbook = new Workbook();
    workbook.LoadFromStream(fs);

    workbook.Worksheets[0].SaveToFile("streamed.txt", "\t", Encoding.UTF8);
    workbook.Dispose();
}
Enter fullscreen mode Exit fullscreen mode

4. Legacy .xls with Macros

Workbook workbook = new Workbook();
workbook.LoadFromFile("macros.xls", ExcelVersion.Version97to2003);

Worksheet sheet = workbook.Worksheets[0];
sheet.Range["A5"].Text = "Updated Value";  // Optional edit
sheet.SaveToFile("macro-txt.txt", " ", Encoding.ASCII);

workbook.Dispose();
Enter fullscreen mode Exit fullscreen mode

Run: dotnet run—outputs clean TXT with preserved structure.


Best Practices and Performance Optimization

  • Error Handling:
  try
  {
      var workbook = new Workbook();
      workbook.LoadFromFile("file.xlsx");
      // Process...
  }
  catch (Exception ex)
  {
      Console.WriteLine($"Conversion failed: {ex.Message}");
  }
  finally
  {
      workbook?.Dispose();
  }
Enter fullscreen mode Exit fullscreen mode
  • Tips:
    | Category | Best Practice |
    |----------------|-----------------------------------|
    | Performance| Stream + .NET 8 AOT (2x faster) |
    | Encoding | UTF8 default; ASCII for legacy |
    | Delimiters | "," or "\t" for parsers |
    | Scale | Process sheets independently |

  • Use Cases:

    • Audit logs to TXT for forensics.
    • ML datasets from Excel.
    • API endpoints exposing TXT downloads.

Spire.XLS handles 1M+ rows in seconds, outperforming Interop by 10x.


Wrap-Up

Converting Excel files to plain text is a common task for data processing. Spire.XLS for .NET provides a simple API to read Excel workbooks and export them as TXT files with minimal C# code. It handles XLS/XLSX formats without requiring Microsoft Office, making it ideal for data migration and system integration.

Action: Integrate Spire.XLS via NuGet and test with your files. Visit e-iceblue.com for pro features.

Top comments (0)