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.
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.
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");
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")
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")
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.
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");
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")
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
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.