首页 .Net .NET Core(C#)使用NPOI改变设置Excel(.xls,.xlsx)单元格背景颜色

.NET Core(C#)使用NPOI改变设置Excel(.xls,.xlsx)单元格背景颜色

1、安装引用DotNetCore.NPOI

1)使用Nuget界面管理器

搜索"DotNetCore.NPOI",在列表中找到它,点击"安装"

相关文档VS(Visual Studio)中Nuget的使用

2)使用Package Manager命令安装

PM> Install-Package DotNetCore.NPOI

3)使用.NET CLI命令安装

> dotnet add TodoApi.csproj package DotNetCore.NPOI

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

命名空间:

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

示例代码:

IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.GetSheet("Sheet1");
IRow row = sheet.CreateRow(0);
//styling
IFont boldFont = workbook.CreateFont();
boldFont.Boldweight = (short)FontBoldWeight.Bold;
ICellStyle boldStyle = workbook.CreateCellStyle();
boldStyle.SetFont(boldFont);
boldStyle.BorderBottom = BorderStyle.Medium;
boldStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
boldStyle.FillPattern = FillPattern.SolidForeground;
ICell cell = row.CreateCell(5);
cell.SetCellValue("cjavapy");
cell.CellStyle = boldStyle;

NPOI源代码:https://github.com/dotnetcore/NPOI

特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。