حماية أوراق العمل

حماية أوراق العمل

مقدمة

خيارات الحماية العامة في Microsoft Excel هي:

  • محتويات
  • أشياء
  • سيناريوهات

لا تخفي أوراق العمل المحمية البيانات الحساسة أو تحميها ، لذا فهي تختلف عن تشفير الملفات. بشكل عام ، تعد حماية ورقة العمل مناسبة لأغراض العرض. يمنع المستخدم النهائي من تعديل البيانات والمحتوى والتنسيق في ورقة العمل.

حماية ورقة العمل

Aspose.Cells يوفر فصل دراسي ،دفتر العمل يمثل ملف Excel Microsoft. الدفتر العمل فئة تحتوي علىأوراق عمل مجموعة تسمح بالوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطةورقة عملصف دراسي.

الورقة عمل فئة توفريحمي الطريقة المستخدمة لتطبيق الحماية على ورقة العمل.يحمي يقبل الأسلوب المعلمات التالية:

  • نوع الحماية ، نوع الحماية المراد تطبيقها على ورقة العمل. يتم تطبيق نوع الحماية بمساعدةنوع الحمايةتعداد.
  • كلمة المرور الجديدة ، كلمة المرور الجديدة المستخدمة لحماية ورقة العمل.
  • كلمة المرور القديمة ، كلمة المرور القديمة ، إذا كانت ورقة العمل محمية بكلمة مرور بالفعل. إذا لم تكن ورقة العمل محمية بالفعل ، فقم فقط بتمريرها فارغة.

النوع الحمايةيحتوي التعداد على أنواع الحماية المحددة مسبقًا التالية:

أنواع الحماية وصف
الجميع لا يمكن للمستخدم تعديل أي شيء في ورقة العمل هذه
محتويات لا يمكن للمستخدم إدخال البيانات في ورقة العمل هذه
أشياء لا يمكن للمستخدم تعديل الكائنات الرسومية
سيناريوهات لا يمكن للمستخدم تعديل السيناريوهات المحفوظة
بنية لا يمكن للمستخدم تعديل الهيكل
Windows يتم تطبيق الحماية على النوافذ
لا أحد لا يتم تطبيق الحماية

يوضح المثال أدناه كيفية حماية ورقة العمل بكلمة مرور.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook excel = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = excel.Worksheets[0];
// Protecting the worksheet with a password
worksheet.Protect(ProtectionType.All, "aspose", null);
// Saving the modified Excel file in default format
excel.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);
// Closing the file stream to free all resources
fstream.Close();

بعد استخدام الكود أعلاه لحماية ورقة العمل ، يمكنك التحقق من الحماية على ورقة العمل عن طريق فتحها. بمجرد فتح الملف ومحاولة إضافة بعض البيانات إلى ورقة العمل ، سترى مربع الحوار التالي:

مربع حوار يحذر من أن المستخدم لا يمكنه تعديل ورقة العمل
ما يجب القيام به: image_بديل_نص

للعمل على ورقة العمل ، قم بإلغاء حماية ورقة العمل عن طريق تحديد ملفحماية ، ومن بعدورقة غير محمية منأدوات عنصر القائمة.

بعد تحديد عنصر قائمة Unprotect Sheet ، سيتم فتح مربع حوار يطالبك بإدخال كلمة المرور حتى تتمكن من العمل في ورقة العمل كما هو موضح أدناه:

|ما يجب القيام به: image_بديل_نص|

قم بحماية بعض Cells في ورقة العمل باستخدام Microsoft Excel

قد تكون هناك سيناريوهات معينة تحتاج فيها إلى قفل بضع خلايا فقط في ورقة العمل. إذا كنت تريد قفل بعض الخلايا المحددة في ورقة العمل ، فيجب عليك إلغاء قفل جميع الخلايا الأخرى في ورقة العمل. تمت تهيئة جميع الخلايا الموجودة في ورقة العمل بالفعل للقفل ، يمكنك التحقق من فتح أي ملف Excel في MS Excel والنقر فوق الزرتنسيق | Cells … ليعرضشكل Cellsمربع الحوار ثم انقر فوقحماية علامة التبويب وشاهد مربع الاختيار المسمى “مغلق” محددًا بشكل افتراضي.

توضح النقاط التالية كيفية قفل بعض الخلايا باستخدام MS Excel. تنطبق هذه الطريقة على Microsoft Office Excel 97 و 2000 و 2002 و 2003 والإصدارات الأحدث.

  1. حدد ورقة العمل بأكملها بالنقر فوقاختر الكل زر (المستطيل الرمادي أعلى رقم الصف مباشرة للصف 1 وعلى يسار حرف A من العمود).
  2. انقرCells على الشكل القائمة ، انقر فوقحماية علامة التبويب ، ثم قم بإلغاء تحديدمقفل خانة الاختيار. هذا يفتح جميع الخلايا في ورقة العمل إذا كانCells الأمر غير متوفر ، فقد تكون أجزاء من ورقة العمل مؤمنة بالفعل. على الأدوات القائمة ، أشر إلىحماية ، ثم انقر فوقورقة غير محمية.
  3. حدد فقط الخلايا التي تريد قفلها وكرر الخطوة 2 ، ولكن هذه المرة حدد ملفمقفل خانة الاختيار.
  4. على الأدوات القائمة ، أشر إلىحماية ، انقرورقة حماية ثم انقر فوقنعم.
  5. في الورقة حماية في مربع الحوار ، لديك خيار تحديد كلمة مرور وتحديد العناصر التي تريد أن يتمكن المستخدمون من تغييرها.

قم بحماية بعض Cells في ورقة العمل باستخدام Aspose Cells

في هذه الطريقة ، نستخدم Aspose.Cells API فقط للقيام بالمهمة.

مثال: يوضح المثال التالي كيفية حماية بعض الخلايا في ورقة العمل. يقوم بإلغاء تأمين جميع الخلايا الموجودة في ورقة العمل أولاً ثم قفل 3 خلايا (A1 ، B1 ، C1) فيها. أخيرًا ، يحمي ورقة العمل. الأسلوبيحتوي الكائن على خاصية منطقية ،مقفل . يمكنك ضبطمقفل الخاصية إلى صح أو خطأ وتطبيقهاالعمود / الصف. طريقة لقفل أو إلغاء قفل الصف / العمود بالسمات التي تريدها.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];
// Define the style object.
Style style;
// Define the styleflag object
StyleFlag styleflag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
style = sheet.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
styleflag = new StyleFlag();
styleflag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, styleflag);
}
// Lock the three cells...i.e. A1, B1, C1.
style = sheet.Cells["A1"].GetStyle();
style.IsLocked = true;
sheet.Cells["A1"].SetStyle(style);
style = sheet.Cells["B1"].GetStyle();
style.IsLocked = true;
sheet.Cells["B1"].SetStyle(style);
style = sheet.Cells["C1"].GetStyle();
style.IsLocked = true;
sheet.Cells["C1"].SetStyle(style);
// Finally, Protect the sheet now.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

حماية صف في ورقة العمل

Aspose.Cells يسمح لك بقفل أي صف في ورقة العمل بسهولة. هنا ، يمكننا الاستفادة منتطبيق ستايل () طريقةAspose.Cells.Row فئة للتطبيقأسلوب إلى صف معين في ورقة العمل. تأخذ هذه الطريقة حجتين: أأسلوب كائن والنمط الكائن الذي يحتوي على جميع الأعضاء المرتبطين بالتنسيق المطبق.

يوضح المثال التالي كيفية حماية صف في ورقة العمل. يقوم بإلغاء تأمين جميع الخلايا الموجودة في ورقة العمل أولاً ثم يقفل الصف الأول فيها. أخيرًا ، يحمي ورقة العمل. الأسلوب يحتوي الكائن على خاصية منطقية ،مقفل . يمكنك ضبطمقفل الخاصية إلى صواب أو خطأ لقفل أو إلغاء قفل الصف / العمود باستخدامالنمطموضوع.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
style = sheet.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first row style.
style = sheet.Cells.Rows[0].GetStyle();
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first row.
sheet.Cells.ApplyRowStyle(0, style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

حماية عمود في ورقة العمل

يسمح لك Aspose.Cells بقفل أي عمود في ورقة العمل بسهولة. هنا ، يمكننا الاستفادة منتطبيق ستايل () طريقةAspose.Cells.Column فئة للتطبيقأسلوب إلى عمود معين في ورقة العمل. تأخذ هذه الطريقة حجتين: أأسلوب كائن والنمطالكائن الذي يحتوي على جميع الأعضاء المرتبطين بالتنسيق المطبق.

يوضح المثال التالي كيفية حماية عمود في ورقة العمل. يقوم بإلغاء تأمين جميع الخلايا الموجودة في ورقة العمل أولاً ثم يقوم بتأمين العمود الأول فيها. أخيرًا ، يحمي ورقة العمل. الأسلوب يحتوي الكائن على خاصية منطقية ،مقفل . يمكنك ضبطمقفل الخاصية إلى صواب أو خطأ لقفل أو إلغاء قفل الصف / العمود باستخدامالنمطموضوع.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
style = sheet.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first column style.
style = sheet.Cells.Columns[0].GetStyle();
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first column.
sheet.Cells.Columns[0].ApplyStyle(style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

السماح للمستخدمين بتحرير النطاقات

يوضح المثال التالي كيفية السماح للمستخدمين بتحرير نطاق في ورقة عمل محمية.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a new Workbook
Workbook book = new Workbook();
// Get the first (default) worksheet
Worksheet sheet = book.Worksheets[0];
// Get the Allow Edit Ranges
ProtectedRangeCollection allowRanges = sheet.AllowEditRanges;
// Define ProtectedRange
ProtectedRange proteced_range;
// Create the range
int idx = allowRanges.Add("r2", 1, 1, 3, 3);
proteced_range = allowRanges[idx];
// Specify the passoword
proteced_range.Password = "123";
// Protect the sheet
sheet.Protect(ProtectionType.All);
// Save the Excel file
book.Save(dataDir + "protectedrange.out.xls");