From 62150b4aca3064a6d768dbcbb601917da7aec027 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sun, 20 Sep 2015 22:41:07 +0000 Subject: [PATCH] fpspreadsheet: Add support for column width and row height to ExcelXML writer. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4347 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../read_write/excelxmldemo/excelxmlwrite.lpr | 14 +++-- components/fpspreadsheet/xlsxml.pas | 52 ++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/components/fpspreadsheet/examples/read_write/excelxmldemo/excelxmlwrite.lpr b/components/fpspreadsheet/examples/read_write/excelxmldemo/excelxmlwrite.lpr index 73984aa2e..651c68b41 100644 --- a/components/fpspreadsheet/examples/read_write/excelxmldemo/excelxmlwrite.lpr +++ b/components/fpspreadsheet/examples/read_write/excelxmldemo/excelxmlwrite.lpr @@ -1,10 +1,14 @@ -{ -excelxmlwrite.lpr +{ ------------------------------------------------------------------------------ + excelxmlwrite.lpr -Demonstrates how to write an Excel XP/2003 xml file using the fpspreadsheet library + Demonstrates how to write an Excel XP/2003 xml file using the fpspreadsheet + library -AUTHORS: Werner Pamler -} + AUTHORS: Werner Pamler + + LICENSE : For details about the license, see the file + COPYING.modifiedLGPL.txt included in the Lazarus distribution. +-------------------------------------------------------------------------------} program excelxmlwrite; {$mode delphi}{$H+} diff --git a/components/fpspreadsheet/xlsxml.pas b/components/fpspreadsheet/xlsxml.pas index d22b34d17..f0966e806 100644 --- a/components/fpspreadsheet/xlsxml.pas +++ b/components/fpspreadsheet/xlsxml.pas @@ -261,25 +261,63 @@ var c, c1, c2: Cardinal; r, r1, r2: Cardinal; cell: PCell; + rowheightStr: String; + colwidthStr: String; + defFnt: TsFont; + col: PCol; + row: PRow; + cw_fact, rh_fact: Double; begin + defFnt := FWorkbook.GetDefaultFont; + cw_fact := defFnt.Size * 0.5; // ColWidthFactor = Approx width of "0" character in pts + rh_fact := defFnt.Size; // RowHeightFactor = Height of a single line + r1 := 0; c1 := 0; r2 := AWorksheet.GetLastRowIndex; c2 := AWorksheet.GetLastColIndex; AppendToStream(AStream, TABLE_INDENT + Format( '' + LF, [ - AWorksheet.GetLastColIndex + 1, AWorksheet.GetLastRowIndex + 1 - ])); + 'x:FullColumns="1" x:FullRows="1" ' + + 'ss:DefaultColumnWidth="%.2f" ' + + 'ss:DefaultRowHeight="%.2f">' + LF, + [ + AWorksheet.GetLastColIndex + 1, AWorksheet.GetLastRowIndex + 1, + FWorksheet.DefaultColWidth * cw_fact, + (FWorksheet.DefaultRowHeight + ROW_HEIGHT_CORRECTION) * rh_fact + ], + FPointSeparatorSettings + )); for c := c1 to c2 do - AppendToStream(AStream, COL_INDENT + - '' + LF); + begin + col := FWorksheet.FindCol(c); + // column width in the worksheet is in multiples of the "0" character width. + // In the xml file, it is needed in pts. + if Assigned(col) then + colwidthStr := Format(' ss:Width="%0.2f"', + [col^.Width * cw_fact], + FPointSeparatorSettings) + else + colwidthStr := ''; + AppendToStream(AStream, COL_INDENT + Format( + '' + LF, [c+1, colWidthStr])); + end; for r := r1 to r2 do begin - AppendToStream(AStream, ROW_INDENT + - '' + LF); + row := FWorksheet.FindRow(r); + // Row height in the worksheet is in multiples of the default font height + // In the xml file, it is needed in pts. + if Assigned(row) then + rowheightStr := Format(' ss:Height="%.2f"', + [(row^.Height + ROW_HEIGHT_CORRECTION) * rh_fact], + FPointSeparatorSettings + ) + else + rowheightStr := ''; + AppendToStream(AStream, ROW_INDENT + Format( + '' + LF, [rowheightStr])); for c := c1 to c2 do begin cell := AWorksheet.FindCell(r, c);