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

来源:互联网 发布:淘宝购物积分怎么领取 编辑:程序博客网 时间:2024/05/16 08:00

使用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("第二页第二行");//savestring savePath = "XLSXTest.xlsx";FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);wb.Write(fs);fs.Close();Debug.Log("报告路径:" + savePath);}}

效果图:


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

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

0 0
原创粉丝点击