Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Mar 30, 2023 5:37 am

version: 13.3.2.0 free
system: win 10

I setup a excel workbook, and merge a lots cells,

then I use CellRange[] rangeList = sheet.MergedCells; to get RangeList,
and then I use foreach (CellRange srcRange in rangeList) srcRange.UnMerge(); to unMerge the cell

at last, I want to copy the content to fill each cell that used to be merged.

neither I use sheet.Copy(rangeFirst, rangeItem, true);
or I get the RangeAddressLocal and then use sheet.Copy(sheet.Range[fromLocal], sheet.Range[itemAddress], true);

It does not work correct.

when I physicly use sheet.Copy(sheet.Range["A3"], sheet.Range["A4"], true);
It works correct.

I don't know how to attach the sample file or images.

here is the code:

Code: Select all
string file = @"D:\Excel\test.xlsx";

Workbook workbook = new Workbook();
workbook.LoadFromFile(file);

Worksheet sheet = workbook.Worksheets[0];

CellRange[] rangeList = sheet.MergedCells;

foreach (CellRange srcRange in rangeList)
{
    string fromLocal = srcRange.Cells.First().RangeAddressLocal;

    //string toLocal = srcRange.Cells.Last().RangeAddressLocal;

    srcRange.UnMerge();

    CellRange rangeFirst = srcRange.Cells.First();

    foreach (CellRange rangeItem in srcRange)
    {
        string itemAddress = rangeItem.RangeAddressLocal;

        //if (fromLocal != itemAddress)
        //    sheet.Copy(sheet.Range[fromLocal], sheet.Range[itemAddress], true);

        if (rangeItem != rangeFirst)
            sheet.Copy(rangeFirst, rangeItem, true);
    }
}

workbook.SaveToFile(file, ExcelVersion.Version2010);
MessageBox.Show("Done!");

xiaoyaoju
 
Posts: 8
Joined: Sat Jan 23, 2021 7:03 am

Thu Mar 30, 2023 5:38 am

if you have fixed this issue or get the right way to solve this problem, pls let me know, thanks.

my email: jones02@qq.com

xiaoyaoju
 
Posts: 8
Joined: Sat Jan 23, 2021 7:03 am

Thu Mar 30, 2023 5:50 am

here is the excel sample file and run test screenshot. thanks a lot
Attachments
sample.rar
(85.84 KiB) Downloaded 416 times

xiaoyaoju
 
Posts: 8
Joined: Sat Jan 23, 2021 7:03 am

Fri Mar 31, 2023 3:29 am

Hi,

Thanks for your inquiry.
Please see the following code for your reference.
Code: Select all
            foreach (CellRange srcRange in rangeList)
            {

                srcRange.UnMerge();

                CellRange rangeFirst = srcRange.CellList.First();
                string value = rangeFirst.DisplayedText;

                foreach (CellRange rangeItem in srcRange)
                {
                    if (rangeItem != rangeFirst)                 
                        rangeItem.Text= value;
     
                }
            }


If the code does not meet your requirement or you have other questions, just feel free to contact us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Apr 03, 2023 8:52 am

I tried your code, it was running perfect, but I don't know why copy is not OK, only when I use the FIX rangelocaladdress it can be OK.

maybe you can list as an issue to fix by the next version.

Thanks a lot.

xiaoyaoju
 
Posts: 8
Joined: Sat Jan 23, 2021 7:03 am

Tue Apr 04, 2023 6:11 am

Hi,

Thanks for your feedback.
I have logged this issue into our issue tracking system with the ticket number SPIREXLS-4599, our developers will investigate and fix it. Sorry for the inconvenience caused, once there are any updates available, I will inform you asap.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.XLS