DeleteRange on table results in corrupted xls file

Hi,

Thanks for reporting this. Tables can be a little tricky in what you are allowed to do or not, and sometimes FlexCel might let you do stuff that Excel won't, and Excel doesn't like this.


I've been trying to reproduce it here with a table in an Excel file from A1 to B3:


xls.DeleteRange(txlscellrange.Create(2, 1, 2, 5), TFlxinsertMode.ShiftRowDown);

Works ok. (deletes row 2)

 As does:

xls.DeleteRange(txlscellrange.Create(3, 1, 3, 5), TFlxinsertMode.ShiftRowDown);

(deletes row 3)

and

xls.DeleteRange(txlscellrange.Create(1, 1, 3, 5), TFlxinsertMode.ShiftRowDown);

(deletes the full table)

If you try:

xls.DeleteRange(txlscellrange.Create(1, 1, 2, 5), TFlxinsertMode.ShiftRowDown);

There is an Exception, and that is ok since Excel won't let you either delete a row which contains the headers of a table.

Finally, I think I was able to reproduce your issue with:

xls.DeleteRange(txlscellrange.Create(2, 3, 2, 5), TFlxinsertMode.ShiftRowDown);

That is, deleting all the rows inside the table. In this case Excel will complain that the file is invalid when you open it, and if you recover it, it will remove the table.

So my first question would be to confirm this is indeed your problem. Are you trying to delete all rows but not the header of a table? If you think that it might be a different problem, please send me a project/file to adrian@tmssoftware.com so I can check it out.

About this particular problem, this should indeed be fixed, but I am not sure how yet; I'll have to think a little more about it. 
The thing is, normally what we should do in this case is throw an exception telling you you can't have a table with 0 rows. That's what makes more sense, and how Excel behaves in most cases where you want to do something that is not allowed. 
But tables in Excel behave a little weird: If you try to delete all rows in a table from Excel, Excel won't show an error, but just not delete all rows. It will keep the first row and clear it, even when you were supposed to delete it.

So I'll have to review it more. I think it makes more sense to just tell you there is a problem via an Exception, but someone might like the "excel way" of just ignoring what you said and not deleting all the rows so the table doesn't become invalid.

In any case, if this is the issue you are having and not a different one, just be aware that you just can't delete all rows in a table, so check the code to not try to do it. As said above our fix is likely to be an Exception anyway in this case, so it will help if you don't try to do it.