Export a sheet from the old binary .xls format (Excel 97–2003).
dotnet add package RecursiveAI.OneDocument.SpreadsheetsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Open("stock.xls"); book.SaveAs("stock.csv"); Console.Write(File.ReadAllText("stock.csv"));
Unzip the example, 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.
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.
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:
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.
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"));
Item,Quantity Paper,1
Related recipes
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 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 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:
CsvDocument.Write(book.Sheets["Returns"], output, new CsvWriteOptions { Formatted = false, ByteOrderMark = false });
Keep the .xls if you need the original workbook features.