Library Vcl NextSuite 5 NextSheet Articles Sample Project

Sample Project

In this tutorial we will step by step build small application that use NextSheet and display formated data in it.

This tutorial require NextSheet 1.0 to be installed.

Placing component on a form

Click on "Next Suite" tab/category within components palette/toolbox and select NextSheet component icon.

NextSheet Icon

Placed

Click on form, and component will be placed with one cell already added, with ColCount and RowCount properties set to 1.

Now add NxSheetCell unit in uses section. This unit contain values for cell alignment, line styles...

Setting ColCount and RowCount properties

By increasing ColCount property new columns will be added at the end of columns array. Otherwise decreasing value of this property will delete columns beginning from last one.

Set ColCount property to 4.

Result:

ColCount

RowCount property is very similar, but instead on columns it work on rows.

Set RowCount property to 3.

RowCount

Now we have matrix of 4 x 3 cells where every cell may have own value and be formated in own style.

Merging cells in first row

Cell's may be merged by calling MergeCells method. This method merge all cells in square between starting and end cell.

Next code will merge cells from cell 0, 0 to the cell 3, 0:

pas
NextSheet1.MergeCells(0, 0, 3, 0);

Result:

Merged

Now, we will set Text property and Alignment property for first cell. Since this is first cell in merged cells region, this cell will occupy space of whole region.

pas
NextSheet1.Cell[0, 0].Alignment := caCenter;
NextSheet1.Cell[0, 0].Text := 'Sales Data';
NextSheet1.Cell[0, 0].Color := clSkyBlue;

Result:

Alignment

Now we will draw one black line bellow first row.

Since cells in first row are merged, we will only set border for top-left cell in merged area:

pas
NextSheet1.Cell[0, 0].BorderBottom.LineStyle := lsSolid;
NextSheet1.Cell[0, 0].BorderBottom.Color := clBlack;

Result:

Underline

After cells are un-merged (by calling Split method), again visible cells will have borders set.

Setting cells in second row

We will merge cells in 2nd row, but not as one cell but 2:

pas
NextSheet1.MergeCells(0, 1, 1, 1, caCenter);
NextSheet1.MergeCells(2, 1, 3, 1, caCenter);

Result:

Merged

And place text in them

pas
NextSheet1.Cell[0, 1].Text := 'First Half';
NextSheet1.Cell[2, 1].Text := 'Second Half';

Result:

Text in two cells

pas
NextSheet1.Cell[0, 1].SetBorder(bpBottom, lsDouble, clBlack);
NextSheet1.Cell[2, 1].SetBorder(bpBottom, lsDouble, clBlack);

Double border

Place text in the third row

Also, we will place text in 3rd row:

pas
NextSheet1.Cell[0, 2].Text := 'Q1';
NextSheet1.Cell[1, 2].Text := 'Q2';
NextSheet1.Cell[2, 2].Text := 'Q3';
NextSheet1.Cell[3, 2].Text := 'Q4';

Result:

Quarters cells

Add sample data

Now we will set RowCount property to 10.

To add sample data with AddCells method. This method add cells from array beginning from one cell.

pas
NextSheet1.AddCells(['435', '335', '23', '46', '94', '256', '334', '24', '134', '920', '23', '1', '567', '753', '13', '88'], 0, 3);

Result:

Sample data

We will add thin line between data and remaining rows:

pas
NextSheet1.Cell[0, 7].BorderTop.SetBorder(clBlack, lsThinLine);
NextSheet1.Cell[1, 7].BorderTop.SetBorder(clBlack, lsThinLine);
NextSheet1.Cell[2, 7].BorderTop.SetBorder(clBlack, lsThinLine);
NextSheet1.Cell[3, 7].BorderTop.SetBorder(clBlack, lsThinLine);

Result:

Thin lines

Sumarize columns

Now we will summarise sample data within last row:

pas
NextSheet1.Cell[0, 7].Text := '=SUM(A4:A7)';
NextSheet1.Cell[1, 7].Text := '=SUM(B4:B7)';
NextSheet1.Cell[2, 7].Text := '=SUM(C4:C7)';
NextSheet1.Cell[3, 7].Text := '=SUM(D4:D7)';

Result:

Summarised

We have use SUM built-in function and calculate sum of each column.

Final make-up

With using StylePainter object we will align and format all number cells.

pas
with NextSheet1.StylePainter do
begin
  Alignment := caCenterRight;
  FormatMask := '0.00';">

,##0.00';

Kind := ckNumber; end; NextSheet1.StylePainter.Apply(0, 3, 3, 7);

Sign in