使用NPOI设置Excel表的单元格背景颜色

使用NPOI设置Excel单元格背景颜色时,应该设置FillForegroundColor属性,而且还要设置FillPattern才行。

代码如下:

style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PINK.index;

style.FillPattern = FillPatternType.SOLID_FOREGROUND;


简单代码示例:

using UnityEngine;
using System.Collections;
using UnityEditor;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using System.Collections.Generic;
using System.Linq;
using System.IO;
public class WorkBook 
{
	[MenuItem("H3D/XLSX文件测试")]
	static void CreateExcelFile()
	{
		// 生成简报
		IWorkbook wb = new HSSFWorkbook();
		var sheet = wb.CreateSheet("第一页");
		int currentRow = 0;

		ICellStyle s = wb.CreateCellStyle ();
		s.FillForegroundColor = HSSFColor.Pink.Index;
		s.FillPattern = FillPattern.SolidForeground;

		// 简报开始
		var row = sheet.CreateRow(currentRow++);
		row.CreateCell(0).SetCellValue("第一页第一行");
		row.GetCell(0).CellStyle = s;

		row = sheet.CreateRow(currentRow++);
		row.CreateCell(0).SetCellValue("第一页第二行");



		var sheet2 = wb.CreateSheet("第二页");
		int currentRow2 = 0;
		// 简报开始
		var row2 = sheet2.CreateRow(currentRow2++);
		row2.CreateCell(0).SetCellValue("第二页第一行");

		row2 = sheet2.CreateRow(currentRow2++);
		row2.CreateCell(0).SetCellValue("第二页第二行");


		//save
		string savePath = "XLSXTest.xlsx";
		FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
		wb.Write(fs);
		fs.Close();
		Debug.Log("报告路径:" + savePath);
	}

}

效果图:


以上引用需要使用:NPOI库;

以上代码和NPOI库需要放入Editor目录下;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值