Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
880
Setting font on Excel Exporter
posted

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);
}

Parents
No Data
Reply
  • 4960
    posted

    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.)

    // in Form_Load or anywhere before calling Export()
    this.exporter1.BeginExport +=
       new BeginExportEventHandler(
          exporter1_BeginExport);

    // . . .
    void exporter1_BeginExport( object sender,
       BeginExportEventArgs e)
    {
       e.Layout.Overrides.CellAppearance.FontData.Name = "Comic Sans MS";
    }

    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.

     

Children