导入、导出主要就是对excel进行取值、赋值
public class NPOIHelper
{
public HSSFWorkbook Workbook2003;
public IWorkbook Workbook2007;
public ISheet iSheet;
public string ExcelVersion;
/// <summary>
/// NPOIHelper
/// </summary>
/// <param name="filePath">Excel文件路径</param>
/// <param name="sheetName">Sheet Name</param>
public NPOIHelper(string filePath, string sheetName)
{
try
{
string extension = Path.GetExtension(filePath);
if (extension.ToLower() == ".xls")
{
ExcelVersion = "2003";
using (FileStream fs = new FileStream(@filePath, FileMode.Open, FileAccess.ReadWrite))
{
Workbook2003 = new HSSFWorkbook(fs);
iSheet = Workbook2003.GetSheet(sheetName);
}
}
else
{
ExcelVersion = "2007";
using (FileStream fs = new FileStream(@filePath, FileMode.Open, FileAccess.ReadWrite))
{
Workbook2007 = WorkbookFactory.Create(fs);
iSheet = Workbook2007.GetSheet(sheetName);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 赋值
/// </summary>
/// <param name="rowIndex">行数 从0开始</param>
/// <param name="cellIndex">列数 从0开始</param>
/// <param name="val">值</param>
public void SetValue(int rowIndex, int cellIndex, string val)
{
IRow dataRow = iSheet.GetRow(rowIndex);
if (dataRow == null)
dataRow = iSheet.CreateRow(rowIndex);
ICell newCell = dataRow.GetCell(cellIndex);
if (newCell == null)
newCell = dataRow.CreateCell(cellIndex);
iSheet.GetRow(rowIndex).GetCell(cellIndex).SetCellValue(val);
}
/// <summary>
/// 取值
/// </summary>
/// <param name="rowIndex">行数 从0开始</param>
/// <param name="cellIndex">列数 从0开始</param>
/// <returns>值</returns>
public string GetValue(int rowIndex, int cellIndex)
{
string _tempData=null;
{
}
IRow dataRow = iSheet.GetRow(rowIndex);
if (dataRow == null)
return string.Empty;
ICell dataCell = dataRow.GetCell(cellIndex);
switch(dataCell.CellType)
{
case CellType.Numeric:
_tempData= dataCell.NumericCellValue.ToString();
break;
case CellType.String:
_tempData =dataCell.StringCellValue;
break;
case CellType.Blank:
_tempData="";
break;
case CellType.Unknown:
_tempData="未知数据";
break;
}
return _tempData;
}
}