تنسيق البيانات

النهج لتنسيق البيانات في الخلايا

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

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

يمكن للمطورين تنسيق الخلايا ومحتوياتها باستخدام واجهة برمجة Aspose.Cells المرنة. توفر Aspose.Cells فئة ، Workbook ، تمثل ملف Microsoft Excel. تحتوي فئة Workbook على WorksheetCollection التي تسمح بالوصول إلى كل ورقة عمل في ملف Excel. تُمثل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة خلايا. يُمثل كل عنصر في مجموعة Cells كائن Cell.

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

استخدام طريقة setStyle

عند تطبيق أنماط تنسيق مختلفة على خلايا مختلفة ، فمن الأفضل استخدام طريقة setStyle من فئة Cell. يظهر مثال أدناه لتوضيح استخدام طريقة setStyle لتطبيق إعدادات التنسيق المختلفة على خلية.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingCellsUsingsetStyleMethod.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
Style style = cell.getStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "output.xls");

استخدام كائن Style

عند تطبيق نفس النمط التنسيقي على خلايا مختلفة ، استخدم كائن Style.

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormattingCellsUsingStyleObject.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
// Adding a new Style to the styles collection of the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "FCUsingStyleObject_out.xls");

تطبيق تأثيرات تعبئة التدرج

لتطبيق تأثيرات ملء التدرج المرغوبة على الخلية ، استخدم طريقة setTwoColorGradient من كائن Style وفقًا لذلك.

مثال الكود

يتم تحقيق الإخراج التالي عن طريق تنفيذ الشيفرة أدناه.

تطبيق تأثيرات ملء التدرج

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ApplyGradientFillEffects.class) + "data/";
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Input a value into B3 cell
worksheet.getCells().get(2, 1).putValue("test");
// Get the Style of the cell
Style style = worksheet.getCells().get("B3").getStyle();
// Set Gradient pattern on
style.setGradient(true);
// Specify two color gradient fill effects
style.setTwoColorGradient(Color.fromArgb(255, 255, 255), Color.fromArgb(79, 129, 189),
GradientStyleType.HORIZONTAL, 1);
// Set the color of the text in the cell
style.getFont().setColor(Color.getRed());
// Specify horizontal and vertical alignment settings
style.setHorizontalAlignment(TextAlignmentType.CENTER);
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Apply the style to the cell
worksheet.getCells().get("B3").setStyle(style);
// Set the third row height in pixels
worksheet.getCells().setRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.getCells().merge(2, 1, 1, 2);
// Save the Excel file
workbook.save(dataDir + "ApplyGradientFillEffects_out.xlsx");

ضبط إعدادات المحاذاة

أي شخص قد استخدم Microsoft Excel لتنسيق الخلايا سيكون متعودًا على إعدادات المحاذاة في Microsoft Excel.

إعدادات التوجيه في Microsoft Excel

todo:image_alt_text

كما يمكنك رؤية من الشكل أعلاه، هناك أنواع مختلفة من خيارات المحاذاة:

كل إعدادات المحاذاة هذه مدعومة تمامًا بواسطة Aspose.Cells ويتم مناقشتها بمزيد من التفصيل أدناه.

ضبط إعدادات المحاذاة

يوفر Aspose.Cells فئة Workbook التي تمثل ملف Excel. تحتوي فئة Workbook على مجموعة WorksheetCollection التي تسمح بالوصول إلى كل ورقة في ملف Excel. يتم تمثيل ورقة العمل بواسطة فئة Worksheet.

توفر فئة Worksheet مجموعة خلايا. كل عنصر في مجموعة الخلايا يمثل كائن من فئة Cell.

يوفر Aspose.Cells الطريقة setStyle في فئة Cell التي تستخدم تنسيق الخلية. توفر فئة Style خصائص مفيدة لتكوين إعدادات الخط.

حدد أي نوع توجيه نص باستخدام تعداد TextAlignmentType. يتمثل أنواع توجيه النص المحددة مسبقًا في تعداد TextAlignmentType على النحو التالي:

** أنواع محاذاة النص ** ** الوصف **
Bottom يمثل محاذاة النص السفلي
Center يمثل محاذاة النص الوسطية
CenterAcross تمثل محاذاة النص في وسط النص
Distributed تمثل توزيع محاذاة النص
Fill تمثل ملء محاذاة النص
General تمثل محاذاة النص العامة
Justify تمثل محاذاة النص التبريري
Left يمثل محاذاة النص اليسار
Right يمثل محاذاة النص اليمين
Top يمثل محاذاة النص العلوي

المحاذاة الأفقية

استخدم طريقة setHorizontalAlignment لكائن النمط Style لمحاذاة النص أفقيًا.

يتم تحقيق الناتج التالي عن طريق تنفيذ رمز المثال أدناه:

محاذاة النص أفقيًا

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(TextAlignmentHorizontal.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "TAHorizontal_out.xls");

المحاذاة الرأسية

استخدم طريقة setVerticalAlignment لكائن النمط Style لمحاذاة النص رأسيًا.

يتم تحقيق الناتج التالي عندما يتم تعيين محاذاة رأسية إلى وسط.

محاذاة النص رأسيًا

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(TextAlignmentVertical.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setVerticalAlignment(TextAlignmentType.CENTER);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "TAVertical_out.xls");

المسافة البادئة

من الممكن تعيين مستوى المسافة البادئة للنص في خلية باستخدام طريقة setIndentLevel لكائن النمط Style.

يتم تحقيق الناتج التالي عندما يتم تعيين مستوى المسافة البادئة إلى 2.

تعديل مستوى المسافة البادئة إلى 2

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(Indentation.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setIndentLevel(2);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "Indentation_out.xls");

الاتجاه

قم بتعيين توجيه (دوران) النص في خلية باستخدام طريقة setRotationAngle لكائن النمط Style.

يتم تحقيق الناتج التالي عند تعيين زاوية الدوران إلى 25.

تم تعيين زاوية الدوران على 25

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(Orientation.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the rotation of the text (inside the cell) to 25
Style style1 = cell.getStyle();
style1.setRotationAngle(25);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "Orientation_out.xls");

التحكم في النص

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

تفاف النص

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

قم بتفعيل أو تعطيل تفاف النص باستخدام طريقة setTextWrapped() من كائن Style.

يتم تحقيق الناتج التالي عند تمكين تفاف النص.

النص الملتف داخل الخلية

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(WrapText.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Enabling the text to be wrapped within the cell
Style style1 = cell.getStyle();
style1.setTextWrapped(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "WrapText_out.xls");

تقليص للتناسب

خيار لتفاف النص في الحقل هو تقليص حجم النص ليتلاءم مع أبعاد الخلية. يتم ذلك عن طريق تعيين خاصية IsTextWrapped لكائن Style إلى صحيح.

يتم تحقيق الناتج التالي عند تقليص النص للملاءمة مع الخلية.

النص المقلص ليتناسب مع حدود الخلية

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ShrinkingToFit.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Shrinking the text to fit according to the dimensions of the cell
Style style1 = cell.getStyle();
style1.setShrinkToFit(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ShrinkingToFit_out.xls");

دمج الخلايا

مثل Microsoft Excel، يدعم Aspose.Cells دمج عدة خلايا في خلية واحدة.

يتم تحقيق الناتج التالي إذا تم دمج الخلايا الثلاث في الصف الأول لإنشاء خلية كبيرة واحدة.

دمج ثلاث خلايا لإنشاء خلية كبيرة

todo:image_alt_text

استخدم طريقة الدمج Merge من مجموعة Cells لدمج الخلايا. تأخذ طريقة الدمج التالية المعلمات التالية:

  • الصف الأول، الصف الأول من حيث يبدأ الدمج.
  • العمود الأول، العمود الأول من حيث يبدأ الدمج.
  • عدد الصفوف، عدد الصفوف المراد دمجها.
  • عدد الأعمدة، عدد الأعمدة المراد دمجها.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(MergingCellsInWorksheet.class) + "data/";
// Create a Workbook.
Workbook wbk = new Workbook();
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.getWorksheets().get(0);
// Create a Cells object to fetch all the cells.
Cells cells = worksheet.getCells();
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.getCells().get(5, 2).setValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.getCells().get(5, 2).getStyle();
// Create a Font object
Font font = style.getFont();
// Set the name.
font.setName("Times New Roman");
// Set the font size.
font.setSize(18);
// Set the font color
font.setColor(Color.getBlue());
// Bold the text
font.setBold(true);
// Make it italic
font.setItalic(true);
// Set the backgrond color of C6 Cell to Red
style.setForegroundColor(Color.getRed());
style.setPattern(BackgroundType.SOLID);
// Apply the Style to C6 Cell.
cells.get(5, 2).setStyle(style);
// Save the Workbook.
wbk.save(dataDir + "mergingcells_out.xls");
wbk.save(dataDir + "mergingcells_out.xlsx");
wbk.save(dataDir + "mergingcells_out.ods");
// Print message
System.out.println("Process completed successfully");

اتجاه النص

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

يتم تعيين ترتيب القراءة باستخدام خاصية TextDirection لكائن Style. توفر Aspose.Cells أنواع توجيه النص المحددة مسبقًا في تعداد TextDirectionType.

أنواع توجيه النص الوصف
Context ترتيب القراءة متسق مع لغة الحرف الأول المُدخل
LeftToRight الترتيب من اليسار إلى اليمين
RightToLeft الترتيب من اليمين إلى اليسار
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ChangeTextDirection.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the text direction from right to left
Style style1 = cell.getStyle();
style1.setTextDirection(TextDirectionType.RIGHT_TO_LEFT);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ChangeTextDirection_out.xls");

يتم تحقيق الإخراج التالي إذا تم تعيين ترتيب القراءة للنص إلى اليمين إلى اليسار.

ضبط ترتيب قراءة النص إلى اليمين إلى اليسار

todo:image_alt_text

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

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

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

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

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

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

  • فهرس البداية، فهرس الحرف للبدء في التحديد منه.
  • عدد الحروف, عدد الأحرف المراد تحديدها.

في ملف الإخراج، في الخلية “A1”، يتم تنسيق الكلمة ‘زيارة’ بالخط الافتراضي ولكن ‘أسبوز!’ بخط عريض وأزرق.

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

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getSharedDataDir(FormattingSelectedCharacters.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose!");
Font font = cell.characters(6, 7).getFont();
// Setting the font of selected characters to bold
font.setBold(true);
// Setting the font color of selected characters to blue
font.setColor(Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "FSCharacters_out.xls");

تفعيل الأوراق وجعل خلية نشطة أو تحديد مجموعة من الخلايا في ورقة العمل

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

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

تنشيط ورقة عمل وجعل خلية نشطة

توفر Aspose.Cells واجهة برمجة تطبيقات محددة لهذه المهام. على سبيل المثال ، الطريقة WorksheetCollection.setActiveSheetIndex مفيدة لتحديد ورقة عمل نشطة. بالمثل ، تُستخدم الطريقة Worksheet.setActiveCell لتعيين والحصول على خلية نشطة في ورقة عمل.

إذا كنت ترغب في أن تتم تمرير شريطي التمرير الأفقي والرأسي إلى موضع فهرس الصف والعمود لإعطاء رؤية جيدة للبيانات المحددة عند فتح الملف في Microsoft Excel ، استخدم خصائص Worksheet.setFirstVisibleRow و Worksheet.setFirstVisibleColumn.

المثال التالي يوضح كيفية تنشيط ورقة عمل وجعل خلية فيها نشطة. يتم تمرير أشرطة التمرير لجعل صف وعمود الظاهر الخاصين أولاً كصف وعمود.

تعيين الخلية B2 كخلية نشطة

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(MakeCellActive.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");
// Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);
// Save the Excel file.
workbook.save(dataDir + "MakeCellActive_out.xls");

اختيار مجموعة من الخلايا في ورقة العمل

توفر Aspose.Cells الطريقة Worksheet.selectRange(int startRow, int startColumn, int totalRows, int totalColumns, bool removeOthers). باستخدام المعلمة الأخيرة - removeOthers - إلى true ، يتم إزالة تحديدات الخلية أو مجموعة الخلايا الأخرى في الورقة.

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SelectRangeofCellsinWorksheet.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Select range of cells(A1:E10) in the worksheet.
worksheet1.selectRange(0, 0, 10, 5, true);
// Save the Excel file.
workbook.save(dataDir + "SROfCInWorksheet_out.xlsx");

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

تنسيق الصفوف والأعمدة في جدول بيانات لإعطاء تقرير مظهر هو أمين الميزة الأكثر استخداماً في تطبيق Excel. توفر واجهات برمجة تطبيقات Aspose.Cells أيضًا هذه الوظيفة من خلال نموذج بياناتها الذي يكشف في المقام الأول عن الفصل الذي يتعامل مع جميع الميزات المتعلقة بالتنسيقات مثل الشكل وسماته وتوجيه النص وألوان الخلفية / الأمامية ، الحدود ، عرض التنسيق للأرقام وتواريخ الأرقام وما إلى ذلك. فصل آخر مفيد توفره واجهات برمجة تطبيقات Aspose.Cells هو فصل التنسيق الذي يسمح بإعادة استخدام كائن التنسيق. 

في هذه المقالة ، سنحاول شرح كيفية استخدام Aspose.Cells for Java API لتطبيق التنسيق على الصفوف والأعمدة. 

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

توفر Aspose.Cells فصلاً ، Workbook الذي يمثل ملف Microsoft Excel. يحتوي فصل Workbook على مجموعة ورق عمل تسمح بالوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطة فصل Worksheet. يوفر فصل Worksheet مجموعة Cells. توفر مجموعة Cells مجموعة Rows.

تنسيق صف

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

لتطبيق نفس التنسيق على صف معين، استخدم كائن Style.

  1. أضف كائن Style إلى فئة Workbook عبر استدعاء طريقة createStyle.
  2. قم بتعيين خصائص كائن Style لتطبيق إعدادات التنسيق.
  3. قم بتعيين كائن Style المكون للمصفوفة إلى طريقة applyStyle من كائن Row.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormattingARow.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
Row row = cells.getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "FormattingARow_out.xls");

تنسيق عمود

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormattingAColumn.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
Column column = cells.getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "FormattingAColumn_out.xls");

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

إذا كانت الحاجة إلى تحديد تنسيق العرض للأرقام والتواريخ لصف أو عمود كاملة فإن العملية تقريباً مماثلة للأعلى، ولكن بدلاً من تعيين المعلمات لمحتويات النص، ستقوم بتعيين التنسيق للأرقام والتواريخ باستخدام خصائص Style.Number أو Style.Custom. يرجى الملاحظة أن خصائص Style.Number من نوع عدد صحيح وتشير إلى التنسيقات المدمجة للأرقام والتواريخ، بينما خصائص Style.Custom من نوع string وتقبل الأنماط الصالحة.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingDisplayFormat.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles collection of the Workbook object
Style style = workbook.createStyle();
// Setting the Number property to 4 which corresponds to the pattern #,##0.00
style.setNumber(4);
// Creating an object of StyleFlag
StyleFlag flag = new StyleFlag();
// Setting NumberFormat property to true so that only this aspect takes effect from Style object
flag.setNumberFormat(true);
// Applying style to the first row of the worksheet
worksheet.getCells().getRows().get(0).applyStyle(style, flag);
// Re-initializing the Style object
style = workbook.createStyle();
// Setting the Custom property to the pattern d-mmm-yy
style.setCustom("d-mmm-yy");
// Applying style to the first column of the worksheet
worksheet.getCells().getColumns().get(0).applyStyle(style, flag);
// Saving spreadsheet on disc
workbook.save(dataDir + "SDisplayFormat_out.xlsx");