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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to lock the specific range of cells while exporting grid to Excel?

Platform: WinForms |
Control: GridControl |
Published Date: January 6, 2017 |
Last Revised Date: May 28, 2019

In order to lock the columns when you export grid to excel, you can use QueryImportExportCellInfo event of GridExcelConverterControl class. The Protect() method can be used for protecting all the cells in sheet with specified password. To Lock the specific range of cells, all other cells should be unlocked by setting the Locked property as false. 

Code Snippet

C#

ExcelEngine engine = new ExcelEngine();
IApplication app = engine.Excel.Application;
IWorkbook book = app.Workbooks.Create(1);
 
Syncfusion.GridExcelConverter.GridExcelConverterControl gecc = new Syncfusion.GridExcelConverter.GridExcelConverterControl();
 
//Triggering the event
gecc.QueryImportExportCellInfo += new Syncfusion.GridExcelConverter.GridImportExportCellInfoEventHandler(Gecc_QueryImportExportCellInfo);
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Files(*.xlsx)|*.xlsx|Files(*.xls)|*.xls";
saveFileDialog.DefaultExt = ".xlsx";
saveFileDialog.FileName = "Sample";
 
gecc.GridToExcel(this.gridControl1.Model, book.Worksheets[0]);
//Protect the cells with password.
book.Worksheets[0].Protect("Password", ExcelSheetProtection.LockedCells | ExcelSheetProtection.UnLockedCells);
book.SaveAs(saveFileDialog.FileName);
 
//Event Customization.
private void Gecc_QueryImportExportCellInfo(object sender, Syncfusion.GridExcelConverter.GridImportExportCellInfoEventArgs e)
{
            //Check whether the cell was locked in Grid or not.
 if (! e.GridCell.ReadOnly)
 {
  e.ExcelCell.Value = e.GridCell.CellValue.ToString();
  //Unlock the other cells.
  e.ExcelCell.CellStyle.Locked = false;
  e.Handled = true;
 }
}

 

VB

Dim engine As New ExcelEngine()
Dim app As IApplication = engine.Excel.Application
Dim book As IWorkbook = app.Workbooks.Create(1)
 
Dim gecc As New Syncfusion.GridExcelConverter.GridExcelConverterControl()
 
'Triggering the event
AddHandler gecc.QueryImportExportCellInfo, AddressOf Gecc_QueryImportExportCellInfo
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "Files(*.xlsx)|*.xlsx|Files(*.xls)|*.xls"
saveFileDialog.DefaultExt = ".xlsx"
saveFileDialog.FileName = "Sample"
 
gecc.GridToExcel(Me.gridControl1.Model, book.Worksheets(0))
'Protect the cells with password.
book.Worksheets(0).Protect("Password", ExcelSheetProtection.LockedCells Or ExcelSheetProtection.UnLockedCells)
book.SaveAs(saveFileDialog.FileName)
 
'Event Customization.
private void Gecc_QueryImportExportCellInfo(Object sender, Syncfusion.GridExcelConverter.GridImportExportCellInfoEventArgs e)
    'Check wheather the cell was locked in Grid or not.
    If Not e.GridCell.ReadOnly Then
        e.ExcelCell.Value = e.GridCell.CellValue.ToString()
        'Unlock the other cells.
        e.ExcelCell.CellStyle.Locked = False
        e.Handled = True
    End If

 

Screenshot

Showing locked column in GridControl

 

Showing message box for exception while changing the column in excel

 

Sample Links:

C# Excel Export with Locked Cells CS

VB Excel Export with Locked Cells VB

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.

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