home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 October
/
Chip_2002-10_cd1.bin
/
zkuste
/
delphi
/
kolekce
/
d56
/
FLEXCEL.ZIP
/
XLSAdapter
/
UXlsSheet.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2002-07-03
|
13KB
|
403 lines
unit UXlsSheet;
interface
uses classes, sysutils, UXlsBaseRecords, UXlsBaseRecordLists, UXlsOtherRecords, UXlsChart,
USST, XlsMessages, UXlsSections, UXlsCondFmt, UXlsRowColEntries, UXlsEscher,
UXlsRangeRecords, UEscherRecords, UXlsWorkbookGlobals, UXlsNotes, UXlsBaseList,
UFlxMessages, UXlsCellRecords, UXlsFormula;
type
TSheet= class (TBaseSection)
protected
FWorkbookGlobals: TWorkbookGlobals;
FWindow2: TWindow2Record;
function GetSelected: boolean;
procedure SetSelected(const Value: boolean);
public
function CopyTo: TSheet; //This method cant be virtual
function DoCopyTo: TSheet; virtual;
procedure InsertAndCopyRows(const FirstRow, LastRow, DestRow, aCount: integer; const SheetInfo: TSheetInfo; const OnlyFormulas: boolean);virtual;abstract;
procedure DeleteRows(const aRow, aCount: word;const SheetInfo: TSheetInfo);virtual; abstract;
procedure ArrangeInsert(const InsPos, InsCount: integer ; const SheetInfo: TSheetInfo);virtual;abstract;
constructor Create(const aWorkbookGlobals: TWorkbookGlobals);virtual;
property Selected: boolean read GetSelected write SetSelected;
end;
ClassOfTSheet= Class of TSheet;
TChart = class (TSheet)
private
FChartRecords: TChartRecordList;
public
constructor Create(const aWorkbookGlobals: TWorkbookGlobals);override;
destructor Destroy;override;
function DoCopyTo: TSheet; override;
function TotalSize:int64; override;
procedure LoadFromStream( const DataStream: TStream; const First: TBOFRecord; const SST: TSST);override;
procedure SaveToStream(const DataStream: TStream);override;
procedure Clear; override;
procedure ArrangeInsert(const InsPos, InsCount: integer; const SheetInfo: TSheetInfo);override;
procedure InsertAndCopyRows(const FirstRow, LastRow, DestRow, aCount: integer; const SheetInfo: TSheetInfo; const OnlyFormulas: boolean);override;
procedure DeleteRows(const aRow, aCount: word; const SheetInfo: TSheetInfo);override;
end;
TChartList = class(TBaseList) //records are TChart
{$INCLUDE inc\TChartListHdr.inc}
procedure SaveToStream(const DataStream: TStream);
procedure ArrangeInsert(const InsPos, InsCount: integer; const SheetInfo: TSheetInfo);
end;
TWorkSheet = class (TSheet)
private
FMiscRecords1: TBaseRecordList;
FMiscRecords2: TBaseRecordList;
FDrawing: TDrawing;
FCells: TCells;
FRanges: TRangeList;
FNotes: TNoteList;
function GetDrawingRow(index: integer): integer;
function GetDrawingName(index: integer): widestring;
public
constructor Create(const aWorkbookGlobals: TWorkbookGlobals);override;
destructor Destroy;override;
function DoCopyTo: TSheet; override;
function TotalSize:int64; override;
procedure LoadFromStream( const DataStream: TStream; const First: TBOFRecord; const SST: TSST);override;
procedure SaveToStream(const DataStream: TStream); override;
procedure Clear; override;
procedure InsertAndCopyRows(const FirstRow, LastRow, DestRow, aCount: integer; const SheetInfo: TSheetInfo; const OnlyFormulas: boolean);override;
procedure DeleteRows(const aRow, aCount: word; const SheetInfo: TSheetInfo);override;
procedure ArrangeInsert(const InsPos, InsCount: integer; const SheetInfo: TSheetInfo);override;
property Notes: TNoteList read FNotes;
property Cells: TCells read FCells;
function DrawingCount: integer;
procedure AssignDrawing(const Index: integer; const Data: string; const DataType: TXlsImgTypes);
property DrawingRow[index: integer]: integer read GetDrawingRow;
property DrawingName[index: integer]: widestring read GetDrawingName;
end;
implementation
{ TSheet }
function TSheet.CopyTo: TSheet;
begin
if Self= nil then Result:=nil else Result:= DoCopyTo;
end;
constructor TSheet.Create(const aWorkbookGlobals: TWorkbookGlobals);
begin
FWorkbookGlobals:=aWorkbookGlobals;
end;
function TSheet.DoCopyTo: TSheet;
begin
Result:= ClassOfTSheet(ClassType).Create(FWorkbookGlobals);
Result.BOF:= BOF.CopyTo as TBOFRecord;
Result.EOF:= EOF.CopyTo as TEOFRecord;
end;
function TSheet.GetSelected: boolean;
begin
if (FWindow2<>nil) then Result:=FWindow2.Selected else Result:=false;
end;
procedure TSheet.SetSelected(const Value: boolean);
begin
if (FWindow2<>nil) then FWindow2.Selected:=value;
end;
{ TChart }
procedure TChart.Clear;
begin
inherited;
if FChartRecords<>nil then FChartRecords.Clear;
end;
function TChart.DoCopyTo: TSheet;
begin
Result:= inherited DoCopyTo;
(Result as TChart).FChartRecords.CopyFrom(FChartRecords);
end;
constructor TChart.Create(const aWorkbookGlobals: TWorkbookGlobals);
begin
inherited;
FChartRecords:= TChartRecordList.Create;
end;
destructor TChart.Destroy;
begin
FreeAndNil(FChartRecords);
inherited;
end;
procedure TChart.LoadFromStream(const DataStream: TStream;
const First: TBOFRecord; const SST: TSST);
var
RecordHeader: TRecordHeader;
R: TBaseRecord;
begin
Clear;
repeat
if (DataStream.Read(RecordHeader, sizeof(RecordHeader)) <> sizeof(RecordHeader)) then
raise Exception.Create(ErrExcelInvalid);
R:=LoadRecord(DataStream, RecordHeader);
try
if RecordHeader.Id=xlr_WINDOW2 then FWindow2:=R as TWindow2Record;
if (R is TLabelSSTRecord) then (R as TLabelSSTRecord).AttachToSST(SST);
if (R is TBofRecord) then raise Exception.Create(ErrExcelInvalid)
else if (R is TIgnoreRecord) then FreeAndNil(R)
else if (R is TEOFRecord) then EOF:=(R as TEOFRecord)
else FChartRecords.Add(R) ;
except
FreeAndNil(R);
Raise;
end; //Finally
until RecordHeader.id = xlr_EOF;
BOF:=First; //Last statement
end;
procedure TChart.SaveToStream(const DataStream: TStream);
begin
if (BOF=nil)or(EOF=nil) then raise Exception.Create(ErrSectionNotLoaded);
BOF.SaveToStream(DataStream);
FChartRecords.SaveToStream(DataStream);
EOF.SaveToStream(DataStream);
end;
function TChart.TotalSize: int64;
begin
Result:= inherited TotalSize+
FChartRecords.TotalSize;
end;
procedure TChart.ArrangeInsert(const InsPos, InsCount: integer; const SheetInfo: TSheetInfo);
begin
FChartRecords.ArrangeInsert( InsPos, InsCount, SheetInfo);
end;
{ TChartList }
{$INCLUDE inc\TChartListImp.inc}
procedure TChartList.ArrangeInsert(const InsPos, InsCount: integer;
const SheetInfo: TSheetInfo);
var
i: integer;
begin
for i:=0 to Count -1 do Items[i].ArrangeInsert(InsPos, InsCount, SheetInfo);
end;
procedure TChartList.SaveToStream(const DataStream: TStream);
var
i:integer;
begin
for i:=0 to Count-1 do Items[i].SaveToStream(DataStream);
end;
procedure TChart.InsertAndCopyRows(const FirstRow, LastRow, DestRow,
aCount: integer; const SheetInfo: TSheetInfo; const OnlyFormulas: boolean);
begin
//Nothing, we never insert rows in a chart sheet
end;
procedure TChart.DeleteRows(const aRow, aCount: word; const SheetInfo: TSheetInfo);
begin
//Nothing, we never delete rows in a chart sheet
end;
{ TWorkSheet }
procedure TWorkSheet.Clear;
begin
inherited;
if FMiscRecords1<>nil then FMiscRecords1.Clear;
if FMiscRecords2<>nil then FMiscRecords2.Clear;
if FDrawing<>nil then FDrawing.Clear;
if FCells<>nil then FCells.Clear;
if FRanges<>nil then FRanges.Clear;
if FNotes<>nil then FNotes.Clear;
end;
function TWorkSheet.DoCopyTo: TSheet;
begin
Result:= inherited DoCopyTo;
(Result as TWorkSheet).FMiscRecords1.CopyFrom(FMiscRecords1);
(Result as TWorkSheet).FMiscRecords2.CopyFrom(FMiscRecords2);
(Result as TWorkSheet).FDrawing.CopyFrom(FDrawing);
(Result as TWorkSheet).FCells.CopyFrom(FCells);
(Result as TWorkSheet).FRanges.CopyFrom(FRanges);
(Result as TWorkSheet).FNotes.CopyFrom(FNotes);
(Result as TWorkSheet).FNotes.FixDwgIds((Result as TWorkSheet).FDrawing);
end;
constructor TWorkSheet.Create(const aWorkbookGlobals: TWorkbookGlobals);
begin
inherited;
FMiscRecords1:= TBaseRecordList.Create;
FMiscRecords2:= TBaseRecordList.Create;
FDrawing:= TDrawing.Create(FWorkbookGlobals.DrawingGroup);
FCells:= TCells.Create(aWorkbookGlobals.SST);
FRanges :=TRangeList.Create;
FNotes:= TNoteList.Create;
end;
destructor TWorkSheet.Destroy;
begin
FreeAndNil(FRanges);
FreeAndNil(FCells);
FreeAndNil(FNotes);
//FDrawing should be freed after notes
FreeAndNil(FDrawing);
FreeAndNil(FMiscRecords1);
FreeAndNil(FMiscRecords2);
inherited;
end;
procedure TWorkSheet.LoadFromStream(const DataStream: TStream;
const First: TBOFRecord; const SST: TSST);
var
RecordHeader: TRecordHeader;
R: TBaseRecord;
MiscRecords: TBaseRecordList;
FShrFmlas: TShrFmlaRecordList;
begin
Clear;
MiscRecords:=FMiscRecords1;
FShrFmlas:= TShrFmlaRecordList.Create;
try
repeat
if (DataStream.Read(RecordHeader, sizeof(RecordHeader)) <> sizeof(RecordHeader)) then
raise Exception.Create(ErrExcelInvalid);
R:=LoadRecord(DataStream, RecordHeader);
try
if RecordHeader.Id=xlr_WINDOW2 then
begin
MiscRecords:=FMiscRecords2;
FWindow2:=R as TWindow2Record;
end;
if (R is TLabelSSTRecord) then (R as TLabelSSTRecord).AttachToSST(SST);
if (R is TBofRecord) then raise Exception.Create(ErrExcelInvalid)
else if (R is TDrawingRecord) then FDrawing.LoadFromStream(DataStream, R as TDrawingRecord, SST)
else if (R is TIgnoreRecord) then FreeAndNil(R)
else if (R is TNoteRecord) then FNotes.AddRecord(R as TNoteRecord, (R as TNoteRecord).Row)
else if (R is TCellRecord) then FCells.AddCell(R as TCellRecord, (R as TCellRecord).Row)
else if (R is TMultipleValueRecord) then begin FCells.AddMultipleCells(R as TMultipleValueRecord);FreeAndNil(R);end
else if (R is TRowRecord) then FCells.AddRow(R as TRowRecord)
else if (R is TCondFmtRecord) then FRanges[FRanges.Add(TCondFmt.Create)].LoadFromStream(DataStream, R as TCondFmtRecord)
else if (R is TCellMergingRecord) then FRanges[FRanges.Add(TMergedCells.Create)].LoadFromStream(DataStream, R as TCellMergingRecord)
else if (R is TShrFmlaRecord) then FShrFmlas.Add(R as TShrFmlaRecord)
else if (R is TEOFRecord) then EOF:=(R as TEOFRecord)
else MiscRecords.Add(R) ;
except
FreeAndNil(R);
Raise;
end; //Finally
until RecordHeader.id = xlr_EOF;
FNotes.FixDwgIds(FDrawing);
FCells.CellList.FixFormulas(FShrFmlas);
finally
FreeAndNil(FShrFmlas);
end; //finally
//this must be the last statment, so if there is an exception, we dont take First
BOF:= First;
end;
procedure TWorkSheet.SaveToStream(const DataStream: TStream);
begin
if (BOF=nil)or(EOF=nil) then raise Exception.Create(ErrSectionNotLoaded);
BOF.SaveToStream(DataStream);
FMiscRecords1.SaveToStream(DataStream);
FCells.SaveToStream(DataStream);
FDrawing.SaveToStream(DataStream);
FNotes.SaveToStream(DataStream);
FMiscRecords2.SaveToStream(DataStream);
FRanges.SaveToStream(DataStream);
EOF.SaveToStream(DataStream);
end;
function TWorkSheet.TotalSize: int64;
begin
Result:= inherited TotalSize+
FMiscRecords1.TotalSize +
FCells.TotalSize +
FRanges.TotalSize +
FDrawing.TotalSize +
FMiscRecords2.TotalSize+
FNotes.TotalSize;
end;
procedure TWorkSheet.InsertAndCopyRows(const FirstRow, LastRow, DestRow, aCount: integer; const SheetInfo: TSheetInfo; const OnlyFormulas: boolean);
begin
FCells.InsertAndCopyRows(FirstRow, LastRow, DestRow, aCount, SheetInfo, OnlyFormulas);
FDrawing.InsertAndCopyRows(FirstRow, LastRow, DestRow, aCount, SheetInfo);
FRanges.InsertAndCopyRows(FirstRow, LastRow, DestRow, aCount, SheetInfo);
FNotes.InsertAndCopyRows(FirstRow, LastRow, DestRow, aCount, SheetInfo, false);
end;
procedure TWorkSheet.DeleteRows(const aRow, aCount: word; const SheetInfo: TSheetInfo);
begin
FCells.DeleteRows(aRow, aCount, SheetInfo);
FDrawing.DeleteRows(aRow, aCount, SheetInfo);
FRanges.DeleteRows(aRow, aCount, SheetInfo);
FNotes.DeleteRows(aRow, aCount, SheetInfo);
end;
procedure TWorkSheet.ArrangeInsert(const InsPos, InsCount: integer; const SheetInfo: TSheetInfo);
begin
//PENDING: Optimize this
FCells.ArrangeInsert(InsPos, InsCount, SheetInfo);
FDrawing.ArrangeInsert(InsPos, InsCount, SheetInfo);
end;
procedure TWorkSheet.AssignDrawing(const Index: integer; const Data: string;
const DataType: TXlsImgTypes);
begin
FDrawing.AssignDrawing( Index, Data, DataType);
end;
function TWorkSheet.DrawingCount: integer;
begin
Result:= FDrawing.DrawingCount;
end;
function TWorkSheet.GetDrawingRow(index: integer): integer;
begin
Result:= FDrawing.DrawingRow[index];
end;
function TWorkSheet.GetDrawingName(index: integer): widestring;
begin
Result:= FDrawing.DrawingName[index];
end;
end.