其他分享
首页 > 其他分享> > NPOI2.2.0.0实例详解(五)—设置EXCEL单元格背景与图案

NPOI2.2.0.0实例详解(五)—设置EXCEL单元格背景与图案

作者:互联网

原文链接:https://my.oschina.net/u/1778848/blog/542242
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
using NPOI.SS.Util;
using System.Drawing;
using NPOI.HSSF.Util;
using NPOI.XSSF.Util;
namespace NPOI
{
    class Program4
    {
        static void Main(string[] args)
        {
            //说明:设置单元格背景与图案

            //1.创建EXCEL中的Workbook         
            IWorkbook myworkbook = new XSSFWorkbook();

            //2.创建Workbook中的Sheet        
            ISheet mysheet = myworkbook.CreateSheet("sheet1");      
            mysheet.DefaultRowHeight = 30 * 20;
            mysheet.SetColumnWidth(1, 30 * 256);

            //3.创建Row中的Cell并赋值
            IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue(""); row0.CreateCell(1).SetCellValue("NoFill");
            IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(""); row1.CreateCell(1).SetCellValue("SolidForeground");
            IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue(""); row2.CreateCell(1).SetCellValue("FineDots");
            IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue(""); row3.CreateCell(1).SetCellValue("AltBars");
            IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue(""); row4.CreateCell(1).SetCellValue("SparseDots");
            IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue(""); row5.CreateCell(1).SetCellValue("ThickHorizontalBands");
            IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue(""); row6.CreateCell(1).SetCellValue("ThickVerticalBands");
            IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue(""); row7.CreateCell(1).SetCellValue("ThickBackwardDiagonals");
            IRow row8 = mysheet.CreateRow(8); row8.CreateCell(0).SetCellValue(""); row8.CreateCell(1).SetCellValue("ThickForwardDiagonals");
            IRow row9 = mysheet.CreateRow(9); row9.CreateCell(0).SetCellValue(""); row9.CreateCell(1).SetCellValue("BigSpots");
            IRow row10 = mysheet.CreateRow(10); row10.CreateCell(0).SetCellValue(""); row10.CreateCell(1).SetCellValue("Bricks");
            IRow row11 = mysheet.CreateRow(11); row11.CreateCell(0).SetCellValue(""); row11.CreateCell(1).SetCellValue("ThinHorizontalBands");
            IRow row12 = mysheet.CreateRow(12); row12.CreateCell(0).SetCellValue(""); row12.CreateCell(1).SetCellValue("ThinVerticalBands");
            IRow row13 = mysheet.CreateRow(13); row13.CreateCell(0).SetCellValue(""); row13.CreateCell(1).SetCellValue("ThinBackwardDiagonals");
            IRow row14 = mysheet.CreateRow(14); row14.CreateCell(0).SetCellValue(""); row14.CreateCell(1).SetCellValue("ThinForwardDiagonals");
            IRow row15 = mysheet.CreateRow(15); row15.CreateCell(0).SetCellValue(""); row15.CreateCell(1).SetCellValue("Squares");
            IRow row16 = mysheet.CreateRow(16); row16.CreateCell(0).SetCellValue(""); row16.CreateCell(1).SetCellValue("Diamonds");
            IRow row17 = mysheet.CreateRow(17); row17.CreateCell(0).SetCellValue(""); row17.CreateCell(1).SetCellValue("LessDots");
            IRow row18 = mysheet.CreateRow(18); row18.CreateCell(0).SetCellValue(""); row18.CreateCell(1).SetCellValue("LeastDots");

            //【Tips】
            // 1.ForegroundColor(默认黑色)【前景颜色】BackgroundColor(默认为前景颜色的反色)【背景颜色】Pattern(必须指定,默认NoFill)【填充的图案】
            // 2.演示中使用 【前景颜色】黑色 【背景颜色】白色

            //4.创建CellStyle并应用于单元格  
            ICellStyle style0 = myworkbook.CreateCellStyle();  style0.FillBackgroundColor = IndexedColors.White.Index;
            style0.FillForegroundColor = IndexedColors.Black.Index; style0.FillPattern = FillPattern.NoFill;               
            row0.GetCell(0).CellStyle = style0;
            ICellStyle style1 = myworkbook.CreateCellStyle();  style1.FillBackgroundColor = IndexedColors.White.Index;
            style1.FillForegroundColor = IndexedColors.Black.Index; style1.FillPattern = FillPattern.SolidForeground;
            row1.GetCell(0).CellStyle = style1;
            ICellStyle style2 = myworkbook.CreateCellStyle(); style2.FillBackgroundColor = IndexedColors.White.Index;
            style2.FillForegroundColor = IndexedColors.Black.Index; style2.FillPattern = FillPattern.FineDots;
            row2.GetCell(0).CellStyle = style2;
            ICellStyle style3 = myworkbook.CreateCellStyle(); style3.FillBackgroundColor = IndexedColors.White.Index;
            style3.FillForegroundColor = IndexedColors.Black.Index; style3.FillPattern = FillPattern.AltBars;
            row3.GetCell(0).CellStyle = style3;
            ICellStyle style4 = myworkbook.CreateCellStyle(); style4.FillBackgroundColor = IndexedColors.White.Index;
            style4.FillForegroundColor = IndexedColors.Black.Index; style4.FillPattern = FillPattern.SparseDots;
            row4.GetCell(0).CellStyle = style4;
            ICellStyle style5 = myworkbook.CreateCellStyle(); style5.FillBackgroundColor = IndexedColors.White.Index;
            style5.FillForegroundColor = IndexedColors.Black.Index; style5.FillPattern = FillPattern.ThickHorizontalBands;
            row5.GetCell(0).CellStyle = style5;
            ICellStyle style6 = myworkbook.CreateCellStyle(); style6.FillBackgroundColor = IndexedColors.White.Index;
            style6.FillForegroundColor = IndexedColors.Black.Index; style6.FillPattern = FillPattern.ThickVerticalBands;
            row6.GetCell(0).CellStyle = style6;
            ICellStyle style7 = myworkbook.CreateCellStyle(); style7.FillBackgroundColor = IndexedColors.White.Index;
            style7.FillForegroundColor = IndexedColors.Black.Index; style7.FillPattern = FillPattern.ThickBackwardDiagonals;
            row7.GetCell(0).CellStyle = style7;
            ICellStyle style8 = myworkbook.CreateCellStyle(); style8.FillBackgroundColor = IndexedColors.White.Index;
            style8.FillForegroundColor = IndexedColors.Black.Index; style8.FillPattern = FillPattern.ThickForwardDiagonals;
            row8.GetCell(0).CellStyle = style8;
            ICellStyle style9 = myworkbook.CreateCellStyle(); style9.FillBackgroundColor = IndexedColors.White.Index;
            style9.FillForegroundColor = IndexedColors.Black.Index; style9.FillPattern = FillPattern.BigSpots;
            row9.GetCell(0).CellStyle = style9;
            ICellStyle style10 = myworkbook.CreateCellStyle(); style10.FillBackgroundColor = IndexedColors.White.Index;
            style10.FillForegroundColor = IndexedColors.Black.Index; style10.FillPattern = FillPattern.Bricks;
            row10.GetCell(0).CellStyle = style10;
            ICellStyle style11 = myworkbook.CreateCellStyle(); style11.FillBackgroundColor = IndexedColors.White.Index;
            style11.FillForegroundColor = IndexedColors.Black.Index; style11.FillPattern = FillPattern.ThinHorizontalBands;
            row11.GetCell(0).CellStyle = style11;
            ICellStyle style12 = myworkbook.CreateCellStyle(); style12.FillBackgroundColor = IndexedColors.White.Index;
            style12.FillForegroundColor = IndexedColors.Black.Index; style12.FillPattern = FillPattern.ThinVerticalBands;
            row12.GetCell(0).CellStyle = style12;
            ICellStyle style13 = myworkbook.CreateCellStyle(); style13.FillBackgroundColor = IndexedColors.White.Index;
            style13.FillForegroundColor = IndexedColors.Black.Index; style13.FillPattern = FillPattern.ThinBackwardDiagonals;
            row13.GetCell(0).CellStyle = style13;
            ICellStyle style14 = myworkbook.CreateCellStyle(); style14.FillBackgroundColor = IndexedColors.White.Index;
            style14.FillForegroundColor = IndexedColors.Black.Index; style14.FillPattern = FillPattern.ThinForwardDiagonals;
            row14.GetCell(0).CellStyle = style14;
            ICellStyle style15 = myworkbook.CreateCellStyle(); style15.FillBackgroundColor = IndexedColors.White.Index;
            style15.FillForegroundColor = IndexedColors.Black.Index; style15.FillPattern = FillPattern.Squares;
            row15.GetCell(0).CellStyle = style15;
            ICellStyle style16 = myworkbook.CreateCellStyle(); style16.FillBackgroundColor = IndexedColors.White.Index;
            style16.FillForegroundColor = IndexedColors.Black.Index; style16.FillPattern = FillPattern.Diamonds;
            row16.GetCell(0).CellStyle = style16;
            ICellStyle style17 = myworkbook.CreateCellStyle(); style17.FillBackgroundColor = IndexedColors.White.Index;
            style17.FillForegroundColor = IndexedColors.Black.Index; style17.FillPattern = FillPattern.LessDots;
            row17.GetCell(0).CellStyle = style17;
            ICellStyle style18 = myworkbook.CreateCellStyle(); style18.FillBackgroundColor = IndexedColors.White.Index;
            style18.FillForegroundColor = IndexedColors.Black.Index; style18.FillPattern = FillPattern.LeastDots;
            row18.GetCell(0).CellStyle = style18;

            //5.保存       
            FileStream file = new FileStream(@"E:\myworkbook4.xlsx", FileMode.Create);
            myworkbook.Write(file);
            file.Close();
        }
    }
}

运行后,效果如下图所示【演示了不同背景图案】


转载于:https://my.oschina.net/u/1778848/blog/542242

标签:Index,NPOI2.2,0.0,单元格,IndexedColors,CreateCell,FillPattern,myworkbook,SetCellVal
来源: https://blog.csdn.net/choujing2591/article/details/100809117