← All articles

Exporting typed data without confusing it with presentation

Keep report values typed until the final export step. A formatted currency string is less useful for later spreadsheet calculations than a decimal value.

t.Turgay
Exporting typed data without confusing it with presentation

An exported report often becomes input to someone else's calculation. Writing the attractive display text “1.234,50 TL” into every cell may cause trouble when the user tries to sum the column. I treat the underlying value, its visual format and the actual file format as separate parts of an export design.

Start with typed report data

The small example derived from reporting patterns in my archive uses an integer ID and a decimal amount. DataTable preserves those types. Currency symbols, grouping separators and localized decimal marks are deferred until output formatting. This .NET 10 console example demonstrates that boundary before involving a workbook library.

using System.Data;
using System.Globalization;
var table = new DataTable();
table.Columns.Add("Id", typeof(int)); table.Columns.Add("Amount", typeof(decimal));
table.Rows.Add(1, 12.50m);
Console.WriteLine("Id;Amount");
foreach (DataRow row in table.Rows)
    Console.WriteLine($"{row.Field<int>("Id")};{row.Field<decimal>("Amount").ToString("0.00", CultureInfo.InvariantCulture)}");

The expected output is the header Id;Amount followed by 1;12.50. InvariantCulture deliberately produces a predictable textual representation. The result is delimited text, not an Excel workbook. Renaming it with an .xlsx extension does not create workbook parts, worksheets or their relationships.

Represent a number as a number in XLSX

A numeric cell should contain the value 12.50, with its two-decimal or currency appearance supplied by a number format. Changing the format should not change the stored amount. At the Open XML level, a minimal numeric cell resembles the following fragment. It belongs inside the workbook package; this XML fragment alone is not an independently usable spreadsheet file.

<c r="B2" t="n">
  <v>12.50</v>
</c>

The same distinction applies when using a higher-level XLSX library: assigning the cell value and assigning its number format should remain separate operations. A shortcut that turns every object into a string can produce a plausible document while discarding numeric and date semantics. Headers, column order and the representation of null values also need an explicit report contract.

A general CSV writer needs more than interpolation

Console.WriteLine is sufficient to illustrate these two numeric fields. Free text introduces delimiters, quotes and embedded line breaks. A value such as “Ankara; central” must not accidentally become two columns. Quoting and escaping need to follow the chosen CSV format, and encoding and delimiter choices must match the intended reader.

User text opened in a spreadsheet may also be interpreted as a formula. In XLSX, assigning an explicit text cell can preserve the intended type. CSV needs a deliberate policy compatible with the target reader. Quoting alone should not be assumed to disable formula evaluation in every spreadsheet application; test the produced file in the environment where it will actually be opened.

Define the exported scope

The export query must apply the same authorization boundary as other data access. “Export all filtered records” is also different from “export the currently visible page”. If the screen displays five rows while the report includes several thousand, the label and query need to agree about that behavior. Exporting the in-memory grid contents should not accidentally pretend to represent the entire result.

Holding all records in a DataTable and then creating another complete workbook representation can multiply memory usage. Streaming writers, temporary files and background generation are options for larger reports, depending on the chosen library and volume. Measure data reading and download behavior alongside file generation time.

Verify that the report remains usable for calculations

Include zero, negative values, nulls, Unicode text, delimiters and formula-like strings in the test input. With actual XLSX output, numeric cells should still support a SUM calculation. For CSV, read the generated file back through a proper parser and compare the fields. A correct extension and successful opening are useful checks; preserving the meaning of the data is the real requirement.

Open XML cell model; Formatting .NET values.

✳ You have reached the end of this orbit.
Explore more articles →
turgay.A little code, a few pixels, a lot of curiosity.© 2026 · Made on Earth ✳