Category / Section
How to hide certain columns and lock or unlock columns while exporting WinForms GridGroupingControl to Excel?
1 min read
Exporting
In order to hide some specific columns and also to lock or unlock columns while exporting from GridGroupingControl to Excel, the ShowColumn() method and the Locked property of worksheet can be used respectively.
C#
ExcelEngine engine = new ExcelEngine(); IApplication app = engine.Excel.Application; app.DefaultVersion = ExcelVersion.Excel2010; IWorkbook book = app.Workbooks.Create(); IWorksheet sheet = book.Worksheets[0]; excelConverter.GroupingGridToExcel(this.gridGroupingControl1, sheet, ConverterOptions.Default); // Hides the Column index "2". sheet.ShowColumn(2, false); // Locks the cell range B12 sheet.Range["B12"].CellStyle.Locked = true; // Unlocks the cell range A2 sheet.Range["A2"].CellStyle.Locked = false; book.SaveAs(this.saveFileDialog1.FileName); System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = this.saveFileDialog1.FileName; proc.Start();
VB
Dim engine As New ExcelEngine() Dim app As IApplication = engine.Excel.Application app.DefaultVersion = ExcelVersion.Excel2010 Dim book As IWorkbook = app.Workbooks.Create() Dim sheet As IWorksheet = book.Worksheets(0) excelConverter.GroupingGridToExcel(Me.gridGroupingControl1, sheet, ConverterOptions.Default) ' Hides the Column index "2". sheet.ShowColumn(2, False) ' Locks the cell range B12 sheet.Range("B12").CellStyle.Locked = True ' Unlocks the cell range A2 sheet.Range("A2").CellStyle.Locked = False book.SaveAs(Me.saveFileDialog1.FileName) Dim proc As New System.Diagnostics.Process() proc.StartInfo.FileName = Me.saveFileDialog1.FileName proc.Start()
Screenshot
The below screen shot shows the column “Description” is hidden.
Samples:
C#: Excel Export_CS
VB: Excel Export_VB
Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/exporting