تطبيق التنسيق الشرطي في أوراق العمل

استخدام Aspose.Cells لتطبيق الصياغة الشرطية بناءً على قيمة Cell

  1. قم بتنزيل وتثبيت Aspose.Cells.
  2. تنزيل Aspose.Cells for .NET.
  3. قم بتثبيته على جهاز الكمبيوتر الخاص بك. جميع مكونات Aspose ، عند تثبيتها ، تعمل في وضع التقييم. لا يوجد حد زمني لوضع التقييم ويقوم فقط بحقن العلامات المائية في المستندات المنتجة.
  4. أنشئ مشروعًا. ابدأ تشغيل Visual Studio.NET وقم بإنشاء تطبيق وحدة تحكم جديد. يقوم هذا المثال بإنشاء تطبيق وحدة تحكم C# ، ولكن يمكنك استخدام VB.NET أيضًا.
  5. أضف المراجع. أضف مرجعًا إلى Aspose.Cells إلى مشروعك ، على سبيل المثال أضف مرجعًا إلى…. \ Program Files \ Aspose \ Aspose.Cells \ Bin \ Net1.0 \ Aspose.Cells.dll
    • تطبيق التنسيق الشرطي على أساس قيمة الخلية. يوجد أدناه الكود المستخدم لإنجاز المهمة. أقوم بتطبيق التنسيق الشرطي على خلية.
// 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);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.BackgroundColor = Color.Red;
// Saving the Excel file
workbook.Save(dataDir+ "output.out.xls", SaveFormat.Auto);

عند تنفيذ الكود أعلاه ، يتم تطبيق التنسيق الشرطي على الخلية “A1” في ورقة العمل الأولى من ملف الإخراج (output.xls). يعتمد التنسيق الشرطي المطبق على A1 على قيمة الخلية. إذا كانت قيمة الخلية A1 بين 50 و 100 ، يكون لون الخلفية أحمر بسبب التنسيق الشرطي المطبق.

استخدام Aspose.Cells لتطبيق الصياغة الشرطية على أساس الصيغة

  1. تطبيق التنسيق الشرطي حسب الصيغة (Code Snippet) يوجد أدناه رمز لإنجاز المهمة. يتم تطبيق التنسيق الشرطي على B3.
// 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);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca = new CellArea();
ca.StartRow = 2;
ca.EndRow = 2;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.Expression);
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)";
fc.Style.BackgroundColor = Color.Red;
sheet.Cells["B3"].Formula = "=SUM(B1:B2)";
sheet.Cells["C4"].PutValue("If Sum of B1:B2 is greater than 100, B3 will have RED background");
// Saving the Excel file
workbook.Save(dataDir+ "output.out.xls", SaveFormat.Auto);

عند تنفيذ الكود أعلاه ، يتم تطبيق التنسيق الشرطي على الخلية “B3” في ورقة العمل الأولى لملف الإخراج (output.xls). يعتمد التنسيق الشرطي المطبق على الصيغة التي تحسب قيمة “B3” كمجموع B1 & B2.