How to copy sheets from a spreadsheet to another?

I have done the below code but the merge cells are not kept after copying.




public static void changeTab(Spreadsheet src, Spreadsheet des)
        {
            if (src != null && des != null)
            {
                List<string> sheetNames = src.Workbook.Worksheets.Select(item => item.Name).ToList();
                foreach (string sheetName in sheetNames)
                {
                    var cloneSheet = src.Workbook.Worksheets[sheetName];
                    //des.Workbook.Worksheets.Remove(sheetName);
                    des.Workbook.Worksheets.AddCopy(cloneSheet,
                        ExcelWorksheetCopyFlags.CopyMerges
                        //| ExcelWorksheetCopyFlags.CopyAll
                        //| ExcelWorksheetCopyFlags.CopyCells
                        //| ExcelWorksheetCopyFlags.CopyColumnHeight
                        //| ExcelWorksheetCopyFlags.CopyAutoFilters
                        //| ExcelWorksheetCopyFlags.CopyOptions
                        //| ExcelWorksheetCopyFlags.CopyConditionlFormats
                        //| ExcelWorksheetCopyFlags.CopyShapes
                        //| ExcelWorksheetCopyFlags.CopyDataValidations
                        //| ExcelWorksheetCopyFlags.CopyPalette
                        );
                    //src.GridCollection.Remove(sheetName, out SpreadsheetGrid value);
                    des.GridCollection[sheetName] = src.GridCollection[sheetName];
                    foreach(var gridRange in des.GridCollection[sheetName].CoveredCells.Ranges)
                    {
                        var excelRange = gridRange.ConvertGridRangeToExcelRange(des.GridCollection[sheetName]);
                        des.Workbook.Worksheets[sheetName].Range[excelRange.ToString()].Merge();
                        des.GridCollection[sheetName].InvalidateCell(gridRange.Right, gridRange.Top);
                    }
                    //des.Workbook.Worksheets[sheetName].Calculate();
                    //des.GridCollection[sheetName].Refresh();
                }
            }
            else
            {
                // todo: handle error
            }
        }



3 Replies

CM Chidanand Murugaiah Syncfusion Team August 30, 2023 01:28 PM UTC

Hi Trung Nguyen Huu,


We are currently analyzing your reported scenario. We need time to validate the scenario, we will provide an update on or before September 01, 2023.


Regards,

Chidanand M.



SG Santhosh Govindasamy Syncfusion Team September 1, 2023 04:18 PM UTC

Hi Trung Nguyen Huu,

I apologize for any inconvenience caused. We were unable to replicate the issue you described in order to provide a code snippet. We are currently analyzing the scenario you reported. We require some time to validate the scenario, and we will provide an update on or before September 05, 2023.
Regards,
Santhosh.G



CM Chidanand Murugaiah Syncfusion Team September 5, 2023 01:26 PM UTC

Hi Trung Nguyen Huu,

 

Based on the information you provided, we assumed that you need to copy one sheet in the spreadsheet, which contains merged cells, and paste it into another sheet. We have prepared a demonstration of your requirement.


Please verify if our demonstration meets your requirements.


Unfortunately, we are currently facing an exception when processing this scenario. Based on your confirmation, we will prioritize this issue and provide a fix and update as soon as possible.

 

Regards,

Chidanand M.

 


Attachment: Spreadsheet_Copy_2e23880c.zip

Loader.
Up arrow icon