Gordana Tasic

Gordana Tasic

  • 1.6k
  • 96
  • 13.7k

How to protect certain columns in an Excel file from modifications?

Mar 21 2021 4:09 PM
I'm creating an Excel file using C#, but I want some columns to be protected from modification. The problem is that I don't know how many rows I'm going to have in that file. I used the following code,I know there is a Range, but I need a universal solution when I don't know how many rows an Excel file has:
  1. xlWorkSheet.Cells.Locked = false;  
  2.  xlWorkSheet.Range["A1:A3"].Locked = true;  
  3.             xlWorkSheet.Range["B1"].Locked = true;  
  4.             xlWorkSheet.Range["C1"].Locked = true;  
  5.             xlWorkSheet.Range["D1:D3"].Locked = true;  
  6.             xlWorkSheet.Range["E1:E3"].Locked = true;  
  7.             xlWorkSheet.Range["F1:F3"].Locked = true;  
  8.             xlWorkSheet.Protect(misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue); 
 


Answers (1)