This project has moved and is read-only. For the latest updates, please go here.

Column.AdjustToContents() multiline support

Jan 19, 2012 at 7:33 PM

Any possibility of extending multiline support in Row.AdjustToContents() to Column.AdjustToContents()?  If I have more than a single line of text (newline separated, not wrapped) in a cell, Column.AdjustToContents() includes all lines of text in the calculation?  I'd like to see it calculate each line of text separately (based on the newline character) and adjust accordingly.  Is that possible?

Jan 22, 2012 at 12:11 AM

It already does that. In the following example columns A and B adjust to their second line.

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Sheet1");

                .AddText("Hello World").AddNewLine()
                .AddText("This is just a text, nothing more.").AddNewLine()

            ws.Cell("b1").Value =
                "Hello World" + Environment.NewLine +
                "This is just a text, nothing more." + Environment.NewLine +

Sep 17, 2012 at 4:14 PM

But is it possible to use AdjustToContents for text without new lines, but with word wrap enabled on cell?

Sep 17, 2012 at 4:32 PM

What you're asking doesn't make sense.

Adjust to contents = Make the column wide enough for the longest text.

Word wrap = Make the row tall enough to show the contents of the cell at the given column width.

Sep 17, 2012 at 6:06 PM

Hm, strange.

First, for me, word wrap != row height. I mean its when words moved to another line if there is no more space on current line (yeah, given column width). So i was thinking if you set word wrap - then text will not continue beyond edges of column, but if correct height is not set, then part of text (moved to other lines) will not be visible (beyond bottom border of cell).

Second, i was trying closed xml few month ago, and even with word wrap - row height (1 line of text) was same for all rows disregard of amount of lines.

But today i tried last build, and now rows height correctly calculates for multiline columns (with word wrap). Something was changed/fixed in last few months?=) Or it was some problem in my code? As i understand its something related to Format->AutoFit feature in excel? Its not calculated in code, but calculated by excel on document opening? No?

If it was just problem on my side  - then sorry, ignore my comments)



You have nice lib =)

Sep 17, 2012 at 6:13 PM

Row height, column width, word wrap, and autofit (adjust to contents) are all interconnected.

For example, setting word wrap but not the row height will cause the row height to be as high as it needs to display the text (at the given column width). If you set the row height then word wrap won't affect the row height (it's fixed).

Play with it in Excel and you'll see the pattern more clearly. ClosedXML behaves just like setting things up in Excel. If it doesn't then please let me know.