unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, NxScrollControl, NxSheet, NxSheetCell, DateUtils, Printers;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    CheckBox1: TCheckBox;
    Button5: TButton;
    Button10: TButton;
    NextSheet1: TNextSheet;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    PrintDialog1: TPrintDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure SheetView1SelectCell(Sender: TObject; ACol, ARow: Integer);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Brands: array[0..4] of WideString = ('Apple', 'Motorola', 'Nokia', 'Samsung', 'SE');


implementation

uses NxSharedCommon;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
	NextSheet1.AddRow(1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  NextSheet1.AddColumn();
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i: Integer;
begin
{--
  StylePainter object give possibility to apply various style properties
  on range of cells 
}

//
//  1. Set Font for all cells to be same as NextSheet1.Font
//

  with NextSheet1.StylePainter do
  begin
    Font.Assign(NextSheet1.Font);

    // "Paint" specified range with attributes from StylePainter
    Apply(0, 0, NextSheet1.ColCount - 1, NextSheet1.RowCount - 1);
  end;

//
//  2. Merge 3 cells in 1st row, and resize this row to 48px
//

  with NextSheet1.Cell[0, 0] do
  begin
    Text := 'Phones Sales Report';
    Color := $00EEEEEE;

    Font.Style := [fsItalic];
    Font.Size := 12;

    BorderBottom.LineStyle := lsSolid;
    BorderBottom.Color := clBlack;
  end;

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

  NextSheet1.Row[0].Size := 48; // resize 1st row.

  for i := 0 to NextSheet1.ColCount - 1 do
  begin
    NextSheet1.Column[i].Size := 100;
  end;

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

  NextSheet1.Cell[0, 1].Text := 'Brand';
  NextSheet1.Cell[0, 1].Color := $00DDDDDD;
  NextSheet1.Cell[0, 1].Font.Assign(NextSheet1.Font);
  NextSheet1.Cell[0, 1].BorderBottom.LineStyle := lsThinLine;


  NextSheet1.MergeCells(1, 1, 3, 1, caCenter);

  NextSheet1.Cell[1, 1].Text := 'Months';
  NextSheet1.Cell[1, 1].Color := $00DDDDDD;
  NextSheet1.Cell[1, 1].Font.Assign(NextSheet1.Font);
  NextSheet1.Cell[1, 1].BorderBottom.LineStyle := lsThinLine;

  // Draw vertical doted border
  for i := 1 to 7 do
  begin
    NextSheet1.Cell[0, i].BorderRight.SetBorder(clBlack, lsDot);
  end;

  for i := 0 to 3 do
  begin
    NextSheet1.Cell[i, 2].BorderBottom.SetBorder(clBlack, lsThinLine);
  end;

  for i := 0 to Length(Brands) - 1 do
  begin
    NextSheet1.Cell[0, 3 + i].Text := Brands[i];
  end;

  NextSheet1.Cell[1, 2].Text := DateToStr(Today);
  NextSheet1.Cell[1, 2].Alignment := caCenter;
  NextSheet1.Cell[1, 2].Kind := ckDateTime;
  NextSheet1.Cell[1, 2].FormatMask := 'mmm yyyy';
  NextSheet1.Cell[1, 2].Color := $00EFEFEF;

  NextSheet1.Cell[2, 2].Text := 'Feb';
  NextSheet1.Cell[2, 2].Alignment := caCenter;
  NextSheet1.Cell[2, 2].Color := $00EFEFEF;

  NextSheet1.Cell[3, 2].Text := 'Mar';
  NextSheet1.Cell[3, 2].Alignment := caCenter;
  NextSheet1.Cell[3, 2].Color := $00EFEFEF;

//
// Insert sample data
//
  NextSheet1.Cell[1, 3].Text := '23000';
  NextSheet1.Cell[1, 4].Text := '12400';
  NextSheet1.Cell[1, 5].Text := '72000';
  NextSheet1.Cell[1, 6].Text := '15100';
  NextSheet1.Cell[1, 7].Text := '42000';

  NextSheet1.Cell[2, 3].Text := '22010';
  NextSheet1.Cell[2, 4].Text := '11200';
  NextSheet1.Cell[2, 5].Text := '72400';
  NextSheet1.Cell[2, 6].Text := '15040';
  NextSheet1.Cell[2, 7].Text := '42300';

  NextSheet1.Cell[3, 3].Text := '21000';
  NextSheet1.Cell[3, 4].Text := '13000';
  NextSheet1.Cell[3, 5].Text := '71000';
  NextSheet1.Cell[3, 6].Text := '12400';
  NextSheet1.Cell[3, 7].Text := '44000';


// Set style properties
  with NextSheet1.StylePainter do
  begin
    Alignment := caCenterRight;
    Kind := ckNumber;
    FormatMask := '#,##0.00';
  end;

// Apply style to cells
  NextSheet1.StylePainter.Apply(1, 3, 3, 9);

//
//  Draw double line above sum
//
  for i := 0 to 3 do
  begin
    NextSheet1.Cell[i, 7].BorderBottom.LineStyle := lsDouble;
  end;

  with NextSheet1.Cell[0, 8] do
  begin
    Alignment := caCenterRight;
    Text := 'Sum:';
    Font.Style := [fsItalic];
  end;

  NextSheet1.Cell[1, 8].Text := '=SUM(B4:B8)';
  NextSheet1.Cell[2, 8].Text := '=SUM(C4:C8)';

  with NextSheet1.Cell[3, 8] do
  begin
    Text := '=SUM(D4:D8)';
    Font.Color := clBlue;
  end;

  with NextSheet1.Cell[0, 9] do
  begin
    Alignment := caCenterRight;
    Text := 'Total:';
    Font.Style := [fsItalic];
  end;

  NextSheet1.Cell[3, 9].Text := '=SUM(B9:D9)';
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  NextSheet1.MergeSelection;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
	if CheckBox1.Checked then NextSheet1.Options := NextSheet1.Options + [soHeadings]
		else NextSheet1.Options := NextSheet1.Options - [soHeadings];
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  with NextSheet1.Selection do
    NextSheet1.SplitCells(StartCol, StartRow, EndCol, EndRow);
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
  ShowMessage('From: ' + IntToStr(NextSheet1.Selection.StartCol) + ', ' +
  						IntToStr(NextSheet1.Selection.StartRow) + #13#10 + 'To: ' +
							IntToStr(NextSheet1.Selection.EndCol) + ', ' +
							IntToStr(NextSheet1.Selection.EndRow));
end;

procedure TForm1.Button13Click(Sender: TObject);
begin
  NextSheet1.DeleteColumn(1);
end;

procedure TForm1.SheetView1SelectCell(Sender: TObject; ACol,
  ARow: Integer);
begin
  Caption := IntToStr(ACol) + ', ' + IntToStr(ARow);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  NextSheet1.SaveToXLSFile('test.xls');
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  NextSheet1.SaveToCSVFile('test.csv');
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
  ShowMessage(NextSheet1.Cell[NextSheet1.SelectedCol, NextSheet1.SelectedRow].Value);
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
  if PrintDialog1.Execute then
  begin                                       
    NextSheet1.Caption := 'Printing NextSheet';
    NextSheet1.Print(100, [poPageNumbers, poPageTitle]);
  end;
end;

end.
