التعامل مع إعدادات الخط

تكوين إعدادات الخط

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

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

يوضح هذا المقال كيفية:

تعيين اسم الخط

تطبيق خط معين على النص في الخلايا باستخدام Font الخاصية setName كائن.

// 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(SettingFontName.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("Hello Aspose!");
// Setting the font name to "Times New Roman"
Style style = cell.getStyle();
Font font = style.getFont();
font.setName("Times New Roman");
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SettingFontName_out.xls");

تعيين نمط الخط إلى عريض

تعيين النص إلى الخط العريض عن طريق تعيين Font الخاصية setBold كائن إلى true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the output directory.
String outputDir = Utils.Get_OutputDirectory();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(i);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.getStyle();
// Setting the font weight to bold
style.getFont().setBold(true);
// Applying the style to the cell
cell.setStyle(style);
// Saving the Excel file
workbook.save(outputDir + "book1.out.xlsx", SaveFormat.XLSX);

تعيين حجم الخط

تعيين حجم الخط باستخدام Font الخاصية setSize كائن.

// 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(SetFontSize.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("Hello Aspose!");
// Setting the font weight to bold
Style style = cell.getStyle();
Font font = style.getFont();
font.setSize(14);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SetFontSize_out.xls");

تعيين نوع تسطير الخط

تسطير النص باستخدام Font الخاصية setUnderline كائن. تقدم Aspose.Cells أنواع تسطير الخط المحددة مسبقًا في تعداد الـ FontUnderlineType.

أنواع تسطير الخط الوصف
NONE بدون تسطير
SINGLE تسطير أحادي
DOUBLE خط مزدوج تحته
ACCOUNTING خط أحادي تحته للمحاسبة
DOUBLE_ACCOUNTING تسطير حسابي مزدوج
DASH تحته مشطوف
DASH_DOT_DOT_HEAVY تسطير دقيقتين دايرين سميكتين
DASH_DOT_HEAVY تسطير داير سميك
DASHED_HEAVY تسطير منقوط سميك
DASH_LONG تسطير طويل منقوط
DASH_LONG_HEAVY تسطير طويل منقوط سميك
DOT_DASH تسطير نقطي داير
DOT_DOT_DASH تسطير نقطتين داير
DOTTED شَر{شَرشَرِ} Dotted Underline
DOTTED_HEAVY تسطير منقوط سميك
HEAVY شَر{شَرشَرِ} Thick Underline
WAVE شَر{شَرشَرِ} Wave Underline
WAVY_DOUBLE تسطير متموج مزدوج
WAVY_HEAVY تسطير متموج سميك
WORDS تحت الخط Underline Non-Space Characters Only
// 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(SettingFontUnderlineType.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("Hello Aspose!");
// Setting the font to be underlined
Style style = cell.getStyle();
Font font = style.getFont();
font.setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SFUnderlineType_out.xls");

تعيين لون الخط

اضبط لون الخط باستخدام كائن الخط و خاصية setColor. اختر أي لون من تعداد اللون وقم بتعيين اللون المحدد لكائن الخط باستخدام خاصية setColor.

// 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(SetFontColor.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("Hello Aspose!");
// Setting the font color to blue
Style style = cell.getStyle();
Font font = style.getFont();
font.setColor(Color.getBlue());
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SetFontColor_out.xls");

ضبط تأثير اليتيمة على النص

يمكنك تطبيق اليتيمة على النص باستخدام خاصية setStrikeout في كائن Font.

// 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(SettingStrikeOutEffect.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("Hello Aspose!");
// Setting the strike out effect on the font
Style style = cell.getStyle();
Font font = style.getFont();
font.setStrikeout(true);
cell.setStyle(style);

ضبط النص الفرعي

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

// 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(SetSubscript.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("Hello Aspose!");
// Setting subscript effect
Style style = cell.getStyle();
Font font = style.getFont();
font.setSubscript(true);
cell.setStyle(style);

ضبط النص العلوي

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

// 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(SetSubscript.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("Hello Aspose!");
// Setting superscript effect
Style style = cell.getStyle();
Font font = style.getFont();
font.setSuperscript(true);
cell.setStyle(style);

مواضيع متقدمة

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