Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

The column width

 Post Reply Post Reply
Author
Christian Hungbaur View Drop Down
New Member
New Member
Avatar

Joined: 11 Nov 2014
Posts: 29
Post Options Post Options   Quote Christian Hungbaur Quote  Post ReplyReply Direct Link To This Post Topic: The column width
    Posted: 01 Aug 2017 at 2:17pm
Our solution is:

1, find the max (length of the title) and calculate the column width after the SetCellValue and AutoFit for that column.

2. Add blank symbol to titel(n), put it to column "n" (SetCellValue), apply AutoFit and compare the width of the column from step 1 with this one. Add more and more blank symbols if the width is not big enough.

The problem is that I cannot find the function that will show the column width.
Back to Top
Christian Hungbaur View Drop Down
New Member
New Member
Avatar

Joined: 11 Nov 2014
Posts: 29
Post Options Post Options   Quote Christian Hungbaur Quote  Post ReplyReply Direct Link To This Post Posted: 01 Aug 2017 at 2:18pm

Sorry. The text was corrupted, Will post it again.

My task is to fill the cells in one column with the text that has 2 parts: a title and a value.

The length of the title is different but the value must be at the same vertical line. To provide this we should use the line

Title1 + "blank symbol" {x1 iteration of the blank symbol} + Value1
Title2 + "blank symbol" {x2 iteration of the blank symbol} + Value2
Title3 + "blank symbol" {x3 iteration of the blank symbol} + Value3
..
Title"n" + "blank symbol" {x"n" iteration of the blank symbol} + Value"n"

Our solution is:

1, find the max (length of the title) and calculate the column width after the SetCellValue and AutoFit for that column.

2. Add blank symbol to titel(n), put it to column "n" (SetCellValue), apply AutoFit and compare the width of the column from step 1 with this one. Add more and more blank symbols if the width is not big enough.

The problem is that I cannot find the function that will show the column width.
Back to Top
Adrian Gallero View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 1170
Post Options Post Options   Quote Adrian Gallero Quote  Post ReplyReply Direct Link To This Post Posted: 01 Aug 2017 at 2:59pm
Hi,
First of all, let me tell you that to get this to work you will have to use a monospaced font. If using proportional fonts, all widths are different and it won't be possible to align them at the right. Say an space is 3 pixels, a "i" 4 pixels and a "M" 5 pixels. You have a line with an i, the other with an M, and you want to align them with spaces. It is not possible, because i + space = 7, while M is 5, and M + space is 8. It is very unlikely that with a proportional font you will get the spaces to neatly align the letters.

For example, with the font in this forum:
1)This is a test    HHH
2)iii   o           YMHHH  <-Not enough
3)iii   o            YMHHH  <-With an extra space, it is too much.

The human eye is sadly very good at noticing this kind of stuff, and you will get a right column that doesn't look right. So you really need to use a monospaced font. (You could use Courier, but there are nicer ones, maybe Consolas?) And if you are using a monospaced font, you can just ensure that all the lines have the same number of characters. So the algorithm could be:

1)count the characters in all the lines, and get the max.
2)Add spaces so all the lines have the same number of characters as the max.

If you want to do it  with proportional fonts, you can use:
ColWidth = xls.GetColWidth(col) /  ExcelMetrics.ColMultDisplay(Workbook);

This will give you the result in "device independent pixels" (1/96 of an inch). If you want them in points (1/72 of an inch), which is the unit FlexCel uses to draw, you have to multiply it by 72.0/92.0.  It depends in the units on which you get the measured text.

You might also need to add some adjustment, because the column widths in Excel aren't always the same: they depend in the zoom level and in the resolution. Everything explained here still applies:

So, you might want to subtract some security margins from the number you got above, so the text looks well in all resolutions and doesn't wrap to the next line.
Back to Top
Christian Hungbaur View Drop Down
New Member
New Member
Avatar

Joined: 11 Nov 2014
Posts: 29
Post Options Post Options   Quote Christian Hungbaur Quote  Post ReplyReply Direct Link To This Post Posted: 01 Aug 2017 at 3:07pm
Thank you for your answer.

xls.GetColWidth(col) exactly what we want to use.

P.S. Now I see that I was use intellisense (visual studio) not properly. The function is exactly where it should be.
Back to Top
Christian Hungbaur View Drop Down
New Member
New Member
Avatar

Joined: 11 Nov 2014
Posts: 29
Post Options Post Options   Quote Christian Hungbaur Quote  Post ReplyReply Direct Link To This Post Posted: 01 Aug 2017 at 4:14pm
Here the part of the code that we use now. 

The width of the text will be calculated based on:

"name" + "blank line" + "i" 

because the function .AutofitCol will ignore the blank symbol at the end of the line.

xls.ActiveSheet = 2;
string value = param.Name;
                    if (param.Name.Length != textLenghtMax)
                    {
                        value = value.PadRight(textLenghtMax);

                        bool isRightPadFinished = false;
                        while (!isRightPadFinished)
                        {
                            value = value + " ";
                            xls.SetCellValue(1, index + 2, value + "i");
                            xls.AutofitCol(index + 2, false, 1);

                            
                            int currentWidth = xls.GetColWidth(index + 2);

                            index++;

                            if (currentWidth + 130 >= maxWidth)
                                isRightPadFinished = true;
                        }

                        param.NameRightPad = value;
                    }
                    index++;

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down