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 Nov 23, 2023 2:52 am

Hi..

If `A1:A4` is merged , the DisplayText of `A3` is empty value appears.
Is there a way to get the DisplayText of merged cells?
I want to get the DisplayText when accessing a cell other than the first merged cell.

I guess I have to use `HasMerged` and `MergeArea` to get it, but is there a simpler way?

Sungkyu_min
 
Posts: 35
Joined: Fri Jan 07, 2022 5:53 am

Thu Nov 23, 2023 5:40 am

Hello,

Thank you for your inquiry.
Regarding your question about merged cells, we would like to inform you that when merging cells, only the first cell contains a value, while the remaining cells are considered empty by default. If you need to retrieve the values from the merged cells, excluding the first cell, you can utilize the following code snippet:
Code: Select all
Workbook workbook = new Workbook();
workbook.LoadFromFile("input.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
//Gets a collection of merged cells in a worksheet
CellRange[] ranges= worksheet.MergedCells;
//Traverse the merge cell collection
foreach (CellRange range in ranges)
{
    //Gets the display text for the merged cell
    string displayText = range.DisplayedText;
    //Gets the region of the merged cells
    CellRange cellRange = range.MergeArea;
    //Gets the specified cell
    CellRange outRange = worksheet["A3"];
    //Traverse all cells of the merged cell
    for (int i = 0; i < cellRange.CellsCount;i++)
    {
        CellRange ranges1 = cellRange.CellList[i];
        //Determines whether the specified cell is in the merge cell range
        if (outRange.RangeAddress.Equals(ranges1.RangeAddress))
        {
            //Assigns the obtained display text to the text in the specified cell
            outRange.Text = displayText;
            Console.WriteLine(outRange.DisplayedText);
        }       
    }
}

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.XLS