Page 1 of 1

Read data from merged cells

Posted: Sun Jun 24, 2007 3:02 pm
by Tougias
Does XLSReadWriteII handles the case of reading merged cells?
Because now it seems that when reading an excel file which contains merged cells, it associates the read data on the first cell, and the rest of the merged cells are considered to be empty.

For example here is a small code where I read the contents of an excel file and populate a Grid component with the cells value:

Code: Select all

for row:=0 to MAX_ROWS do
    begin
        Grid.AddRow();
        for col:=0 to MAX_COLUMNS do
        begin

          data_str:=XLS.Sheet[SheetIndex].AsString[col,row];
          Grid.Cell[col,row].AsString:=data_str;
        end;
    end;
The result is that each cell of the grid component gets the corresponding value from the excel file. BUT the merged cells of the excel file are not imported as merged and therefore the value of the merged cells only goes to the first one of the merged cells. The remain cells are just kept blank.
Any ideas how to solve this?
[/img]

Posted: Wed Jun 27, 2007 10:00 am
by larsa
Hello

This is how it shall work. The data of merged cells is stored in the upper left cell.


Lars Arvidsson

Posted: Wed Jul 04, 2007 10:46 am
by Tougias
larsa wrote:Hello

This is how it shall work. The data of merged cells is stored in the upper left cell.


Lars Arvidsson
OK I managed to solve this by using MergedCells field.

What I needed is that when importing a merged cell containing any value, this value should go to all the cells that were merged.

e.g. if cells A5..A10 are merged and A5 contains the value 1, then when importing this excel file into Delphi using TXLSReadWriteII2 component, I wanted each cell from A5 to A10 to have the value 1.

So in the code I find the number of merged cells by checking XLS.Sheet[SheetIndex].MergedCells.CellInAreas(col,row);
which gives me the number of merged cells.
If it is 0 then the cell is not merged.
If it is greater than 0 (e.g. it's 5) it means that 5 cells were merged into this one.

All the best,
Vangelis Tougias