用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

基于NPOI的excel文件读取

2014-09-19 作者: rentao315举报

[c#]代码库

using NPOI.HPSF;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;


namespace RExcelLib
{

    public class ExcelHleper
    {
        /*
         将Excel中的内容当作纯数据读取,无表说明
         */
        static public DataTable ReadAsPureData(string fileName,int feetPage)
        {
            ISheet sheet;
            sheet = GetSheet(fileName, feetPage);
            
           System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

           using( DataTable dataTable = new DataTable())
           { 
                for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
                {
                    dataTable.Columns.Add("A" + j.ToString());
                }
                while (rows.MoveNext())
                {
                    HSSFRow row = (HSSFRow)rows.Current;
                    DataRow dr = dataTable.NewRow();
                    for (int i = 0; i < row.LastCellNum; i++)
                    {
                        var cell = row.GetCell(i);
                        if (cell == null)
                        {
                            dr[i] = null;
                        }
                        else
                        {
                            dr[i] = cell.ToString();
                        }
                    }
                    dataTable.Rows.Add(dr);
                }
                return dataTable;
           }
        }


        static public DataTable ReadDataFirsLineAsHead(string fileName, int feetPage,bool isAbandonHead = true)
        {
            ISheet sheet;
            sheet = GetSheet(fileName, feetPage);

            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            using (DataTable dataTable = new DataTable())
            {
                var headRow = sheet.GetRow(0);
                for (int j = 0; j < headRow.LastCellNum; j++)
                {
                    var headName = headRow.Cells.ElementAt(j);
                    dataTable.Columns.Add(headName.ToString());
                }

                if (isAbandonHead)
                {
                    rows.MoveNext();
                   
                }
                
                while (rows.MoveNext())
                {
                    HSSFRow row = (HSSFRow)rows.Current;
                    DataRow dr = dataTable.NewRow();
                    
                    for (int i = 0; i < row.LastCellNum; i++)
                    {
                        var cell = row.GetCell(i);
                        if (cell == null)
                        {
                            dr[i] = null;
                        }
                        else
                        {
                            dr[i] = cell.ToString();
                        }
                    }
                    dataTable.Rows.Add(dr);
                }
                return dataTable;
            }
        }


        private static ISheet GetSheet(string fileName, int feetPage)
        {
            ISheet sheet;
            try
            {
                using (FileStream fs = File.OpenRead(fileName))
                {
                    var wk = new HSSFWorkbook(fs);
                    sheet = wk.GetSheetAt(feetPage);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return sheet;
        }

        

    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...