How to determine if a phrase would fit into a cell?

I tried to determine whether a phrase would fit into a cell or not. I thought comparing the values from GetWidthOfValue and GetColumnWidthPixel. But it’s not reliable. Is there any better way?

Thank you.

@sanshi,

Aspose.Cells provides GetWidthOfValue and GetColumnWidthPixel methods to determine the width of the text/value inside a cell and if you need to auto-fit column operation or not. We do not have other better options to cope with it.

Could you share a sample code and template file where it gives wrong results. We will check it soon.

Xlsx1.zip (111.4 KB)
See the attached project. For both rows, GetWidthOfValue are greater than GetColumnWidthPixel. But the first row text fits fine.

Thank you.

@sanshi,

I checked your Excel file and code and results are ok. The reason is A1 cell is merged for cells (range) --> A1:C1. Similarly A2 cell is merged —> A2:C2.

Please note, for first row (merged) cell, you got to check and calculate the width of A + B + C columns and add them by yourselves. Now you get the total widths of A:C columns and then evaluate. Same should be the case with second row.

In short, this is expected behavior and result.

I see. Thank you very much for the answers.

@sanshi,

You are welcome.