This project has moved. For the latest updates, please go here.

error when using method cells.copy if row have 2 merge cells

Apr 2, 2012 at 5:32 AM

I'm using method cells.copy to copy row, source row have 2 group mercel, example, I merge cell B20:H20 and I20:J20.

When i using copy method:  worksheet.Cells["A20:K20"].Copy(worksheet.Cells["A30:K30"]); 

in row 30, Cell merged is B30:J30 while must be B30:H30 and I30:J30;

I debug in method copy found, code to check merge cell not correct, i change it to correct loop, (red font)

Sorry because my bad English.

  //Add merged cells
            if(mergedCells.Count>0)
            {
                List<ExcelAddressBase> mergedAddresses = new List<ExcelAddressBase>();
                foreach (var cell in mergedCells.Values)
                {
                    if(!IsAdded(cell, mergedAddresses))
                    {
                        int startRow = cell.Row, startCol = cell.Column, endRow = cell.Row, endCol = cell.Column+1;
                        while (mergedCells.ContainsKey(ExcelCell.GetCellID(Destination.Worksheet.SheetID, endRow, endCol)))
                        {            

                            // fix error when endCol > mergedCells.Count          

                          // endCol exceed mergedCells.Count
                            if (endCol == mergedCells.Count)
                            {
                                break;
                            }            
                            endCol++;
                        }

                        while(IsMerged(mergedCells, Destination.Worksheet, endRow, startCol,endCol-1))
                        {
                            endRow++;
                        }
               
                        mergedAddresses.Add(new ExcelAddressBase(startRow, startCol, endRow-1, endCol-1));
                    }
                }
                Destination.Worksheet.MergedCells.List.AddRange((from r in mergedAddresses select r.Address));
            }

 

Coordinator
Apr 2, 2012 at 6:35 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.