- ติดตั้ง Package NPOI Version 2.4.1
- อ่านไฟล์ Excel
1.ติดตั้ง Package NPOI Version 2.4.1
PM> Install-Package NPOI -Version 2.4.1
2.อ่านไฟล์ Excel
อ่าน Sheet1 แถวแรก คอลัมน์แรก
using NPOI.SS.UserModel;
using System;
using System.Globalization;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string filename = @"Workbook1.xlsx";
using (var fileData = new FileStream(filename, FileMode.Open))
{
ExcelFileReader excelFileReader = new ExcelFileReader();
excelFileReader.InitializeMembers(fileData);
ISheet sheet1 = excelFileReader.workbook.GetSheet("Sheet1");
IRow row0 = sheet1.GetRow(0);
ICell cell0 = row0.GetCell(0);
string text = cell0.StringCellValue;
Console.WriteLine(text);
}
Console.WriteLine("OK");
}
//
// ExcelFileReader
//
public class ExcelFileReader
{
public IWorkbook workbook;
public DataFormatter dataFormatter;
public IFormulaEvaluator formulaEvaluator;
//
// Initialize from a stream of Excel file
//
public void InitializeMembers(Stream excelFileStream)
{
this.workbook = WorkbookFactory.Create(excelFileStream);
if (this.workbook != null)
{
this.dataFormatter = new DataFormatter(CultureInfo.InvariantCulture);
this.formulaEvaluator = WorkbookFactory.CreateFormulaEvaluator(this.workbook);
}
}
//
// Get formatted value as string from the specified cell
//
public string GetFormattedValue(ICell cell)
{
string returnValue = string.Empty;
if (cell != null)
{
try
{
// Get evaluated and formatted cell value
returnValue = this.dataFormatter.FormatCellValue(cell, this.formulaEvaluator);
}
catch
{
// When failed in evaluating the formula, use stored values instead...
// and set cell value for reference from formulae in other cells...
if (cell.CellType == CellType.Formula)
{
switch (cell.CachedFormulaResultType)
{
case CellType.String:
returnValue = cell.StringCellValue;
cell.SetCellValue(cell.StringCellValue);
break;
case CellType.Numeric:
returnValue = dataFormatter.FormatRawCellContents(cell.NumericCellValue, 0, cell.CellStyle.GetDataFormatString());
cell.SetCellValue(cell.NumericCellValue);
break;
case CellType.Boolean:
returnValue = cell.BooleanCellValue.ToString();
cell.SetCellValue(cell.BooleanCellValue);
break;
default:
break;
}
}
}
}
return (returnValue ?? string.Empty).Trim();
}
//
// Get unformatted value as string from the specified cell
//
public string GetUnformattedValue(ICell cell)
{
string returnValue = string.Empty;
if (cell != null)
{
try
{
// Get evaluated cell value
returnValue = (cell.CellType == CellType.Numeric ||
(cell.CellType == CellType.Formula &&
cell.CachedFormulaResultType == CellType.Numeric)) ?
formulaEvaluator.EvaluateInCell(cell).NumericCellValue.ToString() :
this.dataFormatter.FormatCellValue(cell, this.formulaEvaluator);
}
catch
{
// When failed in evaluating the formula, use stored values instead...
// and set cell value for reference from formulae in other cells...
if (cell.CellType == CellType.Formula)
{
switch (cell.CachedFormulaResultType)
{
case CellType.String:
returnValue = cell.StringCellValue;
cell.SetCellValue(cell.StringCellValue);
break;
case CellType.Numeric:
returnValue = cell.NumericCellValue.ToString();
cell.SetCellValue(cell.NumericCellValue);
break;
case CellType.Boolean:
returnValue = cell.BooleanCellValue.ToString();
cell.SetCellValue(cell.BooleanCellValue);
break;
default:
break;
}
}
}
}
return (returnValue ?? string.Empty).Trim();
}
}
}
}
Link