using System;
創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)樂都,10多年網(wǎng)站建設(shè)經(jīng)驗,價格優(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
{
///
/// 逐行讀取某一個excel文件的某一個工作表有效范圍內(nèi)的全部內(nèi)容,如果某個單元格中無內(nèi)容,則將以空字符串形式填寫到List《string》的相應(yīng)位置,以string.IsNullOrEmpty(str)形式判斷是否為空即可
///
/// 代表excel表格保存的地址,包括"文件名.xls"
/// 代表將要讀取的sheet表的索引位置
///
public static List> rowReadAll(string save_address, int sheet_number)//讀取excel表格相應(yīng)工作表的所有數(shù)據(jù)
{
List> 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> 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> 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
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>();//初始化
}
data.Add(oneRow);
}
else {
List
for (int columnIndex = sart_column - 1; columnIndex < stop_column; columnIndex++)
{
oneRow.Add("");//填充空的數(shù)據(jù)
}
if (data == null)
{
data = new List>();//初始化
}
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ì)測試儀器)