XLS file corrupt after deleting sheets

Questions and answers on how to use XLSReadWriteII 5.
Post Reply
GenasysCarl
Posts: 11
Joined: Mon Jan 09, 2017 6:15 am

XLS file corrupt after deleting sheets

Post by GenasysCarl »

Hi,

After updating to the latest XLSRW6 for some bug fixes, we found a new issue with generated files sometimes being corrupt. I traced it to below code change in the following function.

Code: Select all

procedure TXLSNames_Int.AdjustSheetDelete(const ASheetIndex, ACount: integer);
This is in the latest version and it causes the generated file to be corrupt:

Code: Select all

      case N.SimpleName of
        xsntNone: begin
          FFormulas.AdjustSheetIndex(N.Ptgs,N.PtgsSz,ASheetIndex,-ACount);
          N.LocalSheetId := N.LocalSheetId - ACount;
        end;
What happens is that N.LocalSheetId becomes negative (-2) after deleting a few sheets from the end :oops: . Then the saved file is corrupt.

This is in the previous version I used and it works:

Code: Select all

      case N.SimpleName of
        xsntNone: FFormulas.AdjustSheetIndex(N.Ptgs,N.PtgsSz,ASheetIndex,ACount);
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: XLS file corrupt after deleting sheets

Post by larsa »

Hello

I can't reproduce this. Can you please show a complete example on how to create this.
Lars Arvidsson, Axolot Data
GenasysCarl
Posts: 11
Joined: Mon Jan 09, 2017 6:15 am

Re: XLS file corrupt after deleting sheets

Post by GenasysCarl »

Hi Lars,

When my XLS file loads, it finds a named item with 'xsntNone' type. I am not sure what this is, but it is in the report template created by a user so it is outside my control.

You can get a cleaned copy (28kb) of the XLS here: http://s000.tinyupload.com/index.php?fi ... 6716720063

Code: Select all

procedure TCXLSReports_Test.Test_Problem;
var
  iIndex: Integer;
begin
  FXLS.Filename := 'Book1.xls';
  FXLS.Read;
  FXLS.Delete(FXLS.Count - 1);
  FXLS.Delete(FXLS.Count - 1);
  FXLS.Delete(FXLS.Count - 1);
  FXLS.Write; // File is now corrupt because name item has LocalSheetId < -1

  for iIndex := 0 to FXLS.Names.Count - 1 do
    Check(FXLS.Names[iIndex].LocalSheetId >= -1, 'Sheet ID for named ranges must be -1 or higher.');
end;
Regards,
Carl
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: XLS file corrupt after deleting sheets

Post by larsa »

Hello

This is fixed in update 6.00.36
Lars Arvidsson, Axolot Data
Post Reply