C#逐行读取excel所有内容

发布时间:2020-07-03 13:24:42 作者:tjxyrjxl
来源:网络 阅读:1494

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using NPOI.SS.UserModel;

using NPOI.HSSF.UserModel;

using System.IO;


namespace www.xinduofen.cn

{

    class NpoiOperateExcel

    {

        

        /// <summary>

        /// 逐行读取某一个excel文件的某一个工作表有效范围内的全部内容,如果某个单元格中无内容,则将以空字符串形式填写到List《string》的相应位置,以string.IsNullOrEmpty(str)形式判断是否为空即可

        /// </summary>

        /// <param name="save_address">代表excel表格保存的地址,包括"文件名.xls"</param>

        /// <param name="sheet_number">代表将要读取的sheet表的索引位置</param>

        /// <returns>返回 “不为空” 代表读取成功,否则为读取失败;读取数据list《string》代表一行数据,有多少个就代表有多行数据(所有行左对齐)</returns>

        public static List<List<string>> rowReadAll(string save_address, int sheet_number)//读取excel表格相应工作表的所有数据

        {

            List<List<string>> data = null;



            //如果传入参数合法

            if (!string.IsNullOrEmpty(save_address) && sheet_number > 0){

                int rowAllCnt = NpoiOperateExcel.rowORcolAllCount(save_address,sheet_number,true);

                int colAllCnt = NpoiOperateExcel.rowORcolAllCount(save_address,sheet_number,false);


                data = NpoiOperateExcel.rowReadSection(save_address, 1, rowAllCnt, 1, colAllCnt, sheet_number);

            }

            


            return data;

        }

        

        

        public static int rowORcolAllCount(string save_address, int sheet_number, Boolean readFlag)//读取excel表格相应工作表的所有数据

        {

            int rowORcolCnt = -1;//初始化为-1

            FileStream readfile = null;


            try

            {

                //如果传入参数合法

                if (!string.IsNullOrEmpty(save_address) && sheet_number > 0)

                {

                    readfile = new FileStream(save_address, FileMode.Open, FileAccess.Read);

                    HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile);

                    ISheet sheet = hssfworkbook.GetSheetAt(sheet_number - 1);

                    if (sheet != null)

                    {

                        if (readFlag)//如果需要读取‘有效行数’

                        {

                            rowORcolCnt = sheet.LastRowNum+1;//有效行数(NPOI读取的有效行数不包括列头,所以需要加1)

                        }

                        else

                        { //如果需要读取‘最大有效列数’

                            for (int rowCnt = sheet.FirstRowNum; rowCnt <= sheet.LastRowNum; rowCnt++)//迭代所有行

                            {

                                IRow row = sheet.GetRow(rowCnt);

                                if (row != null && row.LastCellNum > rowORcolCnt)

                                {

                                    rowORcolCnt = row.LastCellNum;

                                }

                            } 

                        }

                    }

                }

            }

            catch (Exception)

            {

                Console.WriteLine("NpoiOperateExcel.rowOrColumnAllCount方法产生了异常!");

            }

            finally

            {

                if (readfile != null) { readfile.Close(); }

            }



            return rowORcolCnt;

        }


        public static List<List<string>> rowReadSection(string save_address, int start_row, int stop_row,

            int sart_column, int stop_column, int sheet_number)//读取excel表格相应工作表的部分数据

        {

            List<List<string>> data = null;//初始化为空

            FileStream readfile = null;


            try

            {

                //如果传入参数合法

                if (!string.IsNullOrEmpty(save_address) && start_row > 0 && stop_row > 0 && sart_column > 0 && stop_column>0 && sheet_number > 0)

                {

                    readfile = new FileStream(save_address, FileMode.Open, FileAccess.Read);

                    HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile);

                    ISheet sheet = hssfworkbook.GetSheetAt(sheet_number - 1);

                    if (sheet != null)

                    {

                        for (int rowIndex = start_row - 1; rowIndex < stop_row; rowIndex++)

                        {

                            IRow row = sheet.GetRow(rowIndex);

                            if (row != null)

                            {

                                List<string> oneRow = new List<string>();


                                for (int columnIndex = sart_column - 1; columnIndex < stop_column; columnIndex++)

                                {

                                    ICell cell = row.GetCell(columnIndex);

                                    if (cell != null)

                                    {

                                        oneRow.Add(cell.StringCellValue);

                                    }

                                    else {

                                        oneRow.Add("");//填充空的数据

                                    }

                                }


                                if (data == null)

                                {

                                    data = new List<List<string>>();//初始化

                                }

                                data.Add(oneRow);

                            }

                            else {

                                List<string> oneRow = new List<string>();//软件为相应位置空行创建内存中的空数据行

                                for (int columnIndex = sart_column - 1; columnIndex < stop_column; columnIndex++)

                                {

                                    oneRow.Add("");//填充空的数据

                                }

                                if (data == null)

                                {

                                    data = new List<List<string>>();//初始化

                                }

                                data.Add(oneRow);

                            }

                        }

                    }

                }

            }

            catch (Exception)

            {

                Console.WriteLine("NpoiOperateExcel.rowReadSection方法产生了异常!");

            }

            finally

            {

                if (readfile != null) { readfile.Close(); }

            }



            return data;

        }

    }

}

内容所有权属于:北京继豪电子(专业研究体质测试仪器)

推荐阅读:
  1. c#读取excel数据
  2. C# 创建、读取Excel公式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

逐行读取excel所有内容 ce

上一篇:DUL 恢复简单表测试

下一篇:www页面是用什么语言开发

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》