diff --git a/components/fpspreadsheet/xlsxml.pas b/components/fpspreadsheet/xlsxml.pas index 858bb9aa7..66bc47146 100644 --- a/components/fpspreadsheet/xlsxml.pas +++ b/components/fpspreadsheet/xlsxml.pas @@ -1,3 +1,17 @@ +{@@ ---------------------------------------------------------------------------- + Unit: xlsxml + + implements a reader and writer for the SpreadsheetXML format. + This document was introduced by Microsoft for Excel XP and 2003. + + REFERENCE: https://msdn.microsoft.com/en-us/library/aa140066%28v=office.15%29.aspx + + AUTHOR : Werner Pamler + + LICENSE : See the file COPYING.modifiedLGPL.txt, included in the Lazarus + distribution, for details about the license. +-------------------------------------------------------------------------------} + unit xlsxml; {$ifdef fpc} @@ -70,7 +84,18 @@ uses fpsStrings, fpsUtils, fpsStreams, fpsNumFormat, fpsHTMLUtils; const - FMT_OFFSET = 61; + FMT_OFFSET = 61; + INDENT1 = ' '; + INDENT2 = ' '; + INDENT3 = ' '; + INDENT4 = ' '; + INDENT5 = ' '; + VALUE_INDENT = INDENT5; + CELL_INDENT = INDENT4; + ROW_INDENT = INDENT3; + COL_INDENT = INDENT3; + TABLE_INDENT = INDENT2; + LF = LineEnding; const { TsFillStyle = ( @@ -150,7 +175,7 @@ begin Result := ''; comment := FWorksheet.FindComment(ACell); if Assigned(comment) then - Result := '' + comment^.Text + ''; + Result := INDENT1 + '' + comment^.Text + '' + LF + CELL_INDENT; // If there will be some rich-text-like formatting in the future, use // Result := ''+comment^.Text+'': end; @@ -194,10 +219,10 @@ end; procedure TsSpreadExcelXMLWriter.WriteBlank(AStream: TStream; const ARow, ACol: Cardinal; ACell: PCell); begin - AppendToStream(AStream, Format( - ' ' + // colIndex, style, hyperlink, merge - '%s' + // Comment ... - '' + LineEnding, [ + AppendToStream(AStream, Format(CELL_INDENT + + '' + // colIndex, style, hyperlink, merge + '%s' + // Comment ... + '' + LF, [ GetIndexStr(ACol+1), GetStyleStr(ACell), GetHyperlinkStr(ACell), GetMergeStr(ACell), GetCommentStr(ACell) ])); @@ -219,13 +244,13 @@ begin cctStr := GetCellContentTypeStr(ACell); end; - AppendToStream(AStream, Format( - ' ' + // colIndex, style, formula, hyperlink, merge - '' + // data type - '%s' + // value string - '' + - '%s' + // Comment ... - '' + LineEnding, [ + AppendToStream(AStream, Format(CELL_INDENT + + '' + // colIndex, style, formula, hyperlink, merge + '' + // data type + '%s' + // value string + '' + + '%s' + // Comment ... + '' + LF, [ GetIndexStr(ACol+1), GetStyleStr(ACell), formulaStr, GetHyperlinkStr(ACell), GetMergeStr(ACell), cctStr, valueStr, @@ -243,16 +268,16 @@ begin c1 := 0; r2 := AWorksheet.GetLastRowIndex; c2 := AWorksheet.GetLastColIndex; - AppendToStream(AStream, - '' + LineEnding); + AppendToStream(AStream, TABLE_INDENT + + '
' + LF); for c := c1 to c2 do - AppendToStream(AStream, - ' ' + LineEnding); + AppendToStream(AStream, COL_INDENT + + '' + LF); for r := r1 to r2 do begin - AppendToStream(AStream, - ' ' + LineEnding); + AppendToStream(AStream, ROW_INDENT + + '' + LF); for c := c1 to c2 do begin cell := AWorksheet.FindCell(r, c); @@ -263,12 +288,12 @@ begin WriteCellToStream(AStream, cell); end; end; - AppendToStream(AStream, - ' ' + LineEnding); + AppendToStream(AStream, ROW_INDENT + + '' + LF); end; - AppendToStream(AStream, - '
' + LineEnding); + AppendToStream(AStream, TABLE_INDENT + + '' + LF); end; procedure TsSpreadExcelXMLWriter.WriteCellToStream(AStream: TStream; ACell: PCell); @@ -324,13 +349,13 @@ begin cctStr := GetCellContentTypeStr(ACell); end; - AppendToStream(AStream, Format( - ' ' + // colIndex, style, formula, hyperlink, merge - '' + // data type - '%s' + // value string - '' + - '%s' + // Comment ... - '' + LineEnding, [ + AppendToStream(AStream, Format(CELL_INDENT + + '' + LF + VALUE_INDENT + // colIndex, style, formula, hyperlink, merge + '' + // data type + '%s' + // value string + '' + LF + CELL_INDENT + + '%s' + // Comment ... + '' + LF, [ GetIndexStr(ACol+1), GetStyleStr(ACell), formulaStr, GetHyperlinkStr(ACell), GetMergeStr(ACell), cctStr, valueStr, @@ -355,13 +380,13 @@ begin formulaStr := Format(' ss:Formula="=%s"', [ACell^.FormulaValue]); end; - AppendToStream(AStream, Format( - ' ' + // colIndex, style, formula, hyperlink, merge - '' + // data type - '%s' + // value string - '' + - '%s' + // Comment ... - '' + LineEnding, [ + AppendToStream(AStream, Format(CELL_INDENT + + '' + LF + VALUE_INDENT + // colIndex, style, formula, hyperlink, merge + '' + // data type + '%s' + // value string + '' + LF + CELL_INDENT + + '%s' + // Comment ... + '' + LF, [ GetIndexStr(ACol+1), GetStyleStr(ACell), formulaStr, GetHyperlinkStr(ACell), GetMergeStr(ACell), cctStr, valueStr, @@ -374,15 +399,15 @@ var datemodeStr: String; begin if FDateMode = dm1904 then - datemodeStr := ' ' + LineEnding else + datemodeStr := INDENT2 + '' + LF else datemodeStr := ''; - AppendToStream(AStream, - '' + LineEnding + - datemodeStr + - 'False' + LineEnding + - 'False' + LineEnding + - '' + LineEnding); + AppendToStream(AStream, INDENT1 + + '' + LF + + datemodeStr + INDENT2 + + 'False' + LF + INDENT2 + + 'False' + LF + INDENT1 + + '' + LF); end; procedure TsSpreadExcelXMLWriter.WriteLabel(AStream: TStream; const ARow, @@ -425,13 +450,13 @@ begin formulaStr := Format(' ss:Formula="=%s"', [ACell^.FormulaValue]); end; - AppendToStream(AStream, Format( - ' ' + // colIndex, style, formula, hyperlink, merge - '<%sData ss:Type="%s"%s>'+ // "ss:", data type, "xmlns=.." - '%s' + // value string - '' + // "ss:" - '%s' + // Comment - '' + LineEnding, [ + AppendToStream(AStream, Format(CELL_INDENT + + '' + LF + VALUE_INDENT + // colIndex, style, formula, hyperlink, merge + '<%sData ss:Type="%s"%s>'+ // "ss:", data type, "xmlns=.." + '%s' + // value string + '' + LF + CELL_INDENT + // "ss:" + '%s' + // Comment + '' + LF, [ GetIndexStr(ACol+1), GetStyleStr(ACell), formulaStr, GetHyperlinkStr(ACell), GetMergeStr(ACell), dataTagStr, cctStr, xmlnsStr, valueStr, @@ -453,13 +478,13 @@ begin formulaStr := Format(' ss:Formula="=%s"', [ACell^.FormulaValue]); end; - AppendToStream(AStream, Format( - ' ' + // colIndex, style, formula, hyperlink, merge - '' + // data type - '%g' + // value - '' + - '%s' + // Comment ... - '' + LineEnding, [ + AppendToStream(AStream, Format(CELL_INDENT + + '' + LF + VALUE_INDENT + // colIndex, style, formula, hyperlink, merge + '' + // data type + '%g' + // value + '' + LF + CELL_INDENT + + '%s' + // Comment ... + '' + LF, [ GetIndexStr(ACol+1), GetStyleStr(ACell), formulaStr, GetHyperlinkStr(ACell), GetMergeStr(ACell), cctStr, AValue, @@ -480,21 +505,21 @@ begin deffnt := FWorkbook.GetDefaultFont; if AIndex = 0 then begin - AppendToStream(AStream, Format( - ' ' + LineEnding, + AppendToStream(AStream, Format(INDENT2 + + '' + LF, [deffnt.FontName, round(deffnt.Size), ColorToHTMLColorStr(deffnt.Color)] ) ) end else begin - AppendToStream(AStream, Format( - ' ' + LineEnding); + AppendToStream(AStream, INDENT2 + + '' + LF); end; end; @@ -616,11 +641,11 @@ procedure TsSpreadExcelXMLWriter.WriteStyles(AStream: TStream); var i: Integer; begin - AppendToStream(AStream, - '' + LineEnding); + AppendToStream(AStream, INDENT1 + + '' + LF); for i:=0 to FWorkbook.GetNumCellFormats-1 do WriteStyle(AStream, i); - AppendToStream(AStream, - '' + LineEnding); + AppendToStream(AStream, INDENT1 + + '' + LF); end; {@@ ---------------------------------------------------------------------------- @@ -654,15 +679,15 @@ end; procedure TsSpreadExcelXMLWriter.WriteToStream(AStream: TStream); begin AppendToStream(AStream, - '' + LineEnding + - '' + LineEnding + '' + LF + + '' + LF ); AppendToStream(AStream, - '' + LineEnding); + '' + LF); WriteExcelWorkbook(AStream); WriteStyles(AStream); @@ -677,11 +702,10 @@ procedure TsSpreadExcelXMLWriter.WriteWorksheet(AStream: TStream; begin FWorksheet := AWorksheet; AppendToStream(AStream, Format( - '' + LineEnding, [AWorksheet.Name]) - ); + ' ' + LF, [AWorksheet.Name]) ); WriteCells(AStream, AWorksheet); AppendToStream(AStream, - '' + LineEnding + ' ' + LF ); end;