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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NPOI-2.2.0.0 的.net2.0和.net4.0版。(.net2.0版实测有效) (一)传统操作Excel遇到的问题: 1、如果是.NET,需要在服务器端装Office,且及时更新它,以防漏洞,还需要设定权限允许.NET访问COM+,如果在导出过程中出问题可能导致服务器宕机。 2、Excel会把只包含数字的列进行类型转换,本来是文本型的,Excel会将其转成数值型的,比如编号000123会变成123。 3、导出时,如果字段内容以“-”或“=”开头,Excel会把它当成公式进行,会报错。 4、Excel会根据Excel文件前8行分析数据类型,如果正好你前8行某一列只是数字,那它会认为该列为数值型,自动将该列转变成类似1.42702E+17格式,日期列变成包含日期和数字的。 (二)使用NPOI的优势 1、您可以完全免费使用该框架 2、包含了大部分EXCEL的特性(单元格样式、数据格式、公式等等) 3、专业的技术支持服务(24*7全天候) (非免费) 4、支持处理的文件格式包括xls, xlsx, docx. 5、采用面向接口的设计架构( 可以查看 NPOI.SS 的命名空间) 6、同时支持文件的导入和导出 7、基于.net 2.0 也支持xlsx 和 docx格式(当然也支持.net 4.0) 8、来自全世界大量成功且真实的测试Cases 9、大量的实例代码 11、你不需要在服务器上安装微软的Office,可以避免版权问题。 12、使用起来比Office PIA的API更加方便,更人性化。 13、你不用去花大力气维护NPOI,NPOI Team会不断更新、改善NPOI,绝对省成本。 14、不仅仅对与Excel可以进行操作,对于doc、ppt文件也可以做对应的操作 NPOI之所以强大,并不是因为它支持导出Excel,而是因为它支持导入Excel,并能“理解”OLE2文档结构,这也是其他一些Excel读写库比较弱的方面。通常,读入并理解结构远比导出来得复杂,因为导入你必须假设一切情况都是可能的,而生成你只要保证满足你自己需求就可以了,如果把导入需求和生成需求比做两个集合,那么生成需求通常都是导入需求的子集,这一规律不仅体现在Excel读写库中,也体现在pdf读写库中,目前市面上大部分的pdf库仅支持生成,不支持导入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值