Articles in this section
Category / Section

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

2 mins read

Importing excel to grid

By default, the font styles of the Excel Cells are imported from Excel to WinForms 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


Conclusion

I hope you enjoyed learning on how to change font styles while importing an excel to WinForms GridControl.

You can refer to our WinForms GridControl featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our our WPF GridControl example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied