码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI学习

时间:2017-08-06 16:01:04      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:demo   ida   list   logs   for   poi   lis   region   user   

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学习

标签:demo   ida   list   logs   for   poi   lis   region   user   

原文地址:http://www.cnblogs.com/czblogs/p/7294809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!