NPOI学习

Posted James Chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NPOI学习相关的知识,希望对你有一定的参考价值。

using System.Collections.Generic;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.SS.UserModel;

namespace NPOIDemo
{
    public class Excel
    {
        public HSSFWorkbook hssfWorkbook
        {
            get {
                FileStream file = new FileStream(@"D:\Source\NPOIDemo\NPOIDemo\Files\temp.xls", FileMode.Open, FileAccess.Read);

                return new HSSFWorkbook(file);
            }
        }

        /// <summary>
        /// 获取Sheet中所有数据有效性
        /// </summary>
        /// <returns></returns>
        public List<IDataValidation> GetDataValidations()
        {
            ISheet ws = hssfWorkbook.GetSheet("1");
            return ws.GetDataValidations();
        }

        /// <summary>
        /// 获取单元格数据有效性
        /// </summary>
        /// <param name="range">单元格范围 F71:F93或E5等</param>
        /// <returns></returns>
        public string[] GetExplicitListValuesByRange(string range)
        {
            ISheet ws = hssfWorkbook.GetSheet("1");
            var dataValidations = ws.GetDataValidations();
            foreach (var item in dataValidations)
            {
                foreach (var rangeAddresses in item.Regions.CellRangeAddresses)
                {
                    if (rangeAddresses.FormatAsString() == range)
                    {
                        return item.ValidationConstraint.ExplicitListValues;
                    }
                }
            }

            return new string[0];
        }

        /// <summary>
        /// 获取批注
        /// </summary>
        /// <returns></returns>
        public string GetCommont()
        {
            ISheet ws = hssfWorkbook.GetSheet("1");
            IComment commont=ws.GetCellComment(88, 55);

            return commont.String.String;
        }

    }
}

 

以上是关于NPOI学习的主要内容,如果未能解决你的问题,请参考以下文章

NPOI+ExcelReport

到处到Excel中NPOI

Npoi操作excel

c# NPOI 方式读取 EXCEL表 类

.Net Core+NPOI快速导入导出Excel

记一次NPOI的使用