AutoFit column while row having specific height

Hello,

for all my rows, I have defined a specific height for the rows, let’s say 100.
I add values in the cells, for simplicity let’s say all in the first column.
Now I use the autofit on the first column.
the text is displayed on only one line so it does not use the whole space available since I defined a row height.

is there a way to make the autofit of columns take into consideration the value defined for the row height?

if we do it manually on excel, it will of course use all the space available created by the use of a “big” row height.

Thank you.

@Kawthar

Thanks for using Aspose.Cells.

Please wrap the text using Style.IsTextWrapped property. Please see the following sample code, its output Excel file and screenshot for a reference.

If you still have any question, then please provide us your sample code and sample Excel file(s). We will look into your issue further and help you asap.

Download Link:
Output Excel File.zip (5.6 KB)

C#

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets[0];

ws.Cells.SetRowHeight(3, 100);

ws.Cells["C4"].PutValue("Text");

Cell c = ws.Cells["B4"];
c.PutValue("This is long text. This is long text. This is long Ending.");

//Wrap the text
Style s = c.GetStyle();
s.IsTextWrapped = true;
c.SetStyle(s);

wb.Save("output.xlsx");

Screenshot:

Hi,

thank you for the answer, however you did not use the autofit, and your example works fine because you used a relatively big height comparing to the text.
Here is an example using kind of the same code as yours :

Dim wb As Workbook = New Workbook()

Dim ws As Worksheet = wb.Worksheets(0)

ws.Cells.SetRowHeight(3, 100)

ws.Cells("C4").PutValue("Text")

Dim c As Cell = ws.Cells("B4")
c.PutValue("This is long text. This is long text. This is long Ending. Actually very very long text for which we should use AutoFit")

dim s As Style = c.GetStyle()
s.IsTextWrapped = True
c.SetStyle(s)

wb.Save("TestWithoutAutoFit.xlsx")

output :
withoutAutoFit.jpg (144.4 KB)

now if we add the autofit, i.e. having this code :

Dim wb As Workbook = New Workbook()

Dim ws As Worksheet = wb.Worksheets(0)

ws.Cells.SetRowHeight(3, 100)

ws.Cells("C4").PutValue("Text")

Dim c As Cell = ws.Cells("B4")
c.PutValue("This is long text. This is long text. This is long Ending. Actually very very long text for which we should use AutoFit")

dim s As Style = c.GetStyle()
s.IsTextWrapped = True
c.SetStyle(s)

ws.AutoFotColumn(1)

wb.Save("TestWithAutoFit.xlsx")

then you would get this :

withAutoFit.jpg (133.1 KB)

however, the expected behaviour would be :
expectedBehaviour.jpg (131.3 KB)

and you can get that by doing in excel the following:

  1. define a height for the row , example 100
  2. write the text and define as “Wrap Text”
  3. Auto Fit the column

thank you for your help

P.S. I could not upload the excel files because the extension is not allowed.

@Kawthar

Please autofit your column with respect to some other cell. For example, the following code auto fits the column B with respect to cell B2. Please see the output Excel file generated by the code and screenshot for your reference.

Download Link:
Output Excel File.zip (5.7 KB)

VB. NET

Dim wb As Workbook = New Workbook
Dim ws As Worksheet = wb.Worksheets(0)

ws.Cells.SetRowHeight(3, 100)

ws.Cells("C4").PutValue("Text")

Dim c As Cell = ws.Cells("B4")
c.PutValue("This is long text. This is long text. This is long Ending. Actually very very long text for which we should use AutoFit")

Dim s As Style = c.GetStyle
s.IsTextWrapped = true
c.SetStyle(s)

'Code Starts from Here
Dim b2 As Cell = ws.Cells("B2")
b2.PutValue("This is sample text. Another one.")

ws.AutoFitColumn(b2.Column, b2.Row, b2.Row)
'Code Ends

wb.Save("TestWithAutoFit.xlsx")

C#

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
ws.Cells.SetRowHeight(3, 100);
ws.Cells["C4"].PutValue("Text");
Cell c = ws.Cells["B4"];
c.PutValue("This is long text. This is long text. This is long Ending. Actually very very long text for which we should use AutoFit");
Style s = c.GetStyle();
s.IsTextWrapped = true;
c.SetStyle(s);

//Code Starts from Here
Cell b2 = ws.Cells["B2"];
b2.PutValue("This is sample text. Another one.");

ws.AutoFitColumn(b2.Column, b2.Row, b2.Row);
//Code Ends

wb.Save("TestWithAutoFit.xlsx");

Screenshot:

Hi,

thank you but this workaround is not fine for me.
replace the line where you are inputting a value in the B2 cell by this :

b2.PutValue("This is sample text. Another one. If I add a very long text then your example won't work :) so we need a better solution. FYI the functionnality works perfectly using spredSheetGear")

you will have an output like attachedstillNotWorking.jpg (145.9 KB)

thank you,

@Kawthar

Thanks for using Aspose APIs.

We were able to observe the issue mentioned by you and logged it in our database for investigation and for a fix. Once, the issue is resolved or we have some other news for you, we will let you know asap.

This issue has been logged as

  • CELLSNET-45970 - When autofit column Aspose.Cells do not consider the row height when the cell is text wrap

Screenshot Showing the Problem:

sc.png (72.9 KB)

@Kawthar

Thanks for using Aspose APIs.

This is to inform you that we have fixed your issue CELLSNET-45970 now. We will soon provide the fix after performing QA and including other enhancements and fixes.

@Kawthar

Thanks for using Aspose APIs.

Please download and try the following fix for your issue CELLSNET-45970 and let us know your feedback.

The issues you have found earlier (filed as CELLSNET-45970) have been fixed in Aspose.Cells for .NET 18.4. Please also check the document/article for your reference: Install Aspose Cells through NuGet|Documentation