تنسيق الخلايا

مقدمة

قم بتنسيق Cells باستخدام أساليب GetStyle و SetStyle

قم بتطبيق أنواع مختلفة من أنماط التنسيق على الخلايا لتعيين ألوان الخلفية أو المقدمة ، والحدود ، والخطوط ، والمحاذاة الأفقية والرأسية ، ومستوى المسافة البادئة ، واتجاه النص ، وزاوية التدوير والمزيد.

استخدام أساليب GetStyle و SetStyle

إذا احتاج المطورون إلى تطبيق أنماط تنسيق مختلفة على خلايا مختلفة ، فمن الأفضل الحصول على ملفأسلوب من الخلية باستخدامCell.GetStyle الطريقة ، حدد سمات النمط ثم قم بتطبيق التنسيق باستخدامCell.SetStyleطريقة. يوجد مثال أدناه لتوضيح هذا الأسلوب لتطبيق تنسيقات مختلفة على خلية.

// 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();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining a Style object
Aspose.Cells.Style style;
// Get the style from A1 cell
style = cell.GetStyle();
// Setting the vertical alignment
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text
style.Font.Color = Color.Green;
// Setting to shrink according to the text contained in it
style.ShrinkToFit = true;
// Setting the bottom border color to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Applying the style to A1 cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

استخدام نمط الكائن لتنسيق مختلف Cells

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

  1. أضفأسلوب عن طريق استدعاءخلق نمط طريقةدفتر العملصف دراسي
  2. الوصول إلى ملفأسلوبموضوع
  3. قم بتعيين الخصائص / السمات المطلوبة لملفأسلوبلتطبيق إعدادات التنسيق المطلوبة
  4. قم بتعيين ملفأسلوبكائن على الخلايا التي تريدها

يمكن أن يؤدي هذا النهج إلى تحسين كفاءة تطبيقاتك بشكل كبير وتوفير الذاكرة أيضًا.

// 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();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the first worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Adding a new Style
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Assigning the Style object to the "A1" cell
cell.SetStyle(style);
// Apply the same style to some other cells
worksheet.Cells["B1"].SetStyle(style);
worksheet.Cells["C1"].SetStyle(style);
worksheet.Cells["D1"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

استخدام Microsoft Excel 2007 الأنماط المعرفة مسبقًا

إذا كنت بحاجة إلى تطبيق أنماط تنسيق مختلفة لـ Microsoft Excel 2007 ، فقم بتطبيق الأنماط باستخدام Aspose.Cells API. يوجد مثال أدناه لتوضيح هذا الأسلوب لتطبيق نمط معرف مسبقًا على خلية.

// 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 workbook = new Workbook();
// Create a style object .
Style style = workbook.CreateStyle();
// Input a value to A1 cell.
workbook.Worksheets[0].Cells["A1"].PutValue("Test");
// Apply the style to the cell.
workbook.Worksheets[0].Cells["A1"].SetStyle(style);
// Save the Excel 2007 file.
workbook.Save(dataDir + "book1.out.xlsx");

تنسيق الأحرف المحددة في Cell

يشرح التعامل مع إعدادات الخط كيفية تنسيق النص في الخلايا ، ولكنه يشرح فقط كيفية تنسيق محتوى الخلية بالكامل. ماذا لو كنت تريد تنسيق الأحرف المختارة فقط؟

Aspose.Cells يدعم هذه الميزة أيضًا. يشرح هذا الموضوع كيفية استخدام هذه الميزة بشكل فعال.

تنسيق الأحرف المحددة

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

الCell فئة توفرالشخصياتطريقة تأخذ المعلمات التالية لتحديد نطاق من الأحرف داخل خلية:

  • فهرس البداية، فهرس الحرف الذي يبدأ منه التحديد.
  • عدد الشخصيات، عدد الأحرف المراد تحديده.

الالشخصيات تقوم الطريقة بإرجاع مثيل لـإعداد الخطفئة تسمح للمطورين بتنسيق الأحرف بنفس طريقة تنسيق الخلية كما هو موضح أدناه في مثال التعليمات البرمجية. في ملف الإخراج ، في الخلية A1 ، سيتم تنسيق كلمة “زيارة” بالخط الافتراضي ولكن “Aspose!” جريئة وأزرق.

// 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();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the font of selected characters to bold
cell.Characters(6, 7).Font.IsBold = true;
// Setting the font color of selected characters to blue
cell.Characters(6, 7).Font.Color = Color.Blue;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

تنسيق الصفوف والأعمدة

في بعض الأحيان ، يحتاج المطورون إلى تطبيق نفس التنسيق على الصفوف أو الأعمدة. غالبًا ما يستغرق تطبيق التنسيق على الخلايا واحدة تلو الأخرى وقتًا أطول ولا يعد حلاً جيدًا. لمعالجة هذه المشكلة ، يوفر Aspose.Cells طريقة بسيطة وسريعة تمت مناقشتها بالتفصيل في هذه المقالة.

تنسيق الصفوف والأعمدة

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

تنسيق صف

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

  1. أضفأسلوب يعترض علىدفتر العمل class عن طريق استدعاءخلق نمططريقة.
  2. تعيينأسلوبخصائص الكائن لتطبيق إعدادات التنسيق.
  3. اجعل السمات ذات الصلة قيد التشغيل لـالنمطموضوع.
  4. قم بتعيين ملفأسلوب يعترض علىصفموضوع.
// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a row from the Rows collection
Row row = worksheet.Cells.Rows[0];
// Assigning the Style object to the Style property of the row
row.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

تنسيق عمود

الCells توفر المجموعة أيضًا ملفالأعمدة مجموعة. كل عنصر فيالأعمدة تمثل المجموعة أعمود موضوع. على غرار أصف كائنعمود يقدم الكائن أيضًاتطبيقطريقة تنسيق العمود.

// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a column from the Columns collection
Column column = worksheet.Cells.Columns[0];
// Applying the style to the column
column.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

موضوعات مسبقة