Hello!
I am using the latest version of the components (2021.2) and when I am exporting to Excel from an UltraGrid (WinForms) via the UltraGridExcelExporter, the column widths in the Excel file are changed.
My exportformattingoptions are set to none, and I am exporting to an existing excel file with already set column widths that we don't want to change.
Can you advice?
/Henrik
Yes, but how do you mean that I should know which width to put in, if it can be whatever template they are using?
The width shouyld be intact in terms of how the file is formatted in the very beginning, not according to the grid.
Hello Henrik,
When set to none the ExportFormattingOptions property excludes formatting from the grid such as foreground and background colors, text alignment, font data, word wrapping, etc., however what is being exported is still a copy of the grid’s layout.
Since you mentioned that your users decide which columns are being exported, what I can suggest you is looping through the exported columns inside the BeginExport event and assigning the width of the column based on the column key. This could be achieved with the following code:
private void UltraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e) { foreach (UltraGridColumn col in e.Layout.Bands[0].Columns) { if(col.Key.Equals("Address")) e.Layout.Bands[0].Columns[0].Width = 350; } }
Please let me know if you have any questions.
Regards, Ivan Kitanov
I cannot say that I agree on that
There is a property in the UltraWInGrid.ExcelExport object called "ExportFormattingOptions", and before one of your colleagues ( Michael DiFilippo) helped me with finding that one, everything in the Excel file was changed (Forecolor, Background color) when the export was made. But when setting that property to None, ForeColors, BackgroundColors, Fonts etc. were not changed. The width of the columns should be included in that scope if you ask me.
Anyway, if you say that this cannot be done in any other way, it is a bit sad, since we loose all kinds of dynamics in terms of Excel files that can be opened and exported to. We can never now which template the user wants to export to.
Any other ideas?
The reason why the styling of the exported excel file is different is because, when exporting the UltraGridExcelExporter uses a copy of the layout of the grid, meaning that any changes done to this layout would be applied on the exported file, but not on the grid in the application. The copy of the layout is then used for the exporting and overrides the file that you are exporting it to. The easiest solution that I can think of is using the BeginExport event and setting the desired width of the columns there. The code below demonstrates the use of the event and changes the width of the first column in the exported excel file.
private void UltraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e) { e.Layout.Bands[0].Columns[0].Width = 200; }