单元格格式
向单元格添加边框
Microsoft Excel允许用户通过添加边框来格式化单元格。
Microsoft Excel中的边框设置
边框的类型取决于添加的位置。例如,顶部边框是添加到单元格顶部位置的边框。用户还可以修改边框的线条样式和颜色。
使用Aspose.Cells,开发人员可以以与Microsoft Excel中相同灵活的方式添加边框并自定义其外观。
向单元格添加边框
Aspose.Cells提供了代表Microsoft Excel文件的Workbook类。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合。Cells集合中的每个项代表Cell类的对象。
Aspose.Cells 提供 setStyle 方法在 Cell 类中,用于设置单元格的样式。同样, Style 类的对象被用来配置字体等属性。
向单元格添加边框
可以用 setBorder 方法为单元格添加边框,把边框类型作为参数传入。所有边框类型都在 BorderType 枚举中预定义。
边框类型 | 描述 |
---|---|
BOTTOM_BORDER | 底部边框线 |
DIAGONAL_DOWN | 从左上到右下的对角线 |
DIAGONAL_UP | 从左下到右上的对角线 |
LEFT_BORDER | 左边框线 |
RIGHT_BORDER | 右边框线 |
TOP_BORDER | 上边框线 |
HORIZONTAL | 仅用于动态样式,例如条件格式设置。 |
VERTICAL | 仅用于动态样式,例如条件格式设置。 |
要设置线条颜色,可使用 Color 枚举选择颜色,并传入 Style 类对象的 setBorder 方法的 Color 参数。线条样式在 CellBorderType 枚举中预定义。 |
线条样式 | 描述 |
---|---|
DASH_DOT | 细点划线 |
DASH_DOT_DOT | 细点点划线 |
DASHED | 表示虚线 |
DOTTED | 表示点线 |
DOUBLE | 表示双线 |
HAIR | 表示发际线 |
MEDIUM_DASH_DOT | 代表中等破折点线 |
MEDIUM_DASH_DOT_DOT | 代表中等破折点点线 |
MEDIUM_DASHED | 代表中等虚线 |
NONE | 表示无线条 |
MEDIUM | 表示中等线 |
SLANTED_DASH_DOT | 代表倾斜的中等破折点线 |
THICK | 表示粗线 |
THIN | 表示细线 |
选择以上线条样式之一,然后将其分配给Style对象的setBorder方法。 |
执行以下代码时生成下面的输出。
应用于单元格四周的边框
// 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(AddingBordersToCells.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(); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = cells.get("A1"); | |
// Adding some value to the "A1" cell | |
cell.setValue("Visit Aspose!"); | |
Style style = cell.getStyle(); | |
// Setting the line of the top border | |
style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Setting the line of the bottom border | |
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Setting the line of the left border | |
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Setting the line of the right border | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Saving the modified style to the "A1" cell. | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(dataDir + "ABToCells_out.xls"); |
向单元格范围添加边框
可以为单元格区域添加边框,而不仅仅是单个单元格。首先,通过调用Cells集合的createRange方法创建一个单元格区域,该方法接受以下参数:
- 第一行,范围的第一行。
- 第一列,范围的第一列。
- 行数,范围内的行数。
- 列数,范围内的列数。
createRange方法返回一个Range对象,包含指定的区域。该Range对象提供一个setOutlineBorders方法,接受以下参数:
- CellBorderType,边框线样式,从CellBorderType枚举中选择。
- Color,边框线颜色,从Color枚举中选择。
执行以下代码时生成下面的输出。
应用于单元格范围的边框
// 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(AddingBordersToCells.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object Obtaining the reference of the newly added worksheet | |
int sheetIndex = workbook.getWorksheets().add(); | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.setValue("Hello World From Aspose"); | |
// Creating a range of cells starting from "A1" cell to 3rd column in a | |
// row | |
Range range = worksheet.getCells().createRange(0, 0, 1, 2); | |
range.setName("MyRange"); | |
// Adding a thick outline border with the blue line | |
range.setOutlineBorders(CellBorderType.THICK, Color.getBlue()); | |
// Saving the Excel file | |
workbook.save(dataDir + "ABToRange_out.xls"); |
颜色和调色板
调色板是在创建图像时可用的颜色数量。在演示文稿中使用标准调色板可以让用户创建一致的外观。每个Microsoft Excel(97-2003)文件都有一个包含可应用于单元格、字体、网格线、图形对象、填充和图表中的线条的56种颜色的调色板。
Microsoft Excel中的调色板设置
通过Aspose.Cells不仅可以使用现有颜色,还可以使用自定义颜色。在使用自定义颜色之前,将其添加到调色板中。本主题解释了如何向调色板中添加自定义颜色。
向调色板中添加自定义颜色
Aspose.Cells还支持56种颜色的调色板。上面显示了标准颜色调色板。如果要使用在调色板中未定义的自定义颜色,则需要在使用之前将该颜色添加到调色板中。
Aspose.Cells提供一个类Workbook,代表一个Microsoft Excel文件。该类提供changePalette方法,接受以下参数以添加自定义颜色以修改调色板:
- 自定义颜色,要添加到调色板中的自定义颜色。
- 索引,将被自定义颜色替换的颜色的索引。应该在0-55之间。
下面的示例在应用于字体之前将一个自定义颜色添加到调色板中。
// 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(ColorsAndPalette.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding custom color to the palette at 55th index | |
Color color = Color.fromArgb(212, 213, 0); | |
workbook.changePalette(color, 55); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
int sheetIndex = workbook.getWorksheets().add(); | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.setValue("Hello Aspose!"); | |
// Setting the custom color to the font | |
Style style = cell.getStyle(); | |
Font font = style.getFont(); | |
font.setColor(color); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(dataDir + "ColorsAndPalette_out.xls"); |
颜色和背景图案
Microsoft Excel可以设置单元格的前景(轮廓)和背景(填充)颜色以及背景图案,如下所示。
在Microsoft Excel中设置颜色和背景图案
Aspose.Cells还以灵活的方式支持这些功能。在本主题中,我们将学习如何使用Aspose.Cells来使用这些功能。
设置颜色和背景图案
Aspose.Cells 提供了一个类,Workbook,它表示 Microsoft Excel 文件。 Workbook 类包含一个WorksheetCollection,允许访问 Excel 文件中的每个工作表。 工作表由Worksheet类表示。 Worksheet 类提供一个Cells集合。 Cells集合中的每个项都表示Cell类的对象。
Aspose.Cells提供在Cell类中的setStyle方法,用于设置单元格格式。还可以使用Style类的对象配置字体设置。
setForegroundColor属性设置单元格的底纹颜色。
setPattern属性指定单元格的前景或背景颜色所使用的背景图案。Aspose.Cells提供BackgroundType枚举,其中包含一组预定义的背景图案类型。
图案类型 | 描述 |
---|---|
DIAGONAL_CROSSHATCH | 代表斜对角交叉图案 |
DIAGONAL_STRIPE | 代表斜向条纹图案 |
GRAY_6 | 代表6.25%灰色图案 |
GRAY_12 | 代表12.5%灰色图案 |
GRAY_25 | 代表25%灰色图案 |
GRAY_50 | 代表50%灰色图案 |
GRAY_75 | 代表75%灰色图案 |
HORIZONTAL_STRIPE | 代表水平条纹图案 |
NONE | 表示无背景 |
REVERSE_DIAGONAL_STRIPE | 代表反斜线条纹图案 |
SOLID | 代表着固体图案 |
THICK_DIAGONAL_CROSSHATCH | 代表粗斜线交叉图案 |
THIN_DIAGONAL_CROSSHATCH | 代表细斜线交叉图案 |
THIN_DIAGONAL_STRIPE | 代表细斜线条纹图案 |
THIN_HORIZONTAL_CROSSHATCH | 代表细水平交叉图案 |
THIN_HORIZONTAL_STRIPE | 代表细水平条纹图案 |
THIN_REVERSE_DIAGONAL_STRIPE | 代表细反斜线条纹图案 |
THIN_VERTICAL_STRIPE | 代表细垂直条纹图案 |
VERTICAL_STRIPE | 代表垂直条纹图案 |
在下面的示例中,A1单元格的前景色已设置,但A2配置为具有前景色和背景色的垂直条纹背景模式。 |
执行代码时会生成以下输出。
在具有背景模式的单元格上应用前景色和背景色
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(ColorsAndBackground.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(); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = cells.get("A1"); | |
Style style = cell.getStyle(); | |
// Setting the foreground color to yellow | |
style.setBackgroundColor(Color.getYellow()); | |
// Setting the background pattern to vertical stripe | |
style.setPattern(BackgroundType.VERTICAL_STRIPE); | |
// Saving the modified style to the "A1" cell. | |
cell.setStyle(style); | |
// Accessing the "A2" cell from the worksheet | |
cell = cells.get("A2"); | |
style = cell.getStyle(); | |
// Setting the foreground color to blue | |
style.setBackgroundColor(Color.getBlue()); | |
// Setting the background color to yellow | |
style.setForegroundColor(Color.getYellow()); | |
// Setting the background pattern to vertical stripe | |
style.setPattern(BackgroundType.VERTICAL_STRIPE); | |
// Saving the modified style to the "A2" cell. | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(dataDir + "ColorsAndBackground_out.xls"); |
重要知识
- 要设置单元格的前景色或背景色,请使用Style对象的ForegroundColor或BackgroundColor属性。只有在配置了Style对象的Pattern属性后,这两个属性才会生效。
- ForegroundColor属性设置单元格的阴影颜色。 Pattern属性指定用于前景色或背景色的背景图案的类型。Aspose.Cells提供了一个包含一组预定义背景图案类型的枚举,BackgroundType。
- 如果从BackgroundType枚举中选择BackgroundType.NONE值,将不会应用前景色。 同样,如果从BackgroundType枚举中选择BackgroundType.NONE或BackgroundType.SOLID值,将不会应用背景色。 -当获取单元格的阴影/填充颜色时,如果Style.Pattern为BackgroundType.NONE,Style.ForegroundColor将返回Color.Empty。
在单元格中格式化所选字符
处理字体设置解释了如何格式化单元格,但只是如何格式化整个单元格的内容。 如果您只想格式化所选字符该怎么办?
Aspose.Cells 支持此功能。本主题解释了如何使用此功能。
格式化所选字符
Aspose.Cells 提供了一个类,Workbook,它表示 Microsoft Excel 文件。 Workbook 类包含一个WorksheetCollection,允许访问 Excel 文件中的每个工作表。 工作表由Worksheet类表示。 Worksheet 类提供一个Cells集合。 Cells集合中的每个项都表示Cell类的对象。
Cell类提供带有参数的characters方法,用于选择单元格中的字符范围:
- 起始索引,从中开始选择的字符的索引。
- 字符数,要选择的字符数。
在输出文件中,A1单元格中,“Visit”一词使用默认字体格式,但“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.getCharacters
方法用于访问文本的相应部分,然后可以使用Cell.setCharacters
进行修改,而get
方法返回一个FontSetting对象数组,可以操控设置字体名称、字体颜色、加粗等各种属性,set
方法用于应用更改。