Hantera fontinställningar

Konfigurera fontinställningar

Aspose.Cells tillhandahåller en klass Workbook som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en WorksheetCollection som tillåter åtkomst till varje arbetsblad i en Excelfil. Ett arbetsblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en Cells samling. Varje objekt i Cells samlingen representerar ett objekt av klassen Cell.

Aspose.Cells tillhandahåller Cell klassens setStyle metod, används för att ställa in en cells formatering. Dessutom tillhandahåller objektet av klassen Style egenskaper för att konfigurera fontinställningar.

Den här artikeln visar hur du:

Ange fontnamn

Tillämpa en specifik font på text i celler med hjälp av Font objektets setName egenskap.

// 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");

Ange fontstil till Fetstil

Ange texten till fetstil genom att ställa Font objektets setBold egenskap till 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);

Inställning av fontstorlek

Ange fontstorleken med Font objektets setSize egenskap.

// 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");

Inställning av font underlinjetyp

Understryk text med Font objektets setUnderline egenskap. Aspose.Cells erbjuder olika fördefinierade font underlinjetyper i FontUnderlineType uppräkning.

Font Underline Types Beskrivning
NONE Ingen understrykning
SINGLE Enkel understrykning
DOUBLE Dubbel understrykning
ACCOUNTING Enkel bokföringsunderstrykning
DOUBLE_ACCOUNTING Dubbel bokföringsunderstrykning
DASH Streckad understrykning
DASH_DOT_DOT_HEAVY Tjock streckad-punktpunkt understrykning
DASH_DOT_HEAVY Tjock streckad-punktpunkt understrykning
DASHED_HEAVY Tjock streckad understrykning
DASH_LONG Lång streckad understrykning
DASH_LONG_HEAVY Tjock lång streckad understrykning
DOT_DASH Streck-punkt understrykning
DOT_DOT_DASH Streck-punkt-punkt understrykning
DOTTED Prickad understrykning
DOTTED_HEAVY Tjock prickad understrykning
HEAVY Tjock Underline
WAVE Våg Underline
WAVY_DOUBLE Dubbel Våg Underline
WAVY_HEAVY Kraftig Våg Underline
WORDS Underline Endast Tecken Utan Mellanslag
// 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");

Sätt Typsnittsfärg

Ställ in typsnittsfärg med Font-objektets setColor-egenskap. Välj en färg från Color-uppräkningen och tilldela den valda färgen till Font-objektets 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");

Ställ in Strikeout-effekt på text

Stryk över text med Font-objektets setStrikeout-egenskap.

// 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);

Ställa in subscript

Gör text upphöjd genom att använda Font-objektets setSubscript-egenskap.

// 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);

Ställa in superscript

Använda superscript på text med Font-objektets setSuperscript-egenskap.

// 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);

Fortsatta ämnen