Using ClosedXML how to adjust row height to content? - c#

I create cell with text. After that I set WrapText property and column width.
var cell = worksheet.Cell("A1");
cell.Style.Alignment.WrapText = true;
cell.SetValue("This is very long text");
worksheet.Column(1).Width = 10;
worksheet.Rows().AdjustToContents();
The text has been moved by words, but row height is not changed. How to adjust row height to cell content?

There are many ways to achieve this.
Don't use wrap or shrink properties on cell values rather include this line just before saving your excel
ws.Columns().AdjustToContents();
Another way is to make use of Allignment property
IXLRange titleRange = ws.Range("B2:AA2");
titleRange.Cells().Style
.Alignment.SetWrapText(true); // Its single statement
Hope it helps!!

It works when you remove the worksheet.Rows().AdjustToContents();.
Autofitting sometimes needs more of a trial and error approach ...

The following code worked for me.
IXLRange contents = ws.Range("A1:A50");
contents.Style.Alignment.WrapText = true;

You can also AdjustToContents on Specific range of cells.
worksheet.Columns(2, 20).AdjustToContents();

I think this is a bug of ClosedXML.
enter link description here
ws.Row(1).AdjustToContents();
ws.Row(1).ClearHeight();
In my case it works.
Version: 0.95.4

Related

C# - DataGridView with Wrap text in Cell but without spaces

I have used the DataGridView with the line break but for some reason something is wrong.
This is the code:
this.übersetzerDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
this.übersetzerDataGridView.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;
Here is the result:
What am I doing wrong?
[SOLVED] After much searching, I finally found the solution.
By using Fill value in the column in question.
The column width adjusts so that the widths of all columns exactly fills the display area of the control.
this.übersetzerDataGridView.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
this.übersetzerDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
this.übersetzerDataGridView.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
Here is the result:
Thanks a lot.
Since #Michael posted his code in German and it might make some people confused (just like me) this did the work for me -
Add the following code to the RowValidated event in your DataGridView
Private Sub DGV_RowValidated(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_I.RowValidated
DGV_I.DefaultCellStyle.WrapMode = DataGridViewTriState.True
DGV_I.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
DGV_I.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub
The result -
Enable wrap mode for the column in question:
dgv.Columns["dtComment"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
Set AutosizeRowsMode to AllCells

Excel how to fit text in cell, based on cell witdh

I want to set the witdh of a cell in my ExcelSheet.
Set the witdh:
worksheet.Columns["K"].ColumnWidth = 114.80;
When the text is larger then the ColumnWith the text is not visible.
I want to split the text to a new row in the same cell based on the ColumnWith.
I tried to add \r\n to the string in the Excel but no result.
EDIT after answers
This works perfectly:
worksheet.Columns["K"].ColumnWidth = 114;
Excel.Range rangeK = worksheet.get_Range("K1");
rangeK.EntireColumn.WrapText = true;
What you are looking for is this:
worksheet.Range("K1:K100").WrapText = True;
That code, for example, will set the cells from K1 to K100 to wrap the contents inside their cells.
You have a couple options. The first option (which is what I would suggest) is to autoresize the columns.
worksheet.Columns.AutoFit();
The next option is to word wrap all text, which I have not done but this link might be of use to you. I hope this helps.

How do I highlight an active row in a Infragistics grid?

I have a row that I activated via code. However, I need it to be highlighted as well, but can't seem to find the correct property to set. How can I do this via C#?
You can change the appearance of the active row through the ActiveRowAppearance
I think the following solution should work for you:
ultraGrid1.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.IndianRed;
ultraGrid1.Rows[0].Activate(); //programatically set active row
ultraGrid1.Selected.Rows.Clear(); //clear your selection of previous row

Excel Cell Height Does Not Increase

I'm using Microsoft.Office.Interop.Excel to write data into excel cell. The cell has a certain initial height and I'm setting both "merge cell = true" and "wrap text = true".
When the cell content is large, the cell height is not increased. I tried both of the following:
_range.entrirerow.autofit()
_range.columns.autofit()
This doesn't work anything for me, please suggest some solution.
Try _range.Rows.Autofit();
Please note:
COM objects don't get released with double periods. It created tempory variables, which you can't access.
So rather use:
Range rows = _range.Rows;
rows.Autfit();
if (rows != null)
{
Marshal.FinalReleaseComObject(rows);
}

try to hide last column fail with error "Cannot shift objects off sheet"

So for some purpose, I need to insert a comment in the last column of excel.
Since I don't want user to be able to see the comment I inserted, I try to hide the column.
Somehow it gave me an error "Cannot shift objects off sheet"
Constants.cs
HIDDEN_DATA_COL = 16384; // Which is last column in excel 2007
MyUtil.cs
Excel.Range range = ws.get_Range(ws.Cells[1, Constants.HIDDEN_DATA_COL], ws.Cells[1, Constants.HIDDEN_DATA_COL]);
range.AddComment(result);
range.Comment.Shape.Width = 50;
range.Comment.Shape.Height = 50;
range.EntireColumn.Hidden = true;
I also make some trial :
remove range.EntireColumn.Hidden and the code is OK.
changing the HIDDEN_DATA_COL to number less than 10 its also make the runtime error dissapear.
Any idea on how to fix it?
"Hiding" comments in the last column of the sheet is probably not the best way to solve your original issue.
You faced the issue of the Cannot shift objects off sheet error within your code but even if you managed to do it, your user would face it whenever he or she would try to hide or insert any column.
Here is how Microsoft advice to handle this: http://support.microsoft.com/kb/211769
As this might be a case of an XY issue, I will suggest some workarounds.
Actually, in your case, you'd probably hide your comment somewhere else, in any hidden cell for instance and if you want the user not to see it, just protect your sheet from unhiding cells.
If you don't want to protect, you can store your comments in another sheet and hide it with VBA using the xlVeryHidden property (user cannot unhide the sheet without VBA).
Fixed by selecting the comment column first.
range.AddComment(result);
range.Comment.Shape.Width = 50;
range.Comment.Shape.Height = 50;
range.Columns.Cells.Select();
range.Columns.EntireColumn.Select();
range.Columns.EntireColumn.Hidden = true;
The issue is not just to hide a column but also to reduce the width of a column, the same error gets presented. To fix= Take the comment information, post/paste it in a different cell to save. Then delete the comment, adjust the column and create the comment again for the cell you had it in before. Do this for each comment on the desired sheet.
Hope this helps, Enjoy. BigEEE :)

Categories

Resources