Merged PR 13298: Added just one approval back in after removing the method call from bug 239935
Added just one approval back in after removing the method call from bug 239935 Added IExcelDataReader support into MK Project Changed instructions below the ECN Title field to align with Windchill Related work items: #225480, #244087
This commit is contained in:
@ -1,104 +1,109 @@
|
||||
#if !NET8
|
||||
#if NET8
|
||||
|
||||
using ExcelDataReader;
|
||||
|
||||
#else
|
||||
|
||||
using Excel;
|
||||
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
|
||||
namespace Fab2ApprovalSystem.Misc {
|
||||
namespace Fab2ApprovalSystem.Misc;
|
||||
|
||||
public class ExcelData {
|
||||
string _path;
|
||||
public ExcelData(string path) {
|
||||
_path = path;
|
||||
}
|
||||
public class ExcelData {
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
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 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(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;
|
||||
|
||||
}
|
||||
private readonly string _Path;
|
||||
|
||||
public ExcelData(string path) {
|
||||
_Path = path;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
public IExcelDataReader getExcelReader() {
|
||||
FileStream stream = File.Open(_Path, FileMode.Open, FileAccess.Read);
|
||||
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; }
|
||||
}
|
||||
|
||||
public IEnumerable<ExcelLotInfo> ReadData() {
|
||||
List<ExcelLotInfo> r = new();
|
||||
ExcelData excelData = new(_Path);
|
||||
List<DataRow> 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 (DataRow 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();
|
||||
ExcelData excelData = new(_Path);
|
||||
IEnumerable<DataRow> lotNos = excelData.getData();
|
||||
foreach (DataRow 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;
|
||||
}
|
||||
|
||||
#if NET8
|
||||
|
||||
public IEnumerable<DataRow> getData(bool firstRowIsColumnNames = true) =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
#else
|
||||
|
||||
public IEnumerable<DataRow> getData(bool firstRowIsColumnNames = true) {
|
||||
IExcelDataReader reader = getExcelReader();
|
||||
reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
|
||||
var workSheet = reader.AsDataSet().Tables[0];
|
||||
var rows = from DataRow a in workSheet.Rows select a;
|
||||
return rows;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
Reference in New Issue
Block a user