تنسيق البيانات
مقاربات لتنسيق البيانات في Cells
من الحقائق الشائعة أنه إذا تم تنسيق خلايا ورقة العمل بشكل صحيح ، فسيصبح من السهل على المستخدمين قراءة محتويات (بيانات) الخلية. توجد طرق عديدة لتنسيق الخلايا ومحتوياتها. إن أبسط طريقة هي تنسيق الخلايا باستخدام Microsoft Excel في بيئة WYSIWYG أثناء إنشاء جدول بيانات مصمم. بعد إنشاء جدول بيانات المصمم ، يمكنك فتح جدول البيانات باستخدام Aspose.Cells مع الاحتفاظ بجميع إعدادات التنسيق المحفوظة في جدول البيانات. هناك طريقة أخرى لتنسيق الخلايا ومحتوياتها وهي استخدام Aspose.Cells API. في هذا الموضوع ، سنصف طريقتين لتنسيق الخلايا ومحتوياتها باستخدام Aspose.Cells API.
التنسيق Cells
يمكن للمطورين تنسيق الخلايا ومحتوياتها باستخدام API المرن من Aspose.Cells. يوفر Aspose.Cells فئة ،دفتر العمل ، يمثل ملف Excel Microsoft. الدفتر العمل فئة تحتوي علىورقة العمل يسمح بالوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطةورقة عمل صف دراسي. الورقة عمل فئة توفر مجموعة Cells. كل عنصر فيCellsتمثل المجموعة كائنًا منCell صف دراسي.
يوفر Aspose.Cells ملفأسلوب الممتلكات فيCell فئة ، تُستخدم لتعيين نمط تنسيق الخلية. علاوة على ذلك ، يوفر Aspose.Cells أيضًاأسلوب فئة تُستخدم لخدمة نفس الغرض. قم بتطبيق أنواع مختلفة من أنماط التنسيق على الخلايا لتعيين ألوان الخلفية أو المقدمة ، والحدود ، والخطوط ، والمحاذاة الأفقية والرأسية ، ومستوى المسافة البادئة ، واتجاه النص ، وزاوية التدوير والمزيد.
باستخدام طريقة 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"); |
استخدام كائن النمط
عند تطبيق نفس نمط التنسيق على خلايا مختلفة ، استخدم ملحقأسلوب موضوع.
- أضفأسلوب إلى مجموعة Styles الخاصة بـدفتر العمل class عن طريق استدعاء طريقة createStyle لفئة المصنف.
- قم بالوصول إلى كائن النمط المضاف حديثًا من مجموعة الأنماط.
- قم بتعيين الخصائص المطلوبة لكائن النمط لتطبيق إعدادات التنسيق المطلوبة.
- قم بتعيين كائن Style الذي تم تكوينه إلى خاصية 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 الخاص بكائن النمط وفقًا لذلك.
مثال رمز
يتم تحقيق الإخراج التالي عن طريق تنفيذ الكود أدناه.
تطبيق تأثيرات تعبئة متدرجة
// 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
كما ترى من الشكل أعلاه ، هناك أنواع مختلفة من خيارات المحاذاة:
- محاذاة النص (افقي عمودي)
- المسافة الفارغة.
- اتجاه.
- التحكم بالنص.
- اتجاه النص.
يتم دعم كافة إعدادات المحاذاة هذه بالكامل بواسطة Aspose.Cells وتتم مناقشتها بمزيد من التفاصيل أدناه.
تكوين إعدادات المحاذاة
Aspose.Cells يوفر فصل دراسي ،دفتر العمل ، يمثل ملف Excel. تحتوي فئة المصنف على مجموعة أوراق العمل التي تتيح الوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطةورقة عمل صف دراسي.
توفر فئة ورقة العمل مجموعة Cells. يمثل كل عنصر في مجموعة Cells عنصرًا من عناصرCell صف دراسي.
يوفر Aspose.Cells طريقة setStyle في ملفCell الفئة المستخدمة في تنسيق الخلية. الأسلوب توفر فئة خصائص مفيدة لتكوين إعدادات الخط.
حدد أي نوع من أنواع محاذاة النص باستخدام تعداد TextAlignmentType. أنواع محاذاة النص المحددة مسبقًا في تعداد TextAlignmentType هي:
أنواع محاذاة النص | وصف |
---|---|
قاع | يمثل محاذاة النص السفلي |
مركز | يمثل مركز محاذاة النص |
CenterAcross | يمثل المركز عبر محاذاة النص |
وزعت | يمثل محاذاة النص الموزع |
ملء | يمثل محاذاة النص التعبئة |
عام | يمثل محاذاة نص عامة |
يبرر | التمثيلات تضبط محاذاة النص |
اليسار | يمثل محاذاة النص الأيسر |
الصحيح | يمثل محاذاة النص الصحيحة |
قمة | يمثل محاذاة النص العلوي |
يمكنك أيضًا تطبيق إعداد الضبط الموزع باستخدام طريقة Style.setJustifyDistributed ().
|
المحاذاة الأفقية
استخدم الأسلوب أسلوب setHorizontalAlignment للكائن لمحاذاة النص أفقيًا.
يتم تحقيق الإخراج التالي عن طريق تنفيذ رمز المثال أدناه:
محاذاة النص أفقيا
// 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 للكائن لمحاذاة النص عموديًا.
يتم تحقيق الإخراج التالي عند ضبط VerticalAlignment على المركز.
محاذاة النص عموديا
// 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 الخاص بالكائن.
يتم تحقيق الإخراج التالي عند ضبط IndentLevel على 2.
تم ضبط مستوى المسافة البادئة على 2
// 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 للكائن.
يتم تحقيق الإخراج التالي عند ضبط زاوية الدوران على 25.
ضبط زاوية الدوران على 25
// 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 للكائن.
يتم تحقيق الإخراج التالي عند تمكين التفاف النص.
نص ملفوف داخل الخلية
// 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 إلىحقيقي.
يتم تحقيق الإخراج التالي عندما يتم تقليص النص ليلائم الخلية.
تم تقليص النص ليلائم حدود الخلية
// 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"); |
دمج Cells
مثل Microsoft Excel ، يدعم Aspose.Cells دمج عدة خلايا في خلية واحدة.
يتم تحقيق الإخراج التالي إذا تم دمج الخلايا الثلاث في الصف الأول لإنشاء خلية مفردة كبيرة.
تم دمج ثلاث خلايا لإنشاء خلية كبيرة
استخدم ال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 للكائن. يوفر Aspose.Cells أنواع اتجاهات النص المحددة مسبقًا في تعداد TextDirectionType.
أنواع اتجاه النص | وصف |
---|---|
سياق الكلام | ترتيب القراءة المتوافق مع لغة أول حرف تم إدخاله |
من اليسار إلى اليمين | ترتيب القراءة من اليسار إلى اليمين |
من اليمين الى اليسار | ترتيب القراءة من اليمين إلى اليسار |
// 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"); |
يتم تحقيق الإخراج التالي إذا تم ضبط ترتيب قراءة النص من اليمين إلى اليسار.
ضبط ترتيب قراءة النص من اليمين إلى اليسار
تنسيق الأحرف المحددة في Cell
التعامل مع إعدادات الخطشرح كيفية تنسيق الخلايا ولكن فقط كيفية تنسيق محتوى الخلايا wntire. ماذا لو كنت تريد تنسيق الأحرف المختارة فقط؟
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-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"); |
تنشيط الأوراق وإجراء Cell نشطًا أو تحديد نطاق Cells في ورقة العمل
في بعض الأحيان ، قد تحتاج إلى تنشيط ورقة عمل محددة بحيث تكون أول ورقة يتم عرضها عندما يفتح شخص ما الملف في Microsoft Excel. قد تحتاج أيضًا إلى تنشيط خلية معينة بحيث يتم تمرير أشرطة التمرير إلى الخلية النشطة بحيث تكون مرئية بوضوح. Aspose.Cells قادر على القيام بجميع المهام المذكورة أعلاه.
الورقة النشطة هي الورقة التي تعمل عليها في مصنف. يكون الاسم الموجود في علامة تبويب الورقة النشطة غامقًا بشكل افتراضي. في هذه الأثناء ، الخلية النشطة هي الخلية المحددة والتي يتم إدخال البيانات فيها عند بدء الكتابة. فقط خلية واحدة نشطة في كل مرة. الخلية النشطة محاطة بحد ثقيل لجعلها تظهر ضد الخلايا الأخرى. يسمح لك Aspose.Cells أيضًا بتحديد نطاق من الخلايا في ورقة العمل.
تنشيط ورقة وتنشيط Cell
يوفر Aspose.Cells API محددًا لهذه المهام. على سبيل المثال ، تعتبر طريقة WorksheetCollection.setActiveSheetIndex مفيدة لتعيين ورقة نشطة. وبالمثل ، يتم استخدام طريقة Worksheet.setActiveCell لتعيين خلية نشطة والحصول عليها في ورقة العمل.
إذا كنت تريد تمرير أشرطة التمرير الأفقية والعمودية إلى موضع فهرس الصف والعمود لإعطاء عرض جيد للبيانات المحددة عند فتح الملف في Microsoft Excel ، فاستخدم خصائص Worksheet.setFirstVisibleRow و Worksheet.setFirstVisibleColumn.
يوضح المثال التالي كيفية تنشيط ورقة عمل وتنشيط خلية فيها. يتم تمرير أشرطة التمرير لجعل الصف الثاني والعمود الثاني أول صف وعمود مرئيين.
تعيين خلية B2 كخلية نشطة
// 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"); |
تحديد نطاق Cells في ورقة العمل
يوفر 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 وهي StyleFlag التي تسمح بإعادة استخدام كائن النمط.
سنحاول في هذه المقالة شرح كيفية استخدام Aspose.Cells for Java API لتطبيق التنسيق على الصفوف والأعمدة.
تنسيق الصفوف والأعمدة
Aspose.Cells يوفر فصل دراسي ،دفتر العمل يمثل ملف Excel Microsoft. الدفتر العمل فئة تحتوي على مجموعة أوراق العمل التي تتيح الوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطة فئة ورقة العمل. الورقة عمل توفر فئة المجموعة Cells. توفر مجموعة Cells مجموعة صفوف.
تنسيق صف
يمثل كل عنصر في مجموعة الصفوف كائن صف. يقدم الكائن Row طريقة applyStyle المستخدمة لتطبيق التنسيق على صف.
لتطبيق نفس التنسيق على صف ، استخدم كائن النمط:
- أضف كائن Style إلى فئة Workbook عن طريق استدعاء أسلوب createStyle الخاص به.
- قم بتعيين خصائص كائن النمط لتطبيق إعدادات التنسيق.
- قم بتعيين كائن 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"); |
تنسيق عمود
توفر مجموعة Cells مجموعة أعمدة. يمثل كل عنصر في مجموعة الأعمدة كائن عمود. على غرار كائن الصف ، يقدم كائن العمود طريقة applicationStyle المستخدمة لتعيين تنسيق العمود. استخدم طريقة 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"); |
ضبط تنسيق العرض Numbers وتواريخ الصفوف والأعمدة
إذا كان المتطلب هو تعيين تنسيق عرض الأرقام والتواريخ لصف أو عمود كامل ، فستكون العملية أكثر أو أقل كما تمت مناقشته أعلاه ، ومع ذلك ، بدلاً من تعيين معلمات للمحتويات النصية ، ستقوم بتعيين التنسيق للأرقام والتواريخ باستخدام Style.Number أو Style.Custom. يرجى ملاحظة أن الخاصية Style.Number هي من النوع الصحيح وتشير إلى تنسيقات الأرقام والتاريخ المضمنة ، بينما الخاصية Style.Custom هي من نوع السلسلة وتقبل الأنماط الصالحة.
// 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"); |