溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

excel怎么逐列讀取所有數(shù)據(jù)

發(fā)布時間:2021-08-18 20:17:50 來源:億速云 閱讀:176 作者:chen 欄目:系統(tǒng)運維

這篇文章主要介紹“excel怎么逐列讀取所有數(shù)據(jù)”,在日常操作中,相信很多人在excel怎么逐列讀取所有數(shù)據(jù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”excel怎么逐列讀取所有數(shù)據(jù)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

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>返回 “不為空” 代表讀取成功,否則為讀取失敗;讀取數(shù)據(jù)list《string》代表一列數(shù)據(jù),有多少個就代表有多列數(shù)據(jù)(所有列上對齊</returns>

        public static List<List<string>> colReadAll(string save_address, int sheet_number)//讀取excel表格相應工作表的所有數(shù)據(jù)

        {

            List<List<string>> data = null;

            //如果傳入?yún)?shù)合法

            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.colReadSection(save_address, 1, rowAllCnt, 1, colAllCnt, sheet_number);

            }

            return data;

        }

        public static int rowORcolAllCount(string save_address, int sheet_number, Boolean readFlag)//讀取excel表格相應工作表的所有數(shù)據(jù)

        {

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

            FileStream readfile = null;

            try

            {

                //如果傳入?yún)?shù)合法

                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)//如果需要讀取‘有效行數(shù)’

                        {

                            rowORcolCnt = sheet.LastRowNum+1;//有效行數(shù)(NPOI讀取的有效行數(shù)不包括列頭,所以需要加1)

                        }

                        else

                        { //如果需要讀取‘最大有效列數(shù)’

                            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方法產(chǎn)生了異常!");

            }

            finally

            {

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

            }

            return rowORcolCnt;

        }

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

            int sart_column, int stop_column, int sheet_number)//讀取excel表格相應工作表的部分數(shù)據(jù)

        {

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

            FileStream readfile = null;

            try

            {

                //如果傳入?yún)?shù)合法

                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 columnIndex = sart_column - 1; columnIndex < stop_column; columnIndex++) {

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

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

                                IRow row = sheet.GetRow(rowIndex);

                                if (row != null)

                                {

                                    ICell cell = row.GetCell(columnIndex);

                                    if (cell != null)

                                    {

                                        oneCol.Add(cell.StringCellValue);

                                    }

                                    else

                                    {

                                        oneCol.Add("");//填充空的數(shù)據(jù)

                                    }

                                }

                                else { 

                                    oneCol.Add("");//填充空的數(shù)據(jù)

                                }

                            }

                            if (data == null)

                            {

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

                            }

                            data.Add(oneCol);

                        }

                    }

                }

            }

            catch (Exception)

            {

                Console.WriteLine("NpoiOperateExcel.colReadSection方法產(chǎn)生了異常!");

            }

            finally

            {

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

            }

            return data;

        }

    }

}

到此,關于“excel怎么逐列讀取所有數(shù)據(jù)”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI