You've already forked lazarus-ccr
fpspreadsheet: Add demo how to create a spreadsheet by means of TsWorksheetDataset.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8101 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -0,0 +1,92 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="12"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<CompatibilityMode Value="True"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<Title Value="CreateTableDemo"/>
|
||||
<Scaled Value="True"/>
|
||||
<ResourceType Value="res"/>
|
||||
<UseXPManifest Value="True"/>
|
||||
<XPManifest>
|
||||
<DpiAware Value="True"/>
|
||||
</XPManifest>
|
||||
<Icon Value="0"/>
|
||||
</General>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
<UseFileFilters Value="True"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<FormatVersion Value="2"/>
|
||||
</RunParams>
|
||||
<RequiredPackages Count="3">
|
||||
<Item1>
|
||||
<PackageName Value="FCL"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<PackageName Value="laz_fpspreadsheet_dataset"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<PackageName Value="LCL"/>
|
||||
</Item3>
|
||||
</RequiredPackages>
|
||||
<Units Count="3">
|
||||
<Unit0>
|
||||
<Filename Value="CreateTableDemo.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
<Unit1>
|
||||
<Filename Value="main.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<ComponentName Value="MainForm"/>
|
||||
<ResourceBaseClass Value="Form"/>
|
||||
</Unit1>
|
||||
<Unit2>
|
||||
<Filename Value="viewerform.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<ComponentName Value="SpreadsheetViewerForm"/>
|
||||
<ResourceBaseClass Value="Form"/>
|
||||
<UnitName Value="ViewerForm"/>
|
||||
</Unit2>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="CreateTableDemo"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Linking>
|
||||
<Options>
|
||||
<Win32>
|
||||
<GraphicApplication Value="True"/>
|
||||
</Win32>
|
||||
</Options>
|
||||
</Linking>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -0,0 +1,25 @@
|
||||
program CreateTableDemo;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
uses
|
||||
{$IFDEF UNIX}
|
||||
cthreads,
|
||||
{$ENDIF}
|
||||
{$IFDEF HASAMIGA}
|
||||
athreads,
|
||||
{$ENDIF}
|
||||
Interfaces, // this includes the LCL widgetset
|
||||
Forms, main, ViewerForm
|
||||
{ you can add units after this };
|
||||
|
||||
{$R *.res}
|
||||
|
||||
begin
|
||||
RequireDerivedFormResource:=True;
|
||||
Application.Scaled:=True;
|
||||
Application.Initialize;
|
||||
Application.CreateForm(TMainForm, MainForm);
|
||||
Application.Run;
|
||||
end.
|
||||
|
@ -0,0 +1,57 @@
|
||||
object MainForm: TMainForm
|
||||
Left = 285
|
||||
Height = 366
|
||||
Top = 131
|
||||
Width = 714
|
||||
Caption = 'Create Table Demo'
|
||||
ClientHeight = 366
|
||||
ClientWidth = 714
|
||||
OnCreate = FormCreate
|
||||
LCLVersion = '2.3.0.0'
|
||||
object DBGrid: TDBGrid
|
||||
Left = 0
|
||||
Height = 324
|
||||
Top = 0
|
||||
Width = 714
|
||||
Align = alClient
|
||||
Color = clWindow
|
||||
Columns = <>
|
||||
DataSource = DataSource
|
||||
TabOrder = 0
|
||||
end
|
||||
object Panel1: TPanel
|
||||
Left = 8
|
||||
Height = 26
|
||||
Top = 332
|
||||
Width = 698
|
||||
Align = alBottom
|
||||
AutoSize = True
|
||||
BorderSpacing.Around = 8
|
||||
BevelOuter = bvNone
|
||||
ClientHeight = 26
|
||||
ClientWidth = 698
|
||||
TabOrder = 1
|
||||
object btnViewSpreadsheet: TButton
|
||||
AnchorSideTop.Control = Panel1
|
||||
AnchorSideTop.Side = asrCenter
|
||||
Left = 0
|
||||
Height = 25
|
||||
Top = 1
|
||||
Width = 136
|
||||
AutoSize = True
|
||||
Caption = 'View spreadsheet file'
|
||||
OnClick = btnViewSpreadsheetClick
|
||||
TabOrder = 0
|
||||
end
|
||||
end
|
||||
object Dataset: TsWorksheetDataset
|
||||
FieldDefs = <>
|
||||
Left = 77
|
||||
Top = 39
|
||||
end
|
||||
object DataSource: TDataSource
|
||||
DataSet = Dataset
|
||||
Left = 77
|
||||
Top = 104
|
||||
end
|
||||
end
|
103
components/fpspreadsheet/examples/dataset/create_table/main.pas
Normal file
103
components/fpspreadsheet/examples/dataset/create_table/main.pas
Normal file
@ -0,0 +1,103 @@
|
||||
unit main;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, DB, Forms, Controls, Graphics, Dialogs, DBGrids, StdCtrls,
|
||||
ExtCtrls, fpsDataset;
|
||||
|
||||
type
|
||||
|
||||
{ TMainForm }
|
||||
|
||||
TMainForm = class(TForm)
|
||||
btnViewSpreadsheet: TButton;
|
||||
DataSource: TDataSource;
|
||||
Dataset: TsWorksheetDataset;
|
||||
DBGrid: TDBGrid;
|
||||
Panel1: TPanel;
|
||||
procedure btnViewSpreadsheetClick(Sender: TObject);
|
||||
procedure FormCreate(Sender: TObject);
|
||||
private
|
||||
|
||||
public
|
||||
|
||||
end;
|
||||
|
||||
var
|
||||
MainForm: TMainForm;
|
||||
|
||||
implementation
|
||||
|
||||
{$R *.lfm}
|
||||
|
||||
uses
|
||||
ViewerForm;
|
||||
|
||||
const
|
||||
FILE_NAME = 'TestData.ods';
|
||||
|
||||
{ TMainForm }
|
||||
|
||||
procedure TMainForm.FormCreate(Sender: TObject);
|
||||
begin
|
||||
// For demonstration purposes we want to create a new file whenever the demo starts.
|
||||
if FileExists(FILE_NAME) then
|
||||
DeleteFile(FILE_NAME);
|
||||
|
||||
// Set the name of the data file
|
||||
Dataset.FileName := FILE_NAME;
|
||||
|
||||
// Set the name of the worksheet
|
||||
Dataset.SheetName := 'Test-Sheet';
|
||||
|
||||
// Define fields
|
||||
Dataset.AutoFieldDefs := false;
|
||||
Dataset.AddFieldDef('FloatColumn', ftFloat);
|
||||
Dataset.AddFieldDef('StringColumn', ftString, 20);
|
||||
Dataset.AddFieldDef('DateColumn', ftDate);
|
||||
Dataset.AddFieldDef('BooleanColumn', ftBoolean, 0, 4); // 4 --> column 4 in worksheet --> skip column 3
|
||||
Dataset.CreateTable;
|
||||
|
||||
// Open the table
|
||||
Dataset.Open;
|
||||
(Dataset.FieldByName('FloatColumn') as TNumericField).DisplayFormat := '0.000';
|
||||
|
||||
// Add some arbitrary data
|
||||
Dataset.Append;
|
||||
Dataset.FieldByName('FloatColumn').AsFloat := 3.1415;
|
||||
Dataset.FieldByName('StringColumn').AsString := 'abc';
|
||||
Dataset.FieldByName('DateColumn').AsDateTime := EncodeDate(2000, 1, 1);
|
||||
Dataset.FieldByName('BooleanColumn').AsBoolean := true;
|
||||
Dataset.Post;
|
||||
|
||||
Dataset.Append;
|
||||
Dataset.FieldByName('FloatColumn').AsFloat := 2*3.1415;
|
||||
Dataset.FieldByName('StringColumn').AsString := 'Lorem ipsum';
|
||||
Dataset.FieldByName('DateColumn').AsDateTime := Date();
|
||||
Dataset.FieldByName('BooleanColumn').AsBoolean := false;
|
||||
Dataset.Post;
|
||||
|
||||
Dataset.Append;
|
||||
Dataset.FieldByName('FloatColumn').AsFloat := 3*3.1415;
|
||||
Dataset.FieldByName('StringColumn').AsString := 'Hello World';
|
||||
Dataset.FieldByName('DateColumn').AsDateTime := Date() + 1;
|
||||
Dataset.FieldByName('BooleanColumn').AsInteger := 3; // anything <> 0 is "true"
|
||||
Dataset.Post;
|
||||
end;
|
||||
|
||||
procedure TMainForm.btnViewSpreadsheetClick(Sender: TObject);
|
||||
begin
|
||||
Dataset.Flush;
|
||||
|
||||
if SpreadsheetViewerForm = nil then
|
||||
SpreadsheetViewerForm := TSpreadsheetViewerForm.Create(Application);
|
||||
|
||||
SpreadsheetViewerForm.LoadFile(Dataset.FileName, Dataset.SheetName);
|
||||
SpreadsheetViewerForm.Show;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
@ -0,0 +1,20 @@
|
||||
object SpreadsheetViewerForm: TSpreadsheetViewerForm
|
||||
Left = 617
|
||||
Height = 334
|
||||
Top = 261
|
||||
Width = 564
|
||||
Caption = 'Simple Spreadsheet Viewer'
|
||||
ClientHeight = 334
|
||||
ClientWidth = 564
|
||||
OnCreate = FormCreate
|
||||
LCLVersion = '2.3.0.0'
|
||||
object StringGrid: TStringGrid
|
||||
Left = 0
|
||||
Height = 334
|
||||
Top = 0
|
||||
Width = 564
|
||||
Align = alClient
|
||||
DefaultColWidth = 100
|
||||
TabOrder = 0
|
||||
end
|
||||
end
|
@ -0,0 +1,64 @@
|
||||
unit ViewerForm;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids,
|
||||
fpSpreadsheet, fpsTypes, fpsUtils;
|
||||
|
||||
type
|
||||
|
||||
{ TSpreadsheetViewerForm }
|
||||
|
||||
TSpreadsheetViewerForm = class(TForm)
|
||||
StringGrid: TStringGrid;
|
||||
procedure FormCreate(Sender: TObject);
|
||||
private
|
||||
public
|
||||
procedure LoadFile(const AFileName, ASheetName: String);
|
||||
end;
|
||||
|
||||
var
|
||||
SpreadsheetViewerForm: TSpreadsheetViewerForm;
|
||||
|
||||
implementation
|
||||
|
||||
{$R *.lfm}
|
||||
|
||||
procedure TSpreadsheetViewerForm.FormCreate(Sender: TObject);
|
||||
begin
|
||||
StringGrid.ColWidths[0] := 40;
|
||||
end;
|
||||
|
||||
procedure TSpreadsheetViewerForm.LoadFile(const AFileName, ASheetName: String);
|
||||
var
|
||||
workbook: TsWorkbook;
|
||||
worksheet: TsWorksheet;
|
||||
lastCol, lastRow: Integer;
|
||||
row, col: Integer;
|
||||
begin
|
||||
workbook := TsWorkbook.Create;
|
||||
try
|
||||
workbook.ReadFromFile(AFileName);
|
||||
worksheet := workbook.GetWorksheetByName(ASheetName);
|
||||
lastCol := worksheet.GetLastColIndex(true);
|
||||
lastRow := worksheet.GetLastRowIndex(true);
|
||||
StringGrid.RowCount := lastRow + 1 + StringGrid.FixedRows;
|
||||
StringGrid.ColCount := lastcol + 1 + StringGrid.FixedCols;
|
||||
for col := 1 to stringGrid.ColCount-1 do
|
||||
StringGrid.Cells[col, 0] := GetColString(col - 1);
|
||||
for row := 1 to StringGrid.RowCount-1 do
|
||||
begin
|
||||
StringGrid.Cells[0, row] := IntToStr(row);
|
||||
for col := 1 to StringGrid.ColCount - 1 do
|
||||
StringGrid.Cells[col, row] := worksheet.ReadAsText(row - 1, col - 1);
|
||||
end;
|
||||
finally
|
||||
workbook.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
Reference in New Issue
Block a user