)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change font styles while importing an excel file to WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: March 19, 2015 |
Last Revised Date: December 20, 2019

Importing excel to grid

By default, the font styles of the Excel Cells are imported from Excel to Grid, as it is. The font styles of the Excel Cells can be changed while importing them to Grid by handling the QueryImportExportCellInfo event. Using this event, the font style can be set, by using the Font.Facename property and other styles can also be changed when e.Action is set as GridConverterAction.Import.

C#

private void importBtn_Click(object sender, EventArgs e)
{
   //Initializing the Excel engine.
   ExcelEngine engine = new ExcelEngine();
   //Opening the Excel file.
   IWorkbook book = engine.Excel.Workbooks.Open(@"..\..\Book1.xlsx");
   IWorksheet sheet = book.Worksheets[0];
   Syncfusion.GridExcelConverter.GridExcelConverterControl gecc = new Syncfusion.GridExcelConverter.GridExcelConverterControl();
   //Triggering the QueryImportExportCellInfo event.
   gecc.QueryImportExportCellInfo += new Syncfusion.GridExcelConverter.GridImportExportCellInfoEventHandler(gecc_QueryImportExportCellInfo);
   //Importing the grid.
   gecc.ExcelToGrid(sheet, this.gridControl1.Model);
}
//Handling the QueryImportExportCellInfo event.
void gecc_QueryImportExportCellInfo(object sender, Syncfusion.GridExcelConverter.GridImportExportCellInfoEventArgs e)
{
   //Changing the font style while Importing.
   if (e.Action == Syncfusion.GridExcelConverter.GridConverterAction.Import)
   {
      //Changing Grid cell's font styles while importing.
      e.GridCell.Font.Facename = "Verdana";
      e.GridCell.CellValue = "Font Style in Verdana";
      e.GridCell.Font.Size = 7.0f;
      e.Handled = true;
   }
}

VB

Private Sub importBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles importBtn.Click
   'Initializing the Excel engine.
   Dim engine As New ExcelEngine()
   'Opening the Excel file.
   Dim book As IWorkbook = engine.Excel.Workbooks.Open("..\..\Book1.xlsx")
   Dim sheet As IWorksheet = book.Worksheets(0)
   Dim gecc As New Syncfusion.GridExcelConverter.GridExcelConverterControl()
   'Triggering the QueryImportExportCellInfo event.
   AddHandler gecc.QueryImportExportCellInfo, AddressOf gecc_QueryImportExportCellInfo
   'Importing the grid.
   gecc.ExcelToGrid(sheet, Me.gridControl1.Model)
End Sub
'Handling the QueryImportExportCellInfo event.
Private Sub gecc_QueryImportExportCellInfo(ByVal sender As Object, ByVal e As Syncfusion.GridExcelConverter.GridImportExportCellInfoEventArgs)
   ‘Changing the font style while Importing.
   If (e.Action = Syncfusion.GridExcelConverter.GridConverterAction.Import) Then
     'Changing Grid cell's font styles while importing.
     e.GridCell.Font.Facename = "Verdana"
     e.GridCell.CellValue = "Font Style in Verdana"
     e.GridCell.Font.Size = 7!
     e.Handled = True 
   End If
End Sub 

Screenshots:

Font type in excel

Figure 1: Font type in Excel

 

Font type in grid after importing from excel sheet

Figure 2: Font type in Grid after importing the above Excel sheet

Samples:

C#: Changing_Font_Style_while_importing_CS

VB: Changing_Font_Style_while_importing_VB

Reference link: https://help.syncfusion.com/windowsforms/grid-control/importing

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon