Excel document count

Hi Aspose Team,
image.png (353.4 KB)
Is there any way achieve the manual cell count from aspose programmatically.
The current default behavior is displaying the number of cells is merged but i want to display number cells is available in the rows.
FYI Test.zip (9.0 KB)
For example above attached document(test.zip) has 4 merged cells so i need to display the total count as 4.

@manikandan1234,

Thanks for the template file and screenshot.

Although I am not entirely certain about your requirements/issue but the following sample code segments or examples (I have written) may help you to accomplish your task. I used your template Excel file and get all merged cells with their respective merged ranges/areas for your reference. Also, I write an example on how to get total number of initialized cells in the worksheet:
e.g.
Sample code:

            //To get all the merged cells and retreive respective ranges/areas 
            Workbook wb = new Workbook("e:\\test2\\test.xlsx");

            Worksheet ws = wb.Worksheets[0];

            ArrayList lst = ws.Cells.MergedCells;

            for (int i = 0; i < lst.Count; i++)
            {
                CellArea ca = (CellArea)lst[i];

                Console.WriteLine(ca);
            }
            //To get each merged cell and its range/area
            Workbook wb = new Workbook("e:\\test2\\test.xlsx");
            var cells = wb.Worksheets[0].Cells;
            string raddress = null;
            foreach (Cell cell in cells)
            {
                if (cell.IsMerged)
                {
                    var range = cell.GetMergedRange();
                    if (raddress != range.Address)
                    {
                        Console.WriteLine("Merged cell: " + cell.Name);
                        Console.WriteLine("Merged range: " + range.Address);
                    }                    
                    raddress = range.Address;
                }
            }
            //To get count of cells in the worksheet
            Workbook wb = new Workbook("e:\\test2\\test.xlsx");
            var cells = wb.Worksheets[0].Cells;
            Console.WriteLine("Total cells: " + cells.Count);

In case, you still have any confusion or you could not accomplish your task, kindly elaborate your requirements in details and samples, we will help you on how to do it via Aspose.Cells APIs.

Thanks for the reply … Kindly have a look at image.png (41.3 KB)
mergeCount.zip (6.5 KB)

Could you provide sample code for above excel document from aspose code…
I want the count should be 2 as i attached the screenshot.
Kindly share the code and excel document.

@manikandan1234,

Thanks for the sample file and screenshot.

See the following sample code on how to get merge count in the worksheet for your reference:
e.g.
Sample code:

            Workbook wb = new Workbook("e:\\test2\\mergeCount.xlsx");

            Worksheet ws = wb.Worksheets[0];

            ArrayList lst = ws.Cells.MergedCells;

            int mergeCount = lst.Count;
            Console.WriteLine("Merge count: " + mergeCount);

            for (int i = 0; i < lst.Count; i++)
            {
                CellArea ca = (CellArea)lst[i];

                Console.WriteLine(ca);                
            }

Hope, this helps a bit.

The sample code which you send that for read the excel document and calculating the number of merged cells and displaying in the console output… my requirements is as below.

  1. Generate an excel document and then merge 2 cells (as i attached in the screenshot.) image.png (32.6 KB)

  2. Select the merged cells and then check the count in the excel document (not the console) FYR. image.png (26.1 KB)

@manikandan1234,

See the sample code to accomplish your task. The sample code generates your desired Excel file with merged cells accordingly.
e.g.
Sample code:

           //Instantiate a new workbook
           Workbook workbook = new Workbook();

           //Get the first worksheet (default sheet)
           Worksheet worksheet = workbook.Worksheets[0];

           //Insert data into cells (A2, A9)
           worksheet.Cells["A2"].PutValue("Test");
           worksheet.Cells["A9"].PutValue("teatsa");

           //Create style to specify the alignment to center aligned
           Style style = workbook.CreateStyle();
           style.HorizontalAlignment = TextAlignmentType.Center;

           //Apply the style to the cells
           worksheet.Cells["A2"].SetStyle(style);
           worksheet.Cells["A9"].SetStyle(style);

           //Set the column A width a bit.
           worksheet.Cells.SetColumnWidth(0, 48.43);


           //Merge A2:A8--> A2
           worksheet.Cells.Merge(1, 0, 7, 1);

           //Merge A9:A15--> A9
           worksheet.Cells.Merge(8, 0, 7, 1);

           //Save the file
           workbook.Save("e:\\test2\\out1.xlsx");

Please refer to it and write/update your own code accordingly. Also, please find some time to see and browse Aspose.Cells for .NET docs to know how you can use APIs to implement different features or other functionality for your complete reference.

Thanks a lot @Amjad_Sahi… It’s resolved the issue.
image.png (22.0 KB)
image.png (43.8 KB)
FYI… I was using the merge the cells logic as cell by cell. The solution from your code is cells range instead of cell by cell to merge the number of cells.
Thanks for the help.

@manikandan1234,

Good to know that your issue is sorted out by the suggested code segment. Feel free to write us back if you have further queries or comments.