HSSF NPOI 颜色

using System;
using System.IO;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace App1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = Path.Combine(Application.StartupPath, "Color Report (Beta).xls");
            HSSFWorkbook book = new HSSFWorkbook();

            ISheet sheet = book.CreateSheet("Color Detail");

            ICellStyle style = book.CreateCellStyle();
            style.FillPattern = FillPattern.SolidForeground;
            style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Aqua.Index;
            sheet.CreateRow(0).CreateCell(0).CellStyle = style;

            ICellStyle style1 = book.CreateCellStyle();
            style1.CloneStyleFrom(style);
            style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Automatic.Index;
            sheet.CreateRow(1).CreateCell(0).CellStyle = style1;

            ICellStyle style2 = book.CreateCellStyle();
            style2.CloneStyleFrom(style);
            style2.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
            sheet.CreateRow(2).CreateCell(0).CellStyle = style2;

            ICellStyle style3 = book.CreateCellStyle();
            style3.CloneStyleFrom(style);
            style3.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
            sheet.CreateRow(3).CreateCell(0).CellStyle = style3;

            ICellStyle style4 = book.CreateCellStyle();
            style4.CloneStyleFrom(style);
            style4.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BlueGrey.Index;
            sheet.CreateRow(4).CreateCell(0).CellStyle = style4;

            ICellStyle style5 = book.CreateCellStyle();
            style5.CloneStyleFrom(style);
            style5.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BrightGreen.Index;
            sheet.CreateRow(5).CreateCell(0).CellStyle = style5;

            ICellStyle style6 = book.CreateCellStyle();
            style6.CloneStyleFrom(style);
            style6.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Brown.Index;
            sheet.CreateRow(6).CreateCell(0).CellStyle = style6;

            ICellStyle style7 = book.CreateCellStyle();
            style7.CloneStyleFrom(style);
            style7.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Coral.Index;
            sheet.CreateRow(7).CreateCell(0).CellStyle = style7;

            ICellStyle style8 = book.CreateCellStyle();
            style8.CloneStyleFrom(style);
            style8.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            sheet.CreateRow(8).CreateCell(0).CellStyle = style8;

            ICellStyle style9 = book.CreateCellStyle();
            style9.CloneStyleFrom(style);
            style9.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkBlue.Index;
            sheet.CreateRow(9).CreateCell(0).CellStyle = style9;

            ICellStyle style10 = book.CreateCellStyle();
            style10.CloneStyleFrom(style);
            style10.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkGreen.Index;
            sheet.CreateRow(10).CreateCell(0).CellStyle = style10;

            ICellStyle style11 = book.CreateCellStyle();
            style11.CloneStyleFrom(style);
            style11.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkRed.Index;
            sheet.CreateRow(11).CreateCell(0).CellStyle = style11;

            ICellStyle style12 = book.CreateCellStyle();
            style12.CloneStyleFrom(style);
            style12.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkTeal.Index;
            sheet.CreateRow(12).CreateCell(0).CellStyle = style12;

            ICellStyle style13 = book.CreateCellStyle();
            style13.CloneStyleFrom(style);
            style13.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.DarkYellow.Index;
            sheet.CreateRow(13).CreateCell(0).CellStyle = style13;

            ICellStyle style14 = book.CreateCellStyle();
            style14.CloneStyleFrom(style);
            style14.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Gold.Index;
            sheet.CreateRow(14).CreateCell(0).CellStyle = style14;

            ICellStyle style15 = book.CreateCellStyle();
            style15.CloneStyleFrom(style);
            style15.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Green.Index;
            sheet.CreateRow(15).CreateCell(0).CellStyle = style15;

            ICellStyle style16 = book.CreateCellStyle();
            style16.CloneStyleFrom(style);
            style16.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
            sheet.CreateRow(16).CreateCell(0).CellStyle = style16;

            ICellStyle style17 = book.CreateCellStyle();
            style17.CloneStyleFrom(style);
            style17.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey40Percent.Index;
            sheet.CreateRow(17).CreateCell(0).CellStyle = style17;

            ICellStyle style18 = book.CreateCellStyle();
            style18.CloneStyleFrom(style);
            style18.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey50Percent.Index;
            sheet.CreateRow(18).CreateCell(0).CellStyle = style18;

            ICellStyle style19 = book.CreateCellStyle();
            style19.CloneStyleFrom(style);
            style19.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey80Percent.Index;
            sheet.CreateRow(19).CreateCell(0).CellStyle = style19;

            ICellStyle style20 = book.CreateCellStyle();
            style20.CloneStyleFrom(style);
            style20.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Indigo.Index;
            sheet.CreateRow(20).CreateCell(0).CellStyle = style20;

            ICellStyle style21 = book.CreateCellStyle();
            style21.CloneStyleFrom(style);
            style21.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lavender.Index;
            sheet.CreateRow(21).CreateCell(0).CellStyle = style21;

            ICellStyle style22 = book.CreateCellStyle();
            style22.CloneStyleFrom(style);
            style22.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            sheet.CreateRow(22).CreateCell(0).CellStyle = style22;

            ICellStyle style23 = book.CreateCellStyle();
            style23.CloneStyleFrom(style);
            style23.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightBlue.Index;
            sheet.CreateRow(23).CreateCell(0).CellStyle = style23;

            ICellStyle style24 = book.CreateCellStyle();
            style24.CloneStyleFrom(style);
            style24.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightCornflowerBlue.Index;
            sheet.CreateRow(24).CreateCell(0).CellStyle = style24;

            ICellStyle style25 = book.CreateCellStyle();
            style25.CloneStyleFrom(style);
            style25.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            sheet.CreateRow(25).CreateCell(0).CellStyle = style25;

            ICellStyle style26 = book.CreateCellStyle();
            style26.CloneStyleFrom(style);
            style26.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightOrange.Index;
            sheet.CreateRow(26).CreateCell(0).CellStyle = style26;

            ICellStyle style27 = book.CreateCellStyle();
            style27.CloneStyleFrom(style);
            style27.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
            sheet.CreateRow(27).CreateCell(0).CellStyle = style27;

            ICellStyle style28 = book.CreateCellStyle();
            style28.CloneStyleFrom(style);
            style28.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
            sheet.CreateRow(28).CreateCell(0).CellStyle = style28;

            ICellStyle style29 = book.CreateCellStyle();
            style29.CloneStyleFrom(style);
            style29.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            sheet.CreateRow(29).CreateCell(0).CellStyle = style29;

            ICellStyle style30 = book.CreateCellStyle();
            style30.CloneStyleFrom(style);
            style30.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Maroon.Index;
            sheet.CreateRow(30).CreateCell(0).CellStyle = style30;

            ICellStyle style31 = book.CreateCellStyle();
            style31.CloneStyleFrom(style);
            style31.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.OliveGreen.Index;
            sheet.CreateRow(31).CreateCell(0).CellStyle = style31;

            ICellStyle style32 = book.CreateCellStyle();
            style32.CloneStyleFrom(style);
            style32.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Orange.Index;
            sheet.CreateRow(32).CreateCell(0).CellStyle = style32;

            ICellStyle style33 = book.CreateCellStyle();
            style33.CloneStyleFrom(style);
            style33.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Orchid.Index;
            sheet.CreateRow(33).CreateCell(0).CellStyle = style33;

            ICellStyle style34 = book.CreateCellStyle();
            style34.CloneStyleFrom(style);
            style34.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
            sheet.CreateRow(34).CreateCell(0).CellStyle = style34;

            ICellStyle style35 = book.CreateCellStyle();
            style35.CloneStyleFrom(style);
            style35.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Pink.Index;
            sheet.CreateRow(35).CreateCell(0).CellStyle = style35;

            ICellStyle style36= book.CreateCellStyle();
            style36.CloneStyleFrom(style);
            style36.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Plum.Index;
            sheet.CreateRow(36).CreateCell(0).CellStyle = style36;

            ICellStyle style37 = book.CreateCellStyle();
            style37.CloneStyleFrom(style);
            style37.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
            sheet.CreateRow(37).CreateCell(0).CellStyle = style37;

            ICellStyle style38 = book.CreateCellStyle();
            style38.CloneStyleFrom(style);
            style38.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Rose.Index;
            sheet.CreateRow(38).CreateCell(0).CellStyle = style38;

            ICellStyle style39 = book.CreateCellStyle();
            style39.CloneStyleFrom(style);
            style39.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RoyalBlue.Index;
            sheet.CreateRow(39).CreateCell(0).CellStyle = style39;

            ICellStyle style40 = book.CreateCellStyle();
            style40.CloneStyleFrom(style);
            style40.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SeaGreen.Index;
            sheet.CreateRow(40).CreateCell(0).CellStyle = style40;

            ICellStyle style41 = book.CreateCellStyle();
            style41.CloneStyleFrom(style);
            style41.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
            sheet.CreateRow(41).CreateCell(0).CellStyle = style41;

            ICellStyle style42 = book.CreateCellStyle();
            style42.CloneStyleFrom(style);
            style42.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Tan.Index;
            sheet.CreateRow(42).CreateCell(0).CellStyle = style42;

            ICellStyle style43 = book.CreateCellStyle();
            style43.CloneStyleFrom(style);
            style43.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Teal.Index;
            sheet.CreateRow(43).CreateCell(0).CellStyle = style43;

            ICellStyle style44 = book.CreateCellStyle();
            style44.CloneStyleFrom(style);
            style44.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Turquoise.Index;
            sheet.CreateRow(44).CreateCell(0).CellStyle = style44;

            ICellStyle style45 = book.CreateCellStyle();
            style45.CloneStyleFrom(style);
            style45.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Violet.Index;
            sheet.CreateRow(45).CreateCell(0).CellStyle = style45;

            ICellStyle style46 = book.CreateCellStyle();
            style46.CloneStyleFrom(style);
            style46.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.White.Index;
            sheet.CreateRow(46).CreateCell(0).CellStyle = style46;

            ICellStyle style47 = book.CreateCellStyle();
            style47.CloneStyleFrom(style);
            style47.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
            sheet.CreateRow(47).CreateCell(0).CellStyle = style47;
            
            ((HSSFSheet)book.GetSheetAt(0)).AlternativeFormula = false;
            ((HSSFSheet)book.GetSheetAt(0)).AlternativeExpression = false;

            //Write the stream data of workbook to the root directory
            FileStream file = new FileStream(fileName, FileMode.Create);
            book.Write(file);
            file.Close();
        }
    }
}
原文地址:https://www.cnblogs.com/z5337/p/4845914.html