千家信息网

excel怎么逐列读取所有数据

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章主要介绍"excel怎么逐列读取所有数据",在日常操作中,相信很多人在excel怎么逐列读取所有数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"excel怎么
千家信息网最后更新 2025年12月02日excel怎么逐列读取所有数据

这篇文章主要介绍"excel怎么逐列读取所有数据",在日常操作中,相信很多人在excel怎么逐列读取所有数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"excel怎么逐列读取所有数据"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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"

/// 代表将要读取的sheet表的索引位置

/// 返回 "不为空" 代表读取成功,否则为读取失败;读取数据list《string》代表一列数据,有多少个就代表有多列数据(所有列上对齐

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

{

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

List oneCol= new List();

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("");//填充空的数据

}

}

else {

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

}

}

if (data == null)

{

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

}

data.Add(oneCol);

}

}

}

}

catch (Exception)

{

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

}

finally

{

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

}

return data;

}

}

}

到此,关于"excel怎么逐列读取所有数据"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0