Recursive AI

Convert Excel 2003 XML to XLSX 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.

Convert Excel 2003 SpreadsheetML to .xlsx. OneDocument can read Excel 2003 XML but not save to it.

Terminal
dotnet add package RecursiveAI.OneDocument.Spreadsheets

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Spreadsheets;
using var book = Workbook.Open("old.xml");
book.SaveAs("new.xlsx");

Unzip the example, then dotnet run.

The three-line snippet reads old.xml from the download and writes new.xlsx without printing anything. Here’s the complete input file:

XML
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <Worksheet ss:Name="Sales">
    <Table>
      <Row>
        <Cell><Data ss:Type="String">North</Data></Cell>
        <Cell><Data ss:Type="Number">42</Data></Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

The full program

The downloaded program prints the import loss report, then reopens new.xlsx and prints the first two cells with their types.

C#
using RecursiveAI.OneDocument.Spreadsheets;
 
var input = args.Length == 0 ? "old.xml" : args[0];
using var workbook = Workbook.Open(input);
Console.WriteLine($"Import loss: {workbook.ImportLoss?.ToString() ?? "none reported"}");
workbook.SaveAs("new.xlsx");
 
using var saved = Workbook.Open("new.xlsx");
var sheet = saved.Sheets[0];
foreach (var address in new[] { "A1", "B1" })
{
    var cell = sheet[address];
    Console.WriteLine($"{address}: {cell.Text} ({cell.Value.Kind})");
}
Text
Import loss: no loss
A1: North (Text)
B1: 42 (Number)

Malformed Excel 2003 XML can throw OfficeException during open. For files that do open, read Workbook.ImportLoss on the imported workbook before discarding it; items such as manual page breaks can be left behind.

Variations

To print each reported loss on its own line, replace the Import loss: print statement in the full program:

C#
if (workbook.ImportLoss is { } loss)
    foreach (var item in loss.Items)
        Console.WriteLine($"{item.Code}: {item.Count}");

The included sample prints no loss lines. To run the downloaded project on another Excel 2003 XML file, supply its path: dotnet run -- "my workbook.xml".

The short answer

Open the Excel 2003 XML file with Workbook.Open, then call SaveAs with an .xlsx path. OneDocument converts the input to an in-memory XLSX package when it opens the file; it does not write back to the 2003 XML format. Inspect ImportLoss on the source workbook for features that couldn’t be mapped.

How can I tell whether my Excel XML file can be converted to XLSX?

Look at the root element. In the example the root is Workbook in the urn:schemas-microsoft-com:office:spreadsheet namespace. OneDocument identifies the format by content; the format docs also list the Excel.Sheet processing instruction. A file with an .xls name can still contain this XML, but a true Excel binary XLS file is a different format. If the file is arbitrary XML, such as invoices with custom elements, this recipe won’t decide which elements should be rows and columns.

Is Excel 2003 XML the same as XLSX?

No. Excel 2003 SpreadsheetML stores workbook data in an XML document. XLSX stores a workbook across parts in an Open XML package. Renaming the file does not build those parts. The operation that does so here is Workbook.Open; SaveAs writes the converted package to disk. The sample’s ss:Type attributes tell the importer that “North” is text and 42 is a number, rather than two strings that happen to look different.

Will formulas survive the conversion from Excel 2003 XML?

The import mapping translates supported R1C1 formula text (row and column references) to A1 formula text and keeps the stored result. Open doesn’t recalculate it. If the formula parser rejects the expression, the import reports sml2003.bad-formula and leaves the stored value as a constant. If formulas matter to you, check both the displayed result and the formula, not just whether the cell looks right.

Layout and worksheet features also need a check. The importer approximates column widths because the formats use different units. Named cell styles become plain cell formats, and manual page breaks and printer settings aren’t carried over. The tiny sample has none of those features; “no loss” for that sample does not mean a larger workbook will look or behave identically.

All onedocument recipes