aardio 文档

单元格背景色

单元格背景色设置示例

以下示例展示如何在 aardio 中使用 NPOI 设置 Excel 单元格的背景色:

import NPOI;

// 创建工作簿
var workbook = NPOI.XSSF.UserModel.XSSFWorkbook();

// 创建工作表
var sheet = workbook.CreateSheet("Sheet1");

// 设置单元格 A1 的纯色背景
var cell1 = sheet.CreateRow(0).CreateCell(0);
var cellstyle1 = workbook.CreateCellStyle();
cellstyle1.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
cellstyle1.FillForegroundColor = NPOI.SS.UserModel.IndexedColors.Yellow.Index;
cell1.CellStyle = cellstyle1;

// 设置单元格 A2 的双色图案背景(包含前景色和背景色)
var cell2 = sheet.CreateRow(1).CreateCell(0);
var cellstyle2 = workbook.CreateCellStyle();
cellstyle2.FillPattern = NPOI.SS.UserModel.FillPattern.BigSpots;
cellstyle2.FillForegroundColor = NPOI.SS.UserModel.IndexedColors.Blue.Index;   // Excel 中的背景色
cellstyle2.FillBackgroundColor = NPOI.SS.UserModel.IndexedColors.Yellow.Index; // Excel 中的图案色
cell2.CellStyle = cellstyle2;

// 保存为名为 "sample.xlsx" 的 Excel 文件
workbook.Write("/sample.xlsx");

代码说明:

  1. 创建工作簿:使用 NPOI.XSSF.UserModel.XSSFWorkbook() 创建新的 Excel 工作簿(XLSX 格式)

  2. 创建工作表:通过 CreateSheet() 方法创建工作表,参数为工作表名称

  3. 设置单元格样式

  4. 保存文件:使用 Write() 方法将工作簿保存为 Excel 文件

注意事项:

Markdown 格式