We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Text length

I am creating an excel sheet based on data in our app. I need to add text, style that text, and then merge with other cells. How do I figure out how many cells that text will cover?

For example, if I added "this is a demo" to a cell in code and open the excel sheet that text would overlap 2 other cells, so I would need to merge 3 cells total.

Johnie




3 Replies

JM Johnson Monohar M Syncfusion Team January 20, 2012 09:44 AM UTC

Hi Johnie,
Thank you for using Syncfusion products.
XlsIO supports to format the texts and merging the cells. You can achieve the cell merging from the following code snippet.
//Creating a range
IRange range = sheet.Range["C10"];

range.Text = "Sample of merging cells";
range.CellStyle.Color = Color.Blue;
range.CellStyle.Font.Color = ExcelKnownColors.Yellow;
range.CellStyle.Font.Bold = true;


//Merging the cells
sheet.UsedRange["C10:E10"].Merge(false);

Also, we have provided a sample that illustrates the text formatting and cells merging in the following link.
Merging_Cells.zip

Please let us know if the provided solution resolved you issue or you need any clarification.
Regards,
Johnson




Merging_Cells_41069c9b.zip


JG Johnie Gilliland January 24, 2012 02:14 PM UTC

I get how to merge cells. What I am looking for is a way to figure out how many cells any given text takes up.



MK Manoj Kumar G M Syncfusion Team January 31, 2012 06:47 AM UTC

Hi Johnie,

Thank you very much for your patience.

You can calculate number of cells that given string takes up by using “MeasureStringSpecial” method in FontImpl class.

Following code snippet illustrates this:

[C#]
FontImpl impl = new FontImpl(range.CellStyle.Font);

SizeF obj = impl.MeasureStringSpecial(range.Text);

double numberOfCells = Math.Ceiling((obj.Width) / value);


Please try the below sample and let us know if this helps you.

Please let us know if you need any clarifications


Regards,
Manojkumar.






Merging_Cells_63b00029.zip

Loader.
Live Chat Icon For mobile
Up arrow icon