I found an example online from one of your articles here.http://help.infragistics.com/Help/NetAdvantage/NET/2008.2/CLR2.0/html/Infragistics2.Excel.v8.2~Infragistics.Excel.Workbook~CreateNewWorksheetCellFormat.html
I tried the same method to set the font from Segoe UI to Verdana, but it is not working. Does anyone know if this is a known issue or if I am missing something?
UltraGridExcelExporter exporter = new UltraGridExcelExporter();Workbook workbook = new Workbook();Worksheet worksheet;
IWorksheetCellFormat styleFormat = workbook.CreateNewWorksheetCellFormat();styleFormat.Font.Name = "Verdana";workbook.Styles.AddUserDefinedStyle(styleFormat, "Custom Cell Style");
foreach (UltraGrid grid in grids){ worksheet = workbook.Worksheets.Add(grid.Tag.ToString()); exporter.Export(grid, worksheet);}
Hi Anomoly,
I think WorkbookStyles aren't the way to go about this. WorkbookStyles allow you to define cell formats that the user can apply from within Excel when they open your workbook, so you can define a set of application-dependent styles like "Needs Attention" and "Field Label". Conversely, when you Load a workbook the WorkbookStylesCollection let you pick-up custom styles that may have been defined in it, and then you can use them programmatically yourself by plucking them off of WorkbookStyle's StyleFormat and applying that IWorksheetCellFormat with a call to SetFormatting upon target cell(s), etc., that you want formatted that way. Otherwise, the user-defined style carries no directions on how the Workbook should apply that style automatically.
Where does this leave you? Well, you can probably handle this in the BeginExport event. You are passed an export layout, which is basically a copy of the grid's layout. Through this export layout, you can override certain Appearances and have it used only during the export process. The following one-liner will apply Comic Sans MS font to all cells (excl. column headers, etc., although you can change their Appearance too if you need to.)
The other caveat is the above works as long as you are not overriding CellAppearance closer to the cell in the grid, in that case you may just have to dig deeper into the export layout to reach the same level as you are overriding it in your application.
That did not work either. Still Segoe UI in every cell. I do not see much room here for human error.
public static void ExportUltraGrids(bool exportSelectedRows, string filePath, UltraGrid[] grids, bool openfile)
{
UltraGridExcelExporter exporter = new UltraGridExcelExporter();
Workbook workbook = new Workbook();
Worksheet worksheet;
if (exportSelectedRows)
exporter.RowExporting += new RowExportingEventHandler(exporter_RowExporting);
exporter.BeginExport += new BeginExportEventHandler(exporter_BeginExport);
foreach (UltraGrid grid in grids)
worksheet = workbook.Worksheets.Add(grid.Tag.ToString());
exporter.Export(grid, worksheet);
}
if (workbook.Worksheets.Count > 0)
workbook.Save(filePath);
if (openfile)
Process.Start(filePath);
private static void exporter_BeginExport(object sender, BeginExportEventArgs e)
e.Layout.Override.CellAppearance.FontData.Name = "Verdana";
Something else must be overriding the font setting on the grid cells, then. Just as a test, trying putting the same line of code into the grid's IniitializeLayout event and see if it has any effect on the on-screen grid. If I am correct, it will have no effect, because some other appearance setting in your application is overriding the Layout.Override.CellAppearance.
Perhaps you are loading an application style library in to your app? Or perhaps you are handling InitializeRow on the grid and applying a font to the cell appearance there?
In any case, as I said, I am pretty sure that what you have here will not really be any more efficient than if you set the format on each individual cell inside the CellExported event.
You are right, adding that line to the InitializeLayout event did nothing in grid view.
I am using "UseAppStyling" on the grid, but I can not imagine why that would effect the exporter. I can not turn this off of my grid style would not match the rest of our application.
If you are loading an application style into the grid, then it will certainly affect the exported grid. The exporting is WYSIWYG - it tries to make the export look just like the grid on the screen.
So it looks like the only way to get what you want here is to use the CellExported event.