Before I save the worksheet, I would like to autofit the columns to the largest data item in the cell. How can I do this?
all i found a work around. May not be the best but defnitely works out. please see below function. row count may be incresed beyond 1000. my application has maximum of 1000 rows written in excel.
Public Sub AutoAdjustColumnWidth(ByVal Column As String, ByVal sheet As Worksheet)
Try
If (Not (String.IsNullOrEmpty(Column))) Then
Dim WidthInPixes As Double = 0
Dim ColumnLetter As String = Column
For Each cell As WorksheetCell In sheet.GetRegion(ColumnLetter + 1.ToString() + ":" + ColumnLetter + (1000).ToString())
If (Not (String.IsNullOrEmpty(cell.Value))) Then
Dim cellfontName As String = IIf(cell.CellFormat.Font.Name Is Nothing, "Arial", cell.CellFormat.Font.Name)
'Arial is default font.
Dim CellFontSize As Single = IIf(cell.CellFormat.Font.Height = -1, 10, (cell.CellFormat.Font.Height / 20))
'10 is default height
Dim fontstyle As FontStyle = IIf(cell.CellFormat.Font.Bold = ExcelDefaultableBoolean.True, fontstyle.Bold, fontstyle.Regular)
Dim font As Font = New Font(cellfontName, CellFontSize, fontstyle)
Dim size As Size = New Size(Integer.MaxValue, Integer.MaxValue)
size = System.Windows.Forms.TextRenderer.MeasureText(cell.Value.ToString(), font, size)
If size.Width > WidthInPixes Then
WidthInPixes = size.Width
End If
Next
If (Not (WidthInPixes = 0)) Then
sheet.Columns(Helper.GetColumnNumber(Column, True)).Width = (sheet.Workbook.PixelsToCharacterWidth256ths(WidthInPixes))
Catch ex As Exception
SetError(ex, "AutoAdjustColumnWidth")
End Try
End Sub
Hello Srikanth,
This is still in our queue. You will be notified through email if it is decided that this request will be implemented as a feature in the future service releases.
Please do not hesitate to contact us if you need any additional assistance.
Hi Boris, we are planning to migrate to infragistics excel from our existing implementation of excel using COM interop. could you please give an update on the anticipated release verions/date of this feature in infragitics excel.?
Thanks.
Srikanth
Hello foxwiz,
I have sent your feature request directly to our product management team. Our product team chooses new feature requests for development based on popular feedback from our customer base. Infragistics continues to monitor application development for all of our products, so as trends appear in requested features, we can plan accordingly.
If your feature is chosen for development, you will be notified at that time. Your reference number for this feature request is FR13516 .
If you would like to follow up on your feature request at a later point, you may contact Developer Support management via email. Please include the reference number of your feature request in the subject and body of your email message. You can reach Developer Support management through the following email address: dsmanager@infragistics.com
Thank you for your request.
You are attempting to set column width to 3000 from your code. But what you want is to autofit the column. Can someone from Infragistics take a look at it. Does Infragistics Excel Component support this feature?