fpspreadsheet: Write custom row heights and column widths to xlsx files. Works in virtual mode also.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3320 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-07-15 13:31:45 +00:00
parent 32a5a26fe8
commit e3c0eb2dc0
3 changed files with 55 additions and 2 deletions

View File

@ -36,6 +36,9 @@ begin
MyWorksheet.WriteUTF8Text(0, 26, 'AA'); // Test for column name
MyWorksheet.WriteColWidth(0, 20);
MyWorksheet.WriteRowHeight(0, 4);
// Uncomment this to test large XLS files
for i := 2 to 2{20} do
begin

View File

@ -90,6 +90,8 @@ begin
worksheet.WriteBackgroundColor(0, 0, scSilver);
headerTemplate := worksheet.FindCell(0, 0);
worksheet.WriteRowHeight(0, 3);
worksheet.WriteColWidth(0, 30);
{ In case of a database, you would open the dataset before calling this: }
workbook.WriteToFile('test_virtual.xlsx', sfOOXML, true);
// workbook.WriteToFile('test_virtual.xls', sfExcel8, true);

View File

@ -19,6 +19,9 @@ Specifications obtained from:
http://openxmldeveloper.org/default.aspx
also:
http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx#BMworksheetworkbook
AUTHORS: Felipe Monteiro de Carvalho
}
unit xlsxooxml;
@ -72,6 +75,7 @@ type
procedure ListAllFills;
procedure ResetStreams;
procedure WriteBorderList(AStream: TStream);
procedure WriteCols(AStream: TStream; ASheet: TsWorksheet);
procedure WriteFillList(AStream: TStream);
procedure WriteFontList(AStream: TStream);
procedure WriteNumFormatList(AStream: TStream);
@ -443,6 +447,30 @@ begin
'</borders>');
end;
procedure TsSpreadOOXMLWriter.WriteCols(AStream: TStream; ASheet: TsWorksheet);
var
col: PCol;
c: Integer;
begin
if ASheet.Cols.Count = 0 then
exit;
AppendToStream(AStream,
'<cols>');
for c:=0 to ASheet.GetLastColIndex do begin
col := ASheet.FindCol(c);
if col <> nil then
AppendToStream(AStream, Format(
'<col min="%d" max="%d" width="%g" customWidth="1" />',
[c+1, c+1, col.Width])
);
end;
AppendToStream(AStream,
'</cols>');
end;
procedure TsSpreadOOXMLWriter.WriteFillList(AStream: TStream);
var
i: Integer;
@ -848,9 +876,13 @@ var
value: Variant;
styleCell: PCell;
fn: String;
row: PRow;
rh: String;
h0: Single;
begin
FCurSheetNum := Length(FSSheets);
SetLength(FSSheets, FCurSheetNum + 1);
h0 := Workbook.GetDefaultFontSize; // Point size of default font
// Create the stream
if (woSaveMemory in Workbook.WritingOptions) then begin
@ -871,14 +903,23 @@ begin
'<sheetView workbookViewId="0" />');
AppendToStream(FSSheets[FCurSheetNum],
'</sheetViews>');
WriteCols(FSSheets[FCurSheetNum], CurSheet);
AppendToStream(FSSheets[FCurSheetNum],
'<sheetData>');
if (woVirtualMode in Workbook.WritingOptions) and Assigned(Workbook.OnNeedCellData)
then begin
for r := 0 to Workbook.VirtualRowCount-1 do begin
row := CurSheet.FindRow(r);
if row <> nil then
rh := Format(' ht="%g" customHeight="1"', [
(row^.Height + ROW_HEIGHT_CORRECTION)*h0])
else
rh := '';
AppendToStream(FSSheets[FCurSheetNum], Format(
'<row r="%d" spans="1:%d">', [r+1, Workbook.VirtualColCount]));
'<row r="%d" spans="1:%d"%s>', [r+1, Workbook.VirtualColCount, rh]));
for c := 0 to Workbook.VirtualColCount-1 do begin
FillChar(lCell, SizeOf(lCell), 0);
CellPosText := CurSheet.CellPosToText(r, c);
@ -920,8 +961,15 @@ begin
// The cells need to be written in order, row by row, cell by cell
LastColIndex := CurSheet.GetLastColIndex;
for r := 0 to CurSheet.GetLastRowIndex do begin
// If the row has a custom height add this value to the <row> specification
row := CurSheet.FindRow(r);
if row <> nil then
rh := Format(' ht="%g" customHeight="1"', [
(row^.Height + ROW_HEIGHT_CORRECTION)*h0])
else
rh := '';
AppendToStream(FSSheets[FCurSheetNum], Format(
'<row r="%d" spans="1:%d">', [r+1, LastColIndex+1]));
'<row r="%d" spans="1:%d"%s>', [r+1, LastColIndex+1, rh]));
// Write cells belonging to this row.
for c := 0 to LastColIndex do begin
LCell.Row := r;