How to autosize the columns after creating a worksheet?
If you are using version 12.1 or later, you could use the new WorksheetCell.GetText() method to get the actual display text that will be shown. Also, measuring the text using the cell's resolved font name and size (from WorksheetCell.GetResolvedCellFormat) and then adding some padding will probably give a better width measurement, but will be slower.
Hello all,
Could you please try out the following code and see if it meets your requirements:
private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e) { e.Layout.PerformAutoResizeColumns(true, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand, Infragistics.Win.UltraWinGrid.AutoResizeColumnWidthOptions.All);
}
Wrote this method to autosize the columns. Someone else can probably write it a bit more elegantly than I did, but it works great.
/// <summary> /// Autosize the columns using their text as a measuring guide /// </summary> /// <param name="book">Excel book</param> public static void AutoSizeColumns(Workbook book) { Dictionary<int, int> colWidths = new Dictionary<int, int>();
foreach (var wksheet in book.Worksheets) { foreach (var row in wksheet.Rows) { foreach (var cell in row.Cells) { object value = cell.Value; if (!colWidths.ContainsKey(cell.ColumnIndex)) colWidths.Add(cell.ColumnIndex, value.ToString().Length); else if (value.ToString().Length > colWidths[cell.ColumnIndex]) colWidths[cell.ColumnIndex] = value.ToString().Length; } }
foreach (var colIndex in colWidths.Keys) { wksheet.Columns[colIndex].Width = colWidths[colIndex] * 256; } } }
No, this has not yet been implemented. You should also submit a feature request because the number of requests is considered when determining what should be implemented in each release.
Hi, is there any news about this feature?
Has it been included in recent releases?