基于NPOI的读取Excel单元格的简单封装类

一、程序代码

using System;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using EntityLayer;
using System.Configuration;

namespace DataLayer
{
    public class ConfigurationProvider
    {
        public static string ReadExcelCell(string excelfilepath, string excelsheet, int excelrow, int excelcol)
        {
            try
            {
                XSSFWorkbook workbook = null;
                FileStream filestream = new FileStream(excelfilepath, FileMode.Open, FileAccess.Read);
                workbook = new XSSFWorkbook(filestream);
                filestream.Close();
                ISheet sheet = workbook.GetSheet(excelsheet);
                IRow row = sheet.GetRow(0);
                if (sheet.GetRow(excelrow) == null)
                {
                    return null;
                }
                else
                {
                    XSSFRow trow = (XSSFRow)sheet.GetRow(excelrow);

                    if (trow.GetCell(excelcol) == null)
                    {
                        return null;
                    }
                    else
                    {
                        XSSFCell tcell = (XSSFCell)trow.GetCell(excelcol);
                        return tcell.ToString();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/AzariasTC/p/12758956.html