using System;
創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)樂都,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
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>
/// 逐行讀取某一個(gè)excel文件的某一個(gè)工作表有效范圍內(nèi)的全部?jī)?nèi)容,如果某個(gè)單元格中無內(nèi)容,則將以空字符串形式填寫到List《string》的相應(yīng)位置,以string.IsNullOrEmpty(str)形式判斷是否為空即可
/// </summary>
/// <param name="save_address">代表excel表格保存的地址,包括"文件名.xls"</param>
/// <param name="sheet_number">代表將要讀取的sheet表的索引位置</param>
/// <returns>返回 “不為空” 代表讀取成功,否則為讀取失?。蛔x取數(shù)據(jù)list《string》代表一行數(shù)據(jù),有多少個(gè)就代表有多行數(shù)據(jù)(所有行左對(duì)齊)</returns>
public static List<List<string>> rowReadAll(string save_address, int sheet_number)//讀取excel表格相應(yīng)工作表的所有數(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.rowReadSection(save_address, 1, rowAllCnt, 1, colAllCnt, sheet_number);
}
return data;
}
public static int rowORcolAllCount(string save_address, int sheet_number, Boolean readFlag)//讀取excel表格相應(yīng)工作表的所有數(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>> rowReadSection(string save_address, int start_row, int stop_row,
int sart_column, int stop_column, int sheet_number)//讀取excel表格相應(yīng)工作表的部分?jǐn)?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 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("");//填充空的數(shù)據(jù)
}
}
if (data == null)
{
data = new List<List<string>>();//初始化
}
data.Add(oneRow);
}
else {
List<string> oneRow = new List<string>();//軟件為相應(yīng)位置空行創(chuàng)建內(nèi)存中的空數(shù)據(jù)行
for (int columnIndex = sart_column - 1; columnIndex < stop_column; columnIndex++)
{
oneRow.Add("");//填充空的數(shù)據(jù)
}
if (data == null)
{
data = new List<List<string>>();//初始化
}
data.Add(oneRow);
}
}
}
}
}
catch (Exception)
{
Console.WriteLine("NpoiOperateExcel.rowReadSection方法產(chǎn)生了異常!");
}
finally
{
if (readfile != null) { readfile.Close(); }
}
return data;
}
}
}
內(nèi)容所有權(quán)屬于:北京繼豪電子(專業(yè)研究體質(zhì)測(cè)試儀器)
分享文章:C#逐行讀取excel所有內(nèi)容
網(wǎng)頁URL:http://chinadenli.net/article0/ihjsio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、微信小程序、網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)、Google、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)