# 单元格背景色

## 单元格背景色设置示例

以下示例展示如何在 aardio 中使用 NPOI 设置 Excel 单元格的背景色：

```aardio
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. **设置单元格样式**：
   - 首先创建行和单元格（注意行列索引从0开始）
   - 通过 `workbook.CreateCellStyle()` 创建单元格样式对象
   - 设置填充模式（`FillPattern`）和颜色（`FillForegroundColor`/`FillBackgroundColor`）
   - 将样式应用到单元格

4. **保存文件**：使用 `Write()` 方法将工作簿保存为 Excel 文件

### 注意事项：

- NPOI 中的索引从0开始，与 aardio 中数组从1开始的惯例不同
- 颜色使用 `IndexedColors` 枚举类中预定义的颜色索引值
- 文件路径建议使用绝对路径，或以"/"开头表示应用程序根目录