144 lines
4.3 KiB
C#
144 lines
4.3 KiB
C#
using Excel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace Fab2ApprovalSystem.Misc
|
|
{
|
|
public class ExcelData
|
|
{
|
|
string _path;
|
|
public ExcelData(string path)
|
|
{
|
|
_path = path;
|
|
}
|
|
|
|
public IExcelDataReader getExcelReader()
|
|
{
|
|
// ExcelDataReader works with the binary Excel file, so it needs a FileStream
|
|
// to get started. This is how we avoid dependencies on ACE or Interop:
|
|
|
|
FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read);
|
|
// We return the interface, so that
|
|
IExcelDataReader reader = null;
|
|
|
|
try
|
|
{
|
|
if (_path.EndsWith(".xls"))
|
|
{
|
|
reader = ExcelReaderFactory.CreateBinaryReader(stream);
|
|
}
|
|
if (_path.EndsWith(".xlsx"))
|
|
{
|
|
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
|
|
}
|
|
return reader;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
public class ExcelLotInfo
|
|
{
|
|
public string LotNo { get; set; }
|
|
public string LotDispo { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerable<ExcelLotInfo> ReadData()
|
|
{
|
|
var r = new List<ExcelLotInfo>();
|
|
var excelData = new ExcelData(_path);
|
|
var lots = excelData.getData().ToList();
|
|
|
|
int lotDispoColumnIndex = -1;
|
|
foreach (DataColumn col in lots[0].Table.Columns)
|
|
{
|
|
if (col.ColumnName.ToLower().Contains("dispo"))
|
|
{
|
|
lotDispoColumnIndex = col.Ordinal;
|
|
break;
|
|
}
|
|
}
|
|
|
|
foreach (var row in lots)
|
|
{
|
|
string temValue = row[0].ToString();
|
|
if (temValue.Trim().Length > 0 && temValue.Trim().Length <= 10 )
|
|
{
|
|
r.Add(new ExcelLotInfo()
|
|
{
|
|
LotNo = row[0].ToString(),
|
|
LotDispo = (lotDispoColumnIndex >= 0 ? row[lotDispoColumnIndex].ToString() : "")
|
|
});
|
|
}
|
|
|
|
}
|
|
return r;
|
|
}
|
|
|
|
|
|
public IEnumerable<string> ReadQDBFlagData()
|
|
{
|
|
List<string> s = new List<string>();
|
|
// We return the interface, so that
|
|
var excelData = new ExcelData(_path);
|
|
//var albums = excelData.getData("Sheet1");
|
|
var lotNos = excelData.getData();
|
|
foreach (var row in lotNos)
|
|
{
|
|
string temValue = row[0].ToString();
|
|
if (temValue.Trim().Length > 0 && temValue.Trim().Length == 9)
|
|
{
|
|
if (row[2].ToString().ToUpper() != "YES" && row[2].ToString().ToUpper() != "NO")
|
|
{
|
|
throw new Exception("Invalid data in the file");
|
|
}
|
|
else
|
|
{
|
|
s.Add(row[0].ToString() + "~" + row[1] + "~" + row[2]);
|
|
}
|
|
}
|
|
|
|
}
|
|
return s;
|
|
}
|
|
|
|
//public IEnumerable<DataRow> getData(string sheet, bool firstRowIsColumnNames = true)
|
|
//{
|
|
// var reader = this.getExcelReader();
|
|
// reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
|
|
// var workSheet = reader.AsDataSet().Tables[sheet];
|
|
// var rows = from DataRow a in workSheet.Rows select a;
|
|
// return rows;
|
|
|
|
//}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="firstRowIsColumnNames"></param>
|
|
/// <returns></returns>
|
|
public IEnumerable<DataRow> getData(bool firstRowIsColumnNames = true)
|
|
{
|
|
var reader = this.getExcelReader();
|
|
reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
|
|
var workSheet = reader.AsDataSet().Tables[0];
|
|
var rows = from DataRow a in workSheet.Rows select a;
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |