Table of Contents

Class TwSpreadsheetExport

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

Writes a TwSpreadsheet workbook out as an Excel file or as CSV. TwSpreadsheet uses it for its export menu, and it can be called on its own, for example to build a file on the server from stored sheets.

public static class TwSpreadsheetExport
Inheritance
TwSpreadsheetExport
Inherited Members

Remarks

Formulas are worked out with the same engine as the component, so a file built on the server holds the same results the user saw.

Fields

CsvContentType

The media type of a CSV file.

public const string CsvContentType = "text/csv"

Field Value

string

ExcelContentType

The media type of an Excel workbook.

public const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

Field Value

string

Methods

Export(IReadOnlyList<Sheet>, SpreadsheetExportFormat, string, int, CultureInfo?)

Writes the workbook as a file in format, with the file name, extension and media type to save it with.

public static ExportFile Export(IReadOnlyList<Sheet> sheets, SpreadsheetExportFormat format, string fileName = "workbook", int sheetIndex = 0, CultureInfo? culture = null)

Parameters

sheets IReadOnlyList<Sheet>

The sheets, in tab order.

format SpreadsheetExportFormat

The format.

fileName string

The file name without its extension. Characters a file name cannot hold are replaced. Defaults to workbook.

sheetIndex int

For CSV, which holds one sheet, the index of the sheet to write.

culture CultureInfo

The culture whose currency symbol currency cells are saved with, or null for the current culture.

Returns

ExportFile

The file.

ExtensionOf(SpreadsheetExportFormat)

Gets the extension a format's files are saved with, such as .xlsx.

public static string ExtensionOf(SpreadsheetExportFormat format)

Parameters

format SpreadsheetExportFormat

The format.

Returns

string

The extension, with its dot.

ToCsv(IReadOnlyList<Sheet>, int)

Writes one sheet as CSV text, from A1 to the last row and column that hold something. Formulas are written as their results, numbers in an invariant format so any spreadsheet reads them the same way, and text that a spreadsheet would run as a formula is prefixed with an apostrophe.

public static string ToCsv(IReadOnlyList<Sheet> sheets, int sheetIndex = 0)

Parameters

sheets IReadOnlyList<Sheet>

The sheets, so formulas that refer to other sheets can be worked out.

sheetIndex int

The index of the sheet to write. Defaults to the first.

Returns

string

The CSV text, with a line break after every row.

ToExcel(IReadOnlyList<Sheet>, CultureInfo?)

Writes the workbook as an Excel file with one worksheet per sheet. Formulas are kept, with their results saved alongside, and each cell keeps its bold, italic, underline, alignment and number format.

public static byte[] ToExcel(IReadOnlyList<Sheet> sheets, CultureInfo? culture = null)

Parameters

sheets IReadOnlyList<Sheet>

The sheets, in tab order.

culture CultureInfo

The culture whose currency symbol currency cells are saved with, or null for the current culture.

Returns

byte[]

The contents of an .xlsx file.