---
title: "Convert XLS to CSV in C#"
url: https://recursiveai.org/onedocument/cookbook/convert-xls-to-csv/
description: "Convert legacy Excel .xls files to CSV in C# with OneDocument. Export the first visible worksheet or choose a sheet by name. Includes a runnable .xls sample."
version: "1.0.0"
---

# Convert XLS to CSV in C#

**OneDocument** is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDocument.Spreadsheets) · [GitHub](https://github.com/recursive-ai-oss/onedocument-csharp) · [Docs](https://recursiveai.org/onedocument/docs/) · [Download example (.zip)](https://recursiveai.org/onedocument/cookbook/convert-xls-to-csv/convert-xls-to-csv.zip)

Export a sheet from the old binary .xls format (Excel 97–2003).

```shell
dotnet add package RecursiveAI.OneDocument.Spreadsheets
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using var book = Workbook.Open("stock.xls");
book.SaveAs("stock.csv");
Console.Write(File.ReadAllText("stock.csv"));
```

Unzip [the example](https://recursiveai.org/onedocument/cookbook/convert-xls-to-csv/convert-xls-to-csv.zip), then `dotnet run`.

This writes `stock.csv` from the first visible sheet and prints it. The download includes `stock.xls`. Its first sheet contains the rows shown below. The second sheet, `Returns`, contains the same header and one row: Paper, 1.

```text
Item,Quantity
"Pens, blue",12
Paper,5
```

## The full program

Run the downloaded program as is, or pass your own paths: `dotnet run -- old.xls export.csv`.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

string input = args.Length > 0 ? args[0] : "stock.xls";
string output = args.Length > 1 ? args[1] : "stock.csv";

using var book = Workbook.Open(input);
book.SaveAs(output);
Console.Write(File.ReadAllText(output));
```

With the supplied workbook, `dotnet run` prints:

```text
Item,Quantity
"Pens, blue",12
Paper,5
```

If the input isn’t recognized, `Workbook.Open` throws `OfficeFormatException` before the program reaches `SaveAs`.

## Variations

### Export a sheet by name

Pass the worksheet itself to `CsvDocument.Write` rather than saving the whole workbook. Here, only `Returns` goes into `returns.csv`.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;
using RecursiveAI.OneDocument.Spreadsheets.Csv;

using var book = Workbook.Open("stock.xls");
using (var output = File.Create("returns.csv"))
    CsvDocument.Write(book.Sheets["Returns"], output);
Console.Write(File.ReadAllText("returns.csv"));
```

```text
Item,Quantity
Paper,1
```

## Related recipes

- [Read an Excel file in C#](https://recursiveai.org/onedocument/cookbook/read-excel-file/)
- [Convert XLSX to CSV in C#](https://recursiveai.org/onedocument/cookbook/convert-xlsx-to-csv/)
- [Import CSV to a workbook in C#](https://recursiveai.org/onedocument/cookbook/import-csv-to-workbook/)

## The short answer

Open the `.xls` file with `Workbook.Open`, then call `SaveAs` with a `.csv` path. The input can be a real Excel 97–2003 binary workbook. You don’t need to open it in Excel or convert it to `.xlsx` first. The output is one worksheet, not the whole workbook. Choose the sheet by name if the one you need isn’t the first visible sheet.

## How do I convert XLS to CSV without opening it in Excel?

Run the conversion as part of your .NET application. `Workbook.Open` identifies the format from the contents, not the filename. The sample is an old binary `.xls` file, not an XLSX or Excel 2003 XML file with a different name. Renaming a workbook to `.csv` doesn’t convert it: `SaveAs` writes new data in the format selected by the output extension. See the [legacy Excel format notes](https://recursiveai.org/onedocument/docs/xls/) for how OneDocument reads BIFF records.

## Are XLS and CSV the same?

No. The workbook can hold several sheets, cell styles, merged ranges, and formulas. A CSV is text with one record per row and one field per cell over the exported range. Commas inside a value are quoted: `"Pens, blue"` reaches a CSV reader as one field, not two. Use a CSV parser rather than splitting each line at every comma.

There are two places where information can be lost. On import to OneDocument’s workbook model, features such as pivot tables, chart records, and VBA are not carried. For that step, `Workbook.ImportLoss` lists what the import couldn’t carry. Then the CSV export flattens the selected worksheet: it doesn’t write workbook styling or formula expressions. OneDocument uses the cached formula results stored in the input; opening the file doesn’t recalculate them. If the cache is stale, the CSV can contain an old result.

By default, the [CSV writer](https://recursiveai.org/onedocument/docs/csv/) formats numbers as displayed, so a date is written as date text rather than as the underlying Excel serial number. It also writes a UTF-8 byte-order mark. Set `Formatted = false` for raw numbers and `ByteOrderMark = false` for UTF-8 without the mark (BOM). In the named-sheet variation, replace the `CsvDocument.Write` call with:

```csharp
CsvDocument.Write(book.Sheets["Returns"], output,
    new CsvWriteOptions { Formatted = false, ByteOrderMark = false });
```

Keep the `.xls` if you need the original workbook features.
