From d887861a83c53fa381f7fd2aa0eb15fede03a5fb Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Thu, 1 Sep 2011 07:55:12 +0000 Subject: [PATCH] fpspreadsheet: Adds bold support to ooxml git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1883 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/ooxmldemo/ooxmlwrite.lpr | 24 ++++++++++++----- components/fpspreadsheet/xlsxooxml.pas | 26 +++++++++++++------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/components/fpspreadsheet/examples/ooxmldemo/ooxmlwrite.lpr b/components/fpspreadsheet/examples/ooxmldemo/ooxmlwrite.lpr index 8a43892d9..35c000716 100644 --- a/components/fpspreadsheet/examples/ooxmldemo/ooxmlwrite.lpr +++ b/components/fpspreadsheet/examples/ooxmldemo/ooxmlwrite.lpr @@ -18,6 +18,7 @@ var MyDir: string; i: Integer; a: TStringList; + MyCell: PCell; begin // Open the output file MyDir := ExtractFilePath(ParamStr(0)); @@ -32,15 +33,24 @@ begin MyWorksheet.WriteNumber(0, 2, 3.0); MyWorksheet.WriteNumber(0, 3, 4.0); -{ Uncommend this to test large XLS files - for i := 2 to 20 do +// Uncommend this to test large XLS files + for i := 2 to 2{20} do begin - MyWorksheet.WriteAnsiText(i, 0, ParamStr(0)); - MyWorksheet.WriteAnsiText(i, 1, ParamStr(0)); - MyWorksheet.WriteAnsiText(i, 2, ParamStr(0)); - MyWorksheet.WriteAnsiText(i, 3, ParamStr(0)); + MyWorksheet.WriteUTF8Text(i, 0, ParamStr(0)); + MyWorksheet.WriteUTF8Text(i, 1, ParamStr(0)); + MyWorksheet.WriteUTF8Text(i, 2, ParamStr(0)); + MyWorksheet.WriteUTF8Text(i, 3, ParamStr(0)); end; -} + + // Test for Bold + MyCell := MyWorksheet.GetCell(2, 0); + MyCell^.UsedFormattingFields := [uffBold]; + MyCell := MyWorksheet.GetCell(2, 1); + MyCell^.UsedFormattingFields := [uffBold]; + MyCell := MyWorksheet.GetCell(2, 2); + MyCell^.UsedFormattingFields := [uffBold]; + MyCell := MyWorksheet.GetCell(2, 3); + MyCell^.UsedFormattingFields := [uffBold]; // Creates a new worksheet MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet 2'); diff --git a/components/fpspreadsheet/xlsxooxml.pas b/components/fpspreadsheet/xlsxooxml.pas index 4f1fc60c4..8b6fcc3e4 100755 --- a/components/fpspreadsheet/xlsxooxml.pas +++ b/components/fpspreadsheet/xlsxooxml.pas @@ -57,6 +57,7 @@ type procedure WriteGlobalFiles(AData: TsWorkbook); procedure WriteContent(AData: TsWorkbook); procedure WriteWorksheet(CurSheet: TsWorksheet); + function GetStyleIndex(ACell: PCell): Cardinal; public destructor Destroy; override; { General writing methods } @@ -143,11 +144,9 @@ begin FStyles := XML_HEADER + LineEnding + '' + LineEnding + - ' ' + LineEnding + - ' ' + LineEnding + - ' ' + LineEnding + - ' ' + LineEnding + - ' ' + LineEnding + + ' ' + LineEnding + + ' ' + LineEnding + + ' ' + LineEnding + ' ' + LineEnding + ' ' + LineEnding + ' ' + LineEnding + @@ -166,11 +165,13 @@ begin ' ' + LineEnding + ' ' + LineEnding + ' ' + LineEnding + - ' ' + LineEnding + + ' ' + LineEnding + ' ' + LineEnding + + ' ' + LineEnding + ' ' + LineEnding + - ' ' + LineEnding + + ' ' + LineEnding + ' ' + LineEnding + + ' ' + LineEnding + ' ' + LineEnding + ' ' + LineEnding + ' ' + LineEnding + @@ -340,6 +341,13 @@ begin ''; end; +// This is an index to the section cellXfs from the styles.xml file +function TsSpreadOOXMLWriter.GetStyleIndex(ACell: PCell): Cardinal; +begin + if uffBold in ACell^.UsedFormattingFields then Result := 1 + else Result := 0; +end; + destructor TsSpreadOOXMLWriter.Destroy; begin SetLength(FSheets, 0); @@ -439,6 +447,7 @@ procedure TsSpreadOOXMLWriter.WriteLabel(AStream: TStream; const ARow, ACol: Word; const AValue: string; ACell: PCell); var CellPosText: string; + lStyleIndex: Cardinal; begin FSharedStrings := FSharedStrings + ' ' + LineEnding + @@ -446,8 +455,9 @@ begin ' ' + LineEnding; CellPosText := TsWorksheet.CellPosToText(ARow, ACol); + lStyleIndex := GetStyleIndex(ACell); FSheets[FCurSheetNum] := FSheets[FCurSheetNum] + - Format(' %d', [CellPosText, FSharedStringsCount]) + LineEnding; + Format(' %d', [CellPosText, lStyleIndex, FSharedStringsCount]) + LineEnding; Inc(FSharedStringsCount); end;