From 05bdb8bb8861602a0db8a2c63139afac1485a01d Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 24 Jul 2017 15:18:13 +0000 Subject: [PATCH] fpspreadsheet: Fix violated file formation limitations when reading some xls files (http://www.lazarusforum.de/viewtopic.php?f=18&t=10824&start=15) git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5987 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/source/common/xlsbiff2.pas | 18 +++--------------- .../fpspreadsheet/source/common/xlsbiff5.pas | 11 +++++++---- .../fpspreadsheet/source/common/xlsbiff8.pas | 11 +++++++---- .../fpspreadsheet/source/common/xlscommon.pas | 1 + 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/components/fpspreadsheet/source/common/xlsbiff2.pas b/components/fpspreadsheet/source/common/xlsbiff2.pas index 71f24d5d1..6890252b9 100644 --- a/components/fpspreadsheet/source/common/xlsbiff2.pas +++ b/components/fpspreadsheet/source/common/xlsbiff2.pas @@ -1313,24 +1313,12 @@ procedure TsSpreadBIFF2Writer.PopulatePalette(AWorkbook: TsWorkbook); begin FPalette.Clear; FPalette.AddBuiltinColors(false); - // The next instruction creates an error log entry if the workbook contains - // more colors than the default 8. This is because BIFF2 can only have a - // palette with 8 colors. + { The next instruction creates an error log entry in CheckLimitations + if the workbook contains more colors than the default 8. + This is because BIFF2 can only have a palette with 8 colors. } FPalette.CollectFromWorkbook(AWorkbook); end; - (* -{@@ ---------------------------------------------------------------------------- - Builds up the list of number formats to be written to the biff2 file. - Unlike biff5+ no formats are added here because biff2 supports only 21 - standard formats; these formats have been added by AddBuiltInFormats. - Nothing to do here. --------------------------------------------------------------------------------} -procedure TsSpreadBIFF2Writer.ListAllNumFormats; -begin - // Nothing to do here. -end; - *) {@@ ---------------------------------------------------------------------------- Attaches cell formatting data for the given cell to the current record. Is called from all writing methods of cell contents and rows diff --git a/components/fpspreadsheet/source/common/xlsbiff5.pas b/components/fpspreadsheet/source/common/xlsbiff5.pas index 6ed1a580e..c24b26793 100644 --- a/components/fpspreadsheet/source/common/xlsbiff5.pas +++ b/components/fpspreadsheet/source/common/xlsbiff5.pas @@ -1242,12 +1242,15 @@ begin FPalette.Clear; FPalette.AddBuiltinColors(true); FPalette.CollectFromWorkbook(AWorkbook); - // Fill up Excel colors of the standard palette to avoid empty color - // place holders in Excel's colordialog. - for i := 16 to High(PALETTE_BIFF5) do + + { Fill up Excel colors of the standard palette to avoid empty color + place holders in Excel's colordialog. } + i := 16; + while (i <= High(PALETTE_BIFF5)) and (FPalette.Count < 64) do begin FPalette.AddColor(PALETTE_BIFF5[i]); // The BIFF5 palette contains duplicate colors -> don't use AddUniqueColor -// FPalette.AddUniqueColor(PALETTE_BIFF5[i]); + inc(i); + end; end; {@@ ---------------------------------------------------------------------------- diff --git a/components/fpspreadsheet/source/common/xlsbiff8.pas b/components/fpspreadsheet/source/common/xlsbiff8.pas index ba60c1fa4..edd429b4a 100644 --- a/components/fpspreadsheet/source/common/xlsbiff8.pas +++ b/components/fpspreadsheet/source/common/xlsbiff8.pas @@ -2238,12 +2238,15 @@ begin FPalette.Clear; FPalette.AddBuiltinColors(true); FPalette.CollectFromWorkbook(AWorkbook); - // Fill up Excel colors of the standard palette to avoid empty color - // place holders in Excel's colordialog. - for i := 16 to High(PALETTE_BIFF8) do + + { Fill up Excel colors of the standard palette to avoid empty color + place holders in Excel's colordialog. } + i := 16; + while (i <= High(PALETTE_BIFF8)) and (FPalette.Count < 64) do begin FPalette.AddColor(PALETTE_BIFF8[i]); // The BIFF8 palette contains duplicate colors -> don't use AddUniqueColor -// FPalette.AddUniqueColor(PALETTE_BIFF8[i]); + inc(i); + end; end; {@@ ---------------------------------------------------------------------------- diff --git a/components/fpspreadsheet/source/common/xlscommon.pas b/components/fpspreadsheet/source/common/xlscommon.pas index d372ab33b..10dfd208b 100644 --- a/components/fpspreadsheet/source/common/xlscommon.pas +++ b/components/fpspreadsheet/source/common/xlscommon.pas @@ -1446,6 +1446,7 @@ begin { Read column start and end index of column range } c1 := WordLEToN(AStream.ReadWord); c2 := WordLEToN(AStream.ReadWord); + if c2 = 256 then dec(c2); // If columns are used Excel writes last index 256 - but it should be 255 ?! { Read col width in 1/256 of the width of "0" character } w := WordLEToN(AStream.ReadWord);