git_bsmd/nsw/Source/bsmd.ExcelReadService/ExcelReader.cs

60 lines
1.8 KiB
C#

//
// Class: ExcelReader
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 6/15/2015 10:03:40 PM
//
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using log4net;
using Excel.Core;
using Excel;
namespace bsmd.ExcelReadService
{
public class ExcelReader : IDisposable
{
private IExcelDataReader dataReader;
private ILog _log = LogManager.GetLogger(typeof(ExcelReader));
public ExcelReader(string filePath)
{
using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase))
dataReader = ExcelReaderFactory.CreateBinaryReader(fs);
else if (filePath.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase))
dataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
else
throw new ArgumentException(string.Format("saved file {0} is not an excel file", filePath));
}
}
public object GetCell(string workSheetName, int row, int col)
{
this.dataReader.IsFirstRowAsColumnNames = false;
DataSet dataSet = dataReader.AsDataSet();
DataTable workSheet = dataSet.Tables[workSheetName];
var cellValue = workSheet.Rows[row][col];
return cellValue;
}
public void Dispose()
{
if (this.dataReader != null)
this.dataReader.Dispose();
}
}
}