本指南分享了在 Java 中为单元格使用内置 **Excel 主题的详细信息。它包含开发所需的资源、编写应用程序的步骤列表以及开发程序以在 Java 中设置 Excel 电子表格主题的代码示例。在支持 Java 和各种语言(例如 C#、Java、Python、Node.js 和 C++)的平台中使用此功能。
在Java中应用Excel主题的步骤
- 设置环境使用Aspose.Cells for Java为Excel设置主题
- 创建或加载包含一些数据的 Excel file 并从中访问单元格
- 使用 ThemeColor 对象获取单元格样式并设置其前景主题颜色
- 获取单元格的字体并设置其主题颜色
- 设置单元格的新样式
- 保存工作簿
上述步骤描述了在Java中使用*Excel工作簿主题的过程。通过从工作簿中的工作表访问单元格样式并设置前景主题颜色和图案来开始该过程。在下一步中,在保存工作簿之前设置字体的主题颜色并将样式设置为单元格。
在 Java 中为 Excel 应用主题的代码
import com.aspose.cells.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Apply color theme in Excel in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Instantiate a Workbook. | |
Workbook workbook = new Workbook(); | |
// Get cells collection | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Get the A3 cell | |
Cell c = cells.get("A3"); | |
// Get the style | |
Style s = c.getStyle(); | |
// Set foreground theme color | |
s.setForegroundThemeColor(new ThemeColor(ThemeColorType.ACCENT_4, 0.5)); | |
// Set the pattern | |
s.setPattern(BackgroundType.DIAGONAL_STRIPE); | |
// Get the font | |
Font f = s.getFont(); | |
// Set the theme color | |
f.setThemeColor(new ThemeColor(ThemeColorType.ACCENT_2, 0.4)); | |
// Apply style. | |
c.setStyle(s); | |
// Put a value. | |
c.putValue("Testing1"); | |
// Save the excel file. | |
workbook.save("output.xlsx"); | |
System.out.println("Done"); | |
} | |
} |
此代码片段演示了在 Java 中使用默认 *Microsoft Excel 主题的过程。它使用 getStyle() 方法访问单元格的样式,并通过调用 setForegroundThemeColor() 方法设置主题颜色。在设置工作表中单元格、单元格区域或整个单元格集合的样式属性和字体时,使用 ThemeColorType 中的各种选项。
本文指导我们设置单元格的主题颜色。要应用条件格式,请参阅有关 使用 Java 在 Excel 中应用条件格式 的文章。