wrap text in range

Nov 8, 2012 at 3:50 PM

I have problem with wrapping text. The first column will merge 2 rows to one cell - "a1:a2". The cell("b1")("c1")("d1")("b2") is just some text. Cell("c2:d2") will merge again and contain big trunk of text. I want the text be wrapped automatically.  I used  ws.Columns().AdjustToContents(); and it doesn't work. I then use ws.Range("c2:d2")).Merge().Alignment.SetWrapText(true); and it doesn't work either. How can I fix this?

 

Thanks.

Coordinator
Nov 8, 2012 at 7:20 PM

How can I reproduce it? The following works fine:

            var wb = new XLWorkbook();
            var ws = wb.AddWorksheet("Sheet1");
            ws.Cell("A1").Value = "This is a really long sentence.";
            ws.Range("A1:B2").Merge().Style.Alignment.SetWrapText();
Nov 8, 2012 at 8:07 PM

How about the follwoing?

var wb = new XLWorkbook();
var ws = wb.AddWorksheet("Sheet1");
ws.Range("A1:A3").Merge().SetValue("1");
ws.Cell("B1").SetValue("b1");
ws.Cell("C1").SetValue("b1");
ws.Cell("D1").SetValue("b1");
ws.Range("B2:D2").Merge().SetValue("This is a really long string and need to be wraped.").Style.Alignment.SetWrapText();
ws.Cell("B3").SetValue("b3");
ws.Cell("C3").SetValue("b3");
ws.Cell("D3").SetValue("b3");

Coordinator
Nov 8, 2012 at 8:14 PM

You get the expected behavior. The contents of the merged range B2:D2 don't have anywhere to wrap to. The row height is not adjusted to accommodate the wrap because it's part of a merged range. Again, this is the expected behavior (you get this if you do it in Excel).

Nov 8, 2012 at 8:22 PM

The height won't adjust even after I use ws.Columns().AdjustToContents(); I would expect this line of code will do the trick. How can I adjust merged cell height with dynamic content length?

Coordinator
Nov 8, 2012 at 8:41 PM

The row heights don't adjust for merged cells. It makes sense if you think about merges that span multiple rows and how you decide how the height should be distributed. Again, this is how Excel works.

Nov 8, 2012 at 8:43 PM

Got it. Thanks a lot for your help.