diff --git a/components/fpspreadsheet/examples/opendocdemo/opendocread.lpi b/components/fpspreadsheet/examples/opendocdemo/opendocread.lpi index d6dc9afdd..997276a13 100644 --- a/components/fpspreadsheet/examples/opendocdemo/opendocread.lpi +++ b/components/fpspreadsheet/examples/opendocdemo/opendocread.lpi @@ -1,4 +1,4 @@ - + @@ -44,7 +44,7 @@ - + diff --git a/components/fpspreadsheet/fpsopendocument.pas b/components/fpspreadsheet/fpsopendocument.pas index 8c62fd5a2..3d7efbd20 100755 --- a/components/fpspreadsheet/fpsopendocument.pas +++ b/components/fpspreadsheet/fpsopendocument.pas @@ -58,6 +58,7 @@ type TsSpreadOpenDocReader = class(TsCustomSpreadReader) private + FDoc : TXMLDocument; FDateMode: TDateMode; FWorksheet: TsWorksheet; // Gets value for the specified attribute. Returns empty string if attribute @@ -67,6 +68,7 @@ type procedure ReadDateMode(SpreadSheetNode: TDOMNode); protected procedure CreateNumFormatList; override; + procedure ReadAutomaticStyles; { Record writing methods } procedure ReadFormula(ARow : Word; ACol : Word; ACellNode: TDOMNode); procedure ReadLabel(ARow : Word; ACol : Word; ACellNode: TDOMNode); @@ -122,6 +124,9 @@ type implementation +uses + StrUtils; + const { OpenDocument general XML constants } XML_HEADER = ''; @@ -174,7 +179,7 @@ const procedure TsSpreadOpenDocNumFormatList.AddBuiltinFormats; begin - // to be filled later... + // there are no built-in number formats which are silently assumed to exist. end; { TsSpreadOpenDocReader } @@ -234,7 +239,6 @@ var FilePath : string; UnZip : TUnZipper; FileList : TStringList; - Doc : TXMLDocument; BodyNode, SpreadSheetNode, TableNode, RowNode, CellNode : TDOMNode; ParamRowsRepeated, ParamColsRepeated, ParamValueType, ParamFormula : string; RowsCount, ColsCount : integer; @@ -252,13 +256,15 @@ begin FreeAndNil(UnZip); end; //try - Doc:=nil; + FDoc := nil; try //process the xml file - ReadXMLFile(Doc,FilePath+'content.xml'); + ReadXMLFile(FDoc, FilePath+'content.xml'); DeleteFile(FilePath+'content.xml'); - BodyNode:= Doc.DocumentElement.FindNode('office:body'); + ReadAutomaticStyles; + + BodyNode := FDoc.DocumentElement.FindNode('office:body'); if not Assigned(BodyNode) then Exit; SpreadSheetNode:=BodyNode.FindNode('office:spreadsheet'); @@ -323,7 +329,7 @@ begin TableNode:=TableNode.NextSibling; end; //while Assigned(TableNode) finally - Doc.Free; + FDoc.Free; end; end; @@ -466,6 +472,56 @@ begin end; end; +procedure TsSpreadOpenDocReader.ReadAutomaticStyles; +var + StylesNode, NumFormatNode, node: TDOMNode; + decs: Integer; + fmtName: String; + grouping: boolean; + fmt: String; + nf: TsNumberFormat; + nex: Integer; +begin + StylesNode := FDoc.DocumentElement.FindNode('office:automatic-styles'); + if not Assigned(StylesNode) then Exit; + + NumFormatNode := StylesNode.FirstChild; + while Assigned(NumFormatNode) do begin + if NumFormatNode.NodeName = 'number:number-style' then begin + fmtName := GetAttrValue(NumFormatNode, 'style:name'); + node := NumFormatNode.FindNode('number:number'); + if node <> nil then begin + decs := StrToInt(GetAttrValue(node, 'number:decimal-places')); + grouping := GetAttrValue(node, 'grouping') = 'true'; + nf := IfThen(grouping, nfFixedTh, nfFixed); + fmt := BuildNumberFormatString(nf, Workbook.FormatSettings, decs); + NumFormatList.AddFormat(fmt, nf, decs); + end; + node := NumFormatNode.FindNode('number:scientific-number'); + if node <> nil then begin + nf := nfExp; + decs := StrToInt(GetAttrValue(node, 'number:decimal-places')); + nex := StrToInt(GetAttrValue(node, 'number:min-exponent-digits')); + fmt := BuildNumberFormatString(nfFixed, Workbook.FormatSettings, decs); + fmt := fmt + 'E+' + DupeString('0', nex); + NumFormatList.AddFormat(fmt, nf, decs); + end; + end else + if NumFormatNode.NodeName = 'number:percentage-style' then begin + fmtName := GetAttrValue(NumFormatNode, 'style:name'); + node := NumFormatNode.FindNode('number:number'); + if node <> nil then begin + nf := nfPercentage; + decs := StrToInt(GetAttrValue(node, 'number:decimal-places')); + fmt := BuildNumberFormatString(nf, Workbook.FormatSettings, decs); + NumFormatList.AddFormat(fmt, nf, decs); + end; + end; + NumFormatNode := NumFormatNode.NextSibling; + end; +end; + + { TsSpreadOpenDocWriter } procedure TsSpreadOpenDocWriter.CreateNumFormatList;