From 9b6a4ba327eb351f3a899035dee822f8c2215f52 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 8 Oct 2016 20:43:47 +0000 Subject: [PATCH] fpspreadsheet: Write column and row formats to xlsx git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5253 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpspreadsheet/xlscommon.pas | 9 ++++++++- components/fpspreadsheet/xlsxooxml.pas | 25 +++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/components/fpspreadsheet/xlscommon.pas b/components/fpspreadsheet/xlscommon.pas index 4fd4c0a7d..a09b8759c 100644 --- a/components/fpspreadsheet/xlscommon.pas +++ b/components/fpspreadsheet/xlscommon.pas @@ -3241,6 +3241,7 @@ type var rec: TColRecord; w: Integer; + width: Single; begin if Assigned(ACol) then begin @@ -3256,7 +3257,13 @@ begin rec.EndCol := WordToLE(ACol^.Col); { calculate width to be in units of 1/256 of pixel width of character "0" } - w := round(FWorkbook.ConvertUnits(ACol^.Width, FWorkbook.Units, suChars)*256); + case ACol^.ColWidthType of + cwtDefault: + width := FWorksheet.ReadDefaultColWidth(suChars); + cwtCustom: + width := FWorkbook.ConvertUnits(ACol^.Width, FWorkbook.Units, suChars); + end; + w := round(width * 256); rec.ColWidth := WordToLE(w); rec.XFIndex := WordToLE(FindXFIndex(ACol^.FormatIndex));// Index of XF record diff --git a/components/fpspreadsheet/xlsxooxml.pas b/components/fpspreadsheet/xlsxooxml.pas index 6713cc9a8..10858b2c7 100755 --- a/components/fpspreadsheet/xlsxooxml.pas +++ b/components/fpspreadsheet/xlsxooxml.pas @@ -2526,23 +2526,32 @@ end; procedure TsSpreadOOXMLWriter.WriteCols(AStream: TStream; AWorksheet: TsWorksheet); var - col: PCol; + lCol: PCol; c: Integer; w: Single; + customWidth: String; + customStyle: String; begin AppendToStream(AStream, ''); for c:=0 to AWorksheet.GetLastColIndex do begin - col := AWorksheet.FindCol(c); + customWidth := ''; + customStyle := ''; + lCol := AWorksheet.FindCol(c); + // The column width is needed in suChars here. - if col <> nil then - w := FWorkbook.ConvertUnits(col^.Width, FWorkbook.Units, suChars) - else - w := AWorksheet.ReadDefaultColWidth(suChars); + w := AWorksheet.ReadDefaultColWidth(suChars); + if lCol <> nil then begin + if lCol^.ColWidthType = cwtCustom then begin + w := FWorkbook.ConvertUnits(lCol^.Width, FWorkbook.Units, suChars); + customWidth := 'customWidth="1" '; + end; + if lCol^.FormatIndex > 0 then customStyle := Format('style="%d" ', [lCol^.FormatIndex]); + end; AppendToStream(AStream, Format( - '', - [c+1, c+1, w], FPointSeparatorSettings) + '', + [c+1, c+1, w, customWidth, customStyle], FPointSeparatorSettings) ); end;