Spreadalonia is a library providing a simple spreadsheet control for Avalonia, with support for some basic features.
The library is released under the LGPLv3 licence.
The library targets .NET Standard 2.0, thus it can be used in projects that target .NET Standard 2.0+ and .NET Core 2.0+. Currently, it only supports Avalonia 0.10, but support for Avalonia 11 will come at some point.
To use the library in your project, you should install the Spreadalonia Nuget package. The library provides the Spreadalonia.Spreadsheet
control, which you can include in an Avalonia Window
.
This repository also contains a very simple demo projects, containing a window with a spreadsheet control.
You will need to add the relevant using
directive (in C# code) or the XML namespace (in the XAML code). You can then add the Spreadsheet
controls from the Spreadalonia namespace. For example
<Window ...
xmlns:spreadalonia="clr-namespace:Spreadalonia;assembly=Spreadalonia">
...
<spreadalonia:Spreadsheet></spreadalonia:Spreadsheet>
...
</Window>
The spreadsheet implements the following basic features:
- Cell value editing
- Keyboard navigation and shortcuts
- Row/column resizing and AutoFit
- Context menu
- Copy, cut and paste
- Auto fill
- Moving cells/columns around
- Inserting and deleting rows and columns
- Undo/redo
- Preview for cells containing colour values
This library provides only the basic spreadsheet control, without any of the external controls that it might make sense for a spreadsheet program to have. You should implement these yourself (based on the UI style of your application).
For example, you may wish to include the following:
- Cell formatting (font family, font style, font weight, colour, text alignment)
- Data sorting
- Search/replace
- Transposing/transforming cells
- ...
The Spreadsheet
control has the following useful properties and methods:
-
Dictionary<(int, int), string> Data { get; }
: this property provides access to the data contained in the spreadsheet. The data is stored in a dictionary, where the key represents the coordinates of a cell (the first element is the column and the second element is the row, both starting from 0), and the value is stored as astring
.Note: in principle, you could change the data in the spreadsheet by adding/removing/changing values in the dictionary instance returned by this property. Please do not do this, as you will mess up the Undo/Redo stack.
-
void SetData(IEnumerable<KeyValuePair<(int, int), string>> data)
: sets the value of the specified cells. Elements of the collection where the value isnull
result in the cell being cleared. -
void ClearContents()
: clears the contents of the selected cells. -
string SerializeData()
: serialises the entire spreadsheet using theColumnSeparator
andRowSeparator
and returns the resulting text representation. -
string GetTextRepresentation(IReadOnlyList<SelectionRange> selection)
: serialises the specifiedselection
using theColumnSeparator
andRowSeparator
and returns the resulting text representation of the selected cells. -
void Load(string serializedData, string serializedFormat)
: clears all the currently loaded data and loads the supplied data and format into the spreadsheet. TheserializedData
andserializedFormat
should have been serialised using the sameColumnSeparator
andRowSeparator
as the spreadsheet. -
string ColumnSeparator { get; set; }
: the character(s) used to separate different columns. This is relevant when serialising the data contained in the spreadsheet and when the user pastes data. By default, this is a tab character (\t
), which provides copy-paste compatibility with other spreadsheet software. -
string RowSeparator { get; set; }
: the character(s) used to separate different rows. This is relevant when serialising the data contained in the spreadsheet and when the user pastes data. By default, this is a newline character (\n
), which provides copy-paste compatibility with other spreadsheet software. -
string QuoteSymbol { get; set; }
: the character(s) used to quote cells containing values corresponding to the row or column separators. This is relevant when serialising the data contained in the spreadsheet and when the user pastes data. By default, this is a double quote character ("
), which provides copy-paste compatibility with other spreadsheet software. -
int MaxTableWidth { get; set; }
: the maximum width of the spreadsheet. By default, this isint.MaxValue - 2
. -
int MaxTableHeight { get; set; }
: the maximum height of the spreadsheet. By default, this isint.MaxValue - 2
. -
string SerializeFormat()
: serialises the formatting information contained in the spreadsheet and returns it as astring
. -
static string[][] SplitData(string text, string rowSeparator, string columnSeparator, string quote, out int width)
: splits the specifiedtext
using the specifiedrowSeparator
,columnSeparator
, andquote
character. When the method returns,width
will contain the maximum width of the splitted rows.
-
ImmutableList<SelectionRange> Selection { get; set; }
: gets or sets the currently selected cells. EachSelectionRange
represents a rectangular selection; multiple disjoint or overlapping selections can be specified. -
SolidColorBrush SelectionAccent { get; set; }
: the colour used to highlight the border of the selected cells. -
string[,] GetSelectedData(out (int, int)[,] coordinates)
: returns the data in the currently selected cells of the spreadsheet, as well as the coordinates of that data. The data is consolidated into a rectangular array. Selected empty cells have a value ofnull
, while elements of the rectangular array that do not correspond to a selected cell have negative coordinates.
-
void InsertColumns()
: inserts a number of columns equal to the number of selected columns, just before the first selected column. This only works if the current selection consists of a single contiguous range of columns. -
void DeleteColumns()
: deletes the selected columns. This only works if the current selection consists of a single contiguous range of columns. -
void InsertRows()
: inserts a number of rows equal to the number of selected rows, just before the first selected rows. This only works if the current selection consists of a single contiguous range of rows. -
void DeleteRows()
: deletes the selected rows. This only works if the current selection consists of a single contiguous range of rows. -
double DefaultRowHeight { get; set; }
: the default row height. This can be overridden on a row-by-row basis. -
double DefaultColumnWidth { get; set; }
: the default column width. This can be overridden on a column-by-column basis. -
void AutoFitWidth()
: automatically determines the width of the selected columns. This only works if the current selection consists of one or more ranges of columns. -
void ResetWidth()
: resets the width of the selected columns. This only works if the current selection consists of one or more ranges of columns. -
void AutoFitHeight()
: automatically determines the height of the selected rows. This only works if the current selection consists of one or more ranges of rows. -
void ResetHeight()
: resets the height of the selected rows. This only works if the current selection consists of one or more ranges of rows. -
void SetWidth(Dictionary<int, double> columnWidths)
: sets the width of the specified columns to the specified values. -
void SetHeight(Dictionary<int, double> rowHeights)
: sets the height of the specified columns to the specified values.
-
FontFamily HeaderFontFamily { get; set; }
anddouble HeaderFontSize { get; set; }
: these properties determine the font size for the row and column headers. -
IBrush HeaderForeground { get; set; }
: the foreground brush for the text in the row and column headers. -
Color HeaderBackground { get; set; }
: the background colour for the row and column headers.
-
Color GridColor { get; set; }
: the colour used to draw the grid lines. -
SolidColorBrush SpreadsheetBackground { get; set; }
: the background colour for the spreadsheet. -
bool ShowColorPreview { get; set; }
: if this istrue
, in cells that contain a colour in the format#RRGGBB
or#RRGGBBAA
, a small square showing a preview of the colour will be drawn. -
void ResetFormat()
: resets the formatting of the selected cells/rows/columns. -
void SetTypeface(Typeface typeface)
: sets thetypeface
(i.e., font family, font style and font weight) of the currently selected cells to the specified value. Thetypeface
is applied to cells, rows, or columns, as appropriate according to the current selection. If the current selection contains the whole spreadsheet, this changes the default typeface. -
Typeface GetTypeface(int left, int top)
: gets the typeface for the specified cell. -
void SetForeground(IBrush foreground)
: sets the foreground colour of the currently selected cells to the specified value. The colour is applied to cells, rows, or columns, as appropriate according to the current selection. If the current selection contains the whole spreadsheet, this changes the default colour. -
(double width, double height) GetCellSize(int left, int top)
: gets the size of the specified cell.
-
TextAlignment DefaultTextAlignment { get; set; }
: the default horizontal text alignment for the cells. This can be overridden on a cell-by-cell basis. -
VerticalAlignment DefaultVerticalAlignment { get; set; }
: the default vertical text alignment for the cells. This can be overridden on a cell-by-cell basis. -
void SetTextAlignment(TextAlignment textAlignment)
: sets the horizontal text alignment of the current selection. If the current selection contains the whole spreadsheet, this changes the default text alignment. -
void SetVerticalAlignment(VerticalAlignment verticalAlignment)
: sets the vertical text alignment of the current selection. If the current selection contains the whole spreadsheet, this changes the default text alignment. -
(TextAlignment, VerticalAlignment) GetAlignment(int left, int top)
: gets the text alignment of the specified cell.
-
void Copy()
: copies the value of the selected cells to the clipboard. The cells are concatenated using theColumnSeparator
and theRowSeparator
. -
void Cut()
: same asCopy
, but then also clears the contents of the selected cells. -
Task Paste(bool overwriteEmpty)
: pastes text or cells from the clipboard onto the spreadsheet. The text is parsed using theColumnSeparator
and theRowSeparator
. IfoverwriteEmpty
isfalse
, empty cells in the pasted content do not affect the contents of the spreadsheet; if it istrue
, cells corresponding to the empty cells are cleared. -
void Paste(string text, bool overwriteEmpty, string rowSeparator = null, string columnSeparator = null)
: pastes the specifiedtext
onto the spreadsheet. The parameteroverwriteEmpty
as the same meaning as in the previous overload of thePaste
method. The specifiedrowSeparator
andcolumnSeparator
are used to parse the data; if either is null, the default separator for the spreadsheet is used.
-
bool CanUndo { get; }
: if this istrue
, it is possible to undo the last action that was performed on the spreadsheet. -
bool CanRedo { get; }
: if this istrue
, it is possible to redo an action that has been undone. -
void Undo()
: undoes the last action performed on the spreadsheet (if possible). -
void Redo()
: redoes the last undone action on the spreadsheet (if possible).
-
bool IsEditing { get; }
: if this istrue
, the user is currently editing the value of a cell. -
void ScrollTopLeft()
: scrolls to the top-left corner of the spreadsheet.
The following events are defined for the Spreadsheet
control:
-
event EventHandler<CellSizeChangedEventArgs> CellSizeChanged
: raised when the size of the selected cell changes (e.g., because the user has used the row or column headers to resize the cell). TheCellSizeChangedEventArgs
object will contain information about which cell has had its size changed and the new size. -
event EventHandler<ColorDoubleTappedEventArgs> ColorDoubleTapped
: ifShowColorPreview
istrue
, this event is raised when the user double-clicks on the colour preview. TheColorDoubleTappedEventArgs
object contains information about the cell on which the user has clicked and the current colour. If you set theColorDoubleTappedEventArgs.Handled
property totrue
, the spreadsheet will not start to edit the cell; otherwise, the cell will enter editing mode as normal. This is useful e.g. if you wish to show a colour picker. If the user clicks on a colour cell outside of the colour preview, the normal editing behaviour will be triggered regardless.
The SelectionRange
struct represents a single rectangular range of selected cells. The properties Left
, Top
, Right
, Bottom
define the corners of the selection (all are inclusive). Width
and Height
return the size of the selection.
Some selection areas have special meanings:
- Selections that correspond to rows have
Left
equal to0
andRight
equal to theMaxTableWidth
value of the spreadsheet. - Selections that correspond to columns have
Top
equal to0
andBottom
equal to theMaxTableHeight
value of the spreadsheet. - Selections that correspond to the entire spreadsheet area have
Top
andLeft
equal to 0, andRight
andBottom
respectively equal to theMaxTableWidth
andMaxTableHeight
of the spreadsheet.
Usually, changing formatting properties for "finite" selections will change the value for individual cells, while changing formatting properties for whole rows or columns will affect the default value of the property for the row/column (which can be overridden by cell-specific values). Changing formatting properties for a selection corresponding to the whole spreadsheet will affect the default value for the spreadsheet (which can be overridden both by row/column defaults, and by cell-specific settings).
The source code for the library is available in this repository. In addition to the Spreadalonia
library project, the repository contains a demo application.