2009-10-06 19:25:18 +00:00
|
|
|
unit mainform;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
2015-08-15 14:24:31 +00:00
|
|
|
StdCtrls, Menus, ExtCtrls, ActnList, Spin, Buttons, ButtonPanel,
|
|
|
|
fpspreadsheetgrid, fpspreadsheet, fpsallformats, fpspreadsheetctrls;
|
2009-10-06 19:25:18 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
TForm1 = class(TForm)
|
2014-06-22 13:49:48 +00:00
|
|
|
BtnOpen: TButton;
|
|
|
|
BtnSave: TButton;
|
|
|
|
BtnNew: TButton;
|
2015-08-15 14:24:31 +00:00
|
|
|
BtnEnterText: TButton;
|
|
|
|
EdNewCellText: TEdit;
|
|
|
|
Label2: TLabel;
|
2014-06-22 13:49:48 +00:00
|
|
|
SheetsCombo: TComboBox;
|
2014-05-04 18:07:54 +00:00
|
|
|
Label1: TLabel;
|
2014-05-23 15:45:07 +00:00
|
|
|
OpenDialog: TOpenDialog;
|
2014-04-19 19:29:13 +00:00
|
|
|
Panel1: TPanel;
|
2014-06-22 13:49:48 +00:00
|
|
|
Panel2: TPanel;
|
2014-05-23 15:45:07 +00:00
|
|
|
SaveDialog: TSaveDialog;
|
2015-08-15 14:24:31 +00:00
|
|
|
sWorkbookSource1: TsWorkbookSource;
|
2014-05-23 15:45:07 +00:00
|
|
|
WorksheetGrid: TsWorksheetGrid;
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure BtnNewClick(Sender: TObject);
|
|
|
|
procedure BtnOpenClick(Sender: TObject);
|
|
|
|
procedure BtnSaveClick(Sender: TObject);
|
2015-08-15 14:24:31 +00:00
|
|
|
procedure BtnEnterTextClick(Sender: TObject);
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure SheetsComboSelect(Sender: TObject);
|
2009-10-06 19:25:18 +00:00
|
|
|
private
|
|
|
|
{ private declarations }
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure LoadFile(const AFileName: String);
|
2009-10-06 19:25:18 +00:00
|
|
|
public
|
|
|
|
{ public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form1: TForm1;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
uses
|
2015-10-09 13:27:04 +00:00
|
|
|
fpcanvas, fpsutils, fpsRegFileFormats;
|
2014-04-19 19:29:13 +00:00
|
|
|
|
2009-10-06 19:25:18 +00:00
|
|
|
|
2014-06-22 13:49:48 +00:00
|
|
|
{ TForm1 }
|
2014-05-07 22:44:00 +00:00
|
|
|
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure TForm1.BtnNewClick(Sender: TObject);
|
2014-05-08 15:33:25 +00:00
|
|
|
var
|
2014-06-22 13:49:48 +00:00
|
|
|
dlg: TForm;
|
|
|
|
edCols, edRows: TSpinEdit;
|
2014-05-08 15:33:25 +00:00
|
|
|
begin
|
2014-06-22 13:49:48 +00:00
|
|
|
dlg := TForm.Create(nil);
|
|
|
|
try
|
|
|
|
dlg.Width := 220;
|
|
|
|
dlg.Height := 128;
|
|
|
|
dlg.Position := poMainFormCenter;
|
|
|
|
dlg.Caption := 'New workbook';
|
|
|
|
edCols := TSpinEdit.Create(dlg);
|
|
|
|
with edCols do begin
|
|
|
|
Parent := dlg;
|
|
|
|
Left := dlg.ClientWidth - Width - 24;
|
|
|
|
Top := 16;
|
2014-07-21 20:33:25 +00:00
|
|
|
Value := WorksheetGrid.ColCount - ord(WorksheetGrid.ShowHeaders);
|
2014-05-08 15:33:25 +00:00
|
|
|
end;
|
2014-06-22 13:49:48 +00:00
|
|
|
with TLabel.Create(dlg) do begin
|
|
|
|
Parent := dlg;
|
|
|
|
Left := 24;
|
|
|
|
Top := edCols.Top + 3;
|
|
|
|
Caption := 'Columns:';
|
|
|
|
FocusControl := edCols;
|
2014-05-15 22:41:14 +00:00
|
|
|
end;
|
2014-06-22 13:49:48 +00:00
|
|
|
edRows := TSpinEdit.Create(dlg);
|
|
|
|
with edRows do begin
|
|
|
|
Parent := dlg;
|
|
|
|
Left := edCols.Left;
|
|
|
|
Top := edCols.Top + edCols.Height + 8;
|
2014-07-21 20:33:25 +00:00
|
|
|
Value := WorksheetGrid.RowCount - ord(WorksheetGrid.ShowHeaders);
|
2014-05-15 22:41:14 +00:00
|
|
|
end;
|
2014-06-22 13:49:48 +00:00
|
|
|
with TLabel.Create(dlg) do begin
|
|
|
|
Parent := dlg;
|
|
|
|
Left := 24;
|
|
|
|
Top := edRows.Top + 3;
|
|
|
|
Caption := 'Rows:';
|
|
|
|
FocusControl := edRows;
|
|
|
|
end;
|
|
|
|
with TButtonPanel.Create(dlg) do begin
|
|
|
|
Parent := dlg;
|
|
|
|
Align := alBottom;
|
|
|
|
ShowButtons := [pbCancel, pbOK];
|
|
|
|
end;
|
|
|
|
if dlg.ShowModal = mrOK then begin
|
2014-06-24 11:45:16 +00:00
|
|
|
WorksheetGrid.NewWorkbook(edCols.Value, edRows.Value);
|
2014-06-22 13:49:48 +00:00
|
|
|
SheetsCombo.Items.Clear;
|
|
|
|
SheetsCombo.Items.Add('Sheet 1');
|
|
|
|
SheetsCombo.ItemIndex := 0;
|
2014-05-23 15:45:07 +00:00
|
|
|
end;
|
2014-06-22 13:49:48 +00:00
|
|
|
finally
|
|
|
|
dlg.Free;
|
2014-05-15 22:41:14 +00:00
|
|
|
end;
|
2014-05-11 11:56:20 +00:00
|
|
|
end;
|
|
|
|
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure TForm1.BtnOpenClick(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
begin
|
2014-09-11 12:52:06 +00:00
|
|
|
if OpenDialog.FileName <> '' then begin
|
|
|
|
OpenDialog.InitialDir := ExtractFileDir(OpenDialog.FileName);
|
|
|
|
OpenDialog.FileName := ChangeFileExt(ExtractFileName(OpenDialog.FileName), '');
|
|
|
|
end;
|
|
|
|
if OpenDialog.Execute then begin
|
2014-05-23 15:45:07 +00:00
|
|
|
LoadFile(OpenDialog.FileName);
|
2014-09-11 12:52:06 +00:00
|
|
|
end;
|
2013-12-28 15:29:22 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
// Saves sheet in grid to file, overwriting existing file
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure TForm1.BtnSaveClick(Sender: TObject);
|
2014-08-11 11:34:46 +00:00
|
|
|
var
|
2015-02-04 18:15:19 +00:00
|
|
|
err, fn: String;
|
2013-12-28 15:29:22 +00:00
|
|
|
begin
|
2014-05-23 15:45:07 +00:00
|
|
|
if WorksheetGrid.Workbook = nil then
|
2014-05-07 22:44:00 +00:00
|
|
|
exit;
|
2014-04-19 19:29:13 +00:00
|
|
|
|
2014-09-11 12:52:06 +00:00
|
|
|
if WorksheetGrid.Workbook.Filename <>'' then begin
|
2015-02-04 18:15:19 +00:00
|
|
|
fn := AnsiToUTF8(WorksheetGrid.Workbook.Filename);
|
|
|
|
SaveDialog.InitialDir := ExtractFileDir(fn);
|
|
|
|
SaveDialog.FileName := ChangeFileExt(ExtractFileName(fn), '');
|
2014-09-11 12:52:06 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-23 15:45:07 +00:00
|
|
|
if SaveDialog.Execute then
|
2014-05-27 12:19:28 +00:00
|
|
|
begin
|
|
|
|
Screen.Cursor := crHourglass;
|
|
|
|
try
|
2015-02-04 18:15:19 +00:00
|
|
|
WorksheetGrid.SaveToSpreadsheetFile(UTF8ToAnsi(SaveDialog.FileName));
|
2015-08-15 15:34:24 +00:00
|
|
|
//WorksheetGrid.WorkbookSource.SaveToSpreadsheetFile(UTF8ToAnsi(SaveDialog.FileName)); // works as well
|
2014-05-27 12:19:28 +00:00
|
|
|
finally
|
|
|
|
Screen.Cursor := crDefault;
|
2014-08-11 11:34:46 +00:00
|
|
|
// Show a message in case of error(s)
|
|
|
|
err := WorksheetGrid.Workbook.ErrorMsg;
|
|
|
|
if err <> '' then
|
|
|
|
MessageDlg(err, mtError, [mbOK], 0);
|
2014-05-27 12:19:28 +00:00
|
|
|
end;
|
|
|
|
end;
|
2013-12-28 15:29:22 +00:00
|
|
|
end;
|
|
|
|
|
2015-08-15 14:24:31 +00:00
|
|
|
procedure TForm1.BtnEnterTextClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
WorksheetGrid.Worksheet.WriteText(109, 27, EdNewCellText.Text);
|
|
|
|
end;
|
|
|
|
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure TForm1.SheetsComboSelect(Sender: TObject);
|
2014-05-11 16:16:59 +00:00
|
|
|
begin
|
2014-06-22 13:49:48 +00:00
|
|
|
WorksheetGrid.SelectSheetByIndex(SheetsCombo.ItemIndex);
|
2014-05-07 22:44:00 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-29 21:58:48 +00:00
|
|
|
// Loads first worksheet from file into grid
|
2014-06-22 13:49:48 +00:00
|
|
|
procedure TForm1.LoadFile(const AFileName: String);
|
2014-08-11 11:34:46 +00:00
|
|
|
var
|
|
|
|
err: String;
|
2014-04-29 21:58:48 +00:00
|
|
|
begin
|
2014-05-04 18:07:54 +00:00
|
|
|
// Load file
|
2014-05-27 12:19:28 +00:00
|
|
|
Screen.Cursor := crHourglass;
|
2014-04-29 21:58:48 +00:00
|
|
|
try
|
2014-09-11 12:52:06 +00:00
|
|
|
try
|
|
|
|
WorksheetGrid.LoadFromSpreadsheetFile(UTF8ToSys(AFileName));
|
2014-05-27 12:19:28 +00:00
|
|
|
|
2014-09-11 12:52:06 +00:00
|
|
|
// Update user interface
|
|
|
|
Caption := Format('fpsGrid - %s (%s)', [
|
|
|
|
AFilename,
|
2015-10-09 13:27:04 +00:00
|
|
|
GetSpreadTechnicalName(WorksheetGrid.Workbook.FileFormatID)
|
2014-09-11 12:52:06 +00:00
|
|
|
]);
|
2014-05-27 12:19:28 +00:00
|
|
|
|
2014-09-11 12:52:06 +00:00
|
|
|
// Collect the sheet names in the combobox for switching sheets.
|
|
|
|
WorksheetGrid.GetSheets(SheetsCombo.Items);
|
|
|
|
SheetsCombo.ItemIndex := 0;
|
|
|
|
except
|
|
|
|
on E:Exception do begin
|
|
|
|
// Empty worksheet instead of the loaded one
|
|
|
|
WorksheetGrid.NewWorkbook(26, 100);
|
|
|
|
Caption := 'fpsGrid - no name';
|
|
|
|
SheetsCombo.Items.Clear;
|
|
|
|
// Grab the error message
|
|
|
|
WorksheetGrid.Workbook.AddErrorMsg(E.Message);
|
|
|
|
end;
|
|
|
|
end;
|
2014-06-22 13:49:48 +00:00
|
|
|
|
2014-04-29 21:58:48 +00:00
|
|
|
finally
|
2014-05-27 12:19:28 +00:00
|
|
|
Screen.Cursor := crDefault;
|
2014-08-11 11:34:46 +00:00
|
|
|
|
|
|
|
// Show a message in case of error(s)
|
|
|
|
err := WorksheetGrid.Workbook.ErrorMsg;
|
|
|
|
if err <> '' then
|
|
|
|
MessageDlg(err, mtError, [mbOK], 0);
|
2014-04-29 21:58:48 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-08 14:53:16 +00:00
|
|
|
|
2009-10-06 19:25:18 +00:00
|
|
|
initialization
|
|
|
|
{$I mainform.lrs}
|
|
|
|
|
|
|
|
end.
|
|
|
|
|