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


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

    2016年12月15日 15:25:01 起个名字真的好难啊 阅读数:15091更多

    个人分类: 文件操作

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/e295166319/article/details/53670780

    使用NPOI设置Excel单元格背景颜色时,应该设置FillForegroundColor属性,而且还要设置FillPattern才行。

    代码如下:

    style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PINK.index;

    style.FillPattern = FillPatternType.SOLID_FOREGROUND;

    简单代码示例:

    
     
    1. using UnityEngine;

    2. using System.Collections;

    3. using UnityEditor;

    4. using NPOI.HSSF.UserModel;

    5. using NPOI.HPSF;

    6. using NPOI.HSSF.Util;

    7. using NPOI.POIFS.FileSystem;

    8. using NPOI.SS.UserModel;

    9. using System.Collections.Generic;

    10. using System.Linq;

    11. using System.IO;

    12. public class WorkBook

    13. {

    14. [MenuItem("H3D/XLSX文件测试")]

    15. static void CreateExcelFile()

    16. {

    17. // 生成简报

    18. IWorkbook wb = new HSSFWorkbook();

    19. var sheet = wb.CreateSheet("第一页");

    20. int currentRow = 0;

    21.  
    22. ICellStyle s = wb.CreateCellStyle ();

    23. s.FillForegroundColor = HSSFColor.Pink.Index;

    24. s.FillPattern = FillPattern.SolidForeground;

    25.  
    26. // 简报开始

    27. var row = sheet.CreateRow(currentRow++);

    28. row.CreateCell(0).SetCellValue("第一页第一行");

    29. row.GetCell(0).CellStyle = s;

    30.  
    31. row = sheet.CreateRow(currentRow++);

    32. row.CreateCell(0).SetCellValue("第一页第二行");

    33.  
    34.  
    35.  
    36. var sheet2 = wb.CreateSheet("第二页");

    37. int currentRow2 = 0;

    38. // 简报开始

    39. var row2 = sheet2.CreateRow(currentRow2++);

    40. row2.CreateCell(0).SetCellValue("第二页第一行");

    41.  
    42. row2 = sheet2.CreateRow(currentRow2++);

    43. row2.CreateCell(0).SetCellValue("第二页第二行");

    44.  
    45.  
    46. //save

    47. string savePath = "XLSXTest.xlsx";

    48. FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);

    49. wb.Write(fs);

    50. fs.Close();

    51. Debug.Log("报告路径:" + savePath);

    52. }

    53.  
    54. }

    效果图:


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

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

  • 相关阅读:
    prufer 序列 学习笔记
    题解 [SDOI2009]E&D/染色游戏/Moving Pebbles
    题解 Christmas Game
    题解 [HNOI/AHOI2018]毒瘤
    题解 UVA1500 Alice and Bob
    Mac端影片压制流程
    react:Text nodes cannot appear as a child
    阿里云docker镜像地址
    Vscode 智能插件
    vue-vant实现IndexBar索引栏
  • 原文地址:https://www.cnblogs.com/grj001/p/12225412.html
Copyright © 2020-2023  润新知