首页 > Windows开发 > 详细

C#通过NPOI逐行读取excel数据

时间:2017-07-22 14:49:06      阅读:1368      评论:0      收藏:0      [点我收藏+]
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
    {
        ///
        /// 逐行读取excel内容,如果某个单元格中无内容,则将以空字符串形式填写到List《string》的相应位置,以string.IsNullOrEmpty(str)形式判断是否为空即可
        ///
        /// 代表excel表格保存的地址,包括"文件名.xls"
        /// 代表数据读取的起始行(1,2,3.......65536)最多支持65536行
        /// 代表数据读取的结束行(1,2,3.......65536)最多支持65536行
        /// 代表数据读取的起始列(1,2,3......256)最多支持256列
        /// 代表数据读取的结束列(1,2,3......256)最多支持256列
        /// 代表将要读取的sheet表的索引位置
        /// 返回 “不为空” 代表读取成功,否则为读取失败;返回数据list《string》代表一行数据,有多少个就代表有多行数据(所有行左对齐)
        public static List> rowReadSection(string save_address, int start_row, int stop_row,
            int sart_column, int stop_column, int sheet_number)//读取excel表格相应工作表的部分数据
        {
            List> 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 oneRow = new List();

                                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>();//初始化
                                }
                                data.Add(oneRow);
                            }
                            else {
                                List oneRow = new List();//软件为相应位置空行创建内存中的空数据行
                                for (int columnIndex = sart_column - 1; columnIndex < stop_column; columnIndex++)
                                {
                                    oneRow.Add("");//填充空的数据
                                }
                                if (data == null)
                                {
                                    data = new List>();//初始化
                                }
                                data.Add(oneRow);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("NpoiOperateExcel.rowReadSection方法产生了异常!");
            }
            finally
            {
                if (readfile != null) { readfile.Close(); }
            }

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

C#通过NPOI逐行读取excel数据

(2)
(2)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!