Initiate InventoyTypes from a xlxs
Arrival mostly working
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using CsvHelper.Excel;
|
||||
|
||||
namespace InventoryTraker.Web.Utilities
|
||||
{
|
||||
public class ExcelParserBase : IDisposable
|
||||
{
|
||||
protected readonly CsvReader CsvReader;
|
||||
|
||||
protected ExcelParserBase(FileSystemInfo excelFile, CsvConfiguration csvConfiguration)
|
||||
{
|
||||
CsvReader = OpenExcel(excelFile, csvConfiguration);
|
||||
}
|
||||
|
||||
protected ExcelParserBase(FileSystemInfo excelFile) :
|
||||
this(excelFile,
|
||||
new CsvConfiguration
|
||||
{
|
||||
HasHeaderRecord = false,
|
||||
IgnoreBlankLines = false,
|
||||
IgnoreReadingExceptions = true
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
internal static CsvReader OpenExcel(FileSystemInfo excelFile, CsvConfiguration csvConfiguration = null)
|
||||
{
|
||||
if (!excelFile.Exists)
|
||||
throw new FileNotFoundException($"Cannot find file '{excelFile.Name}'");
|
||||
|
||||
var excelParser =
|
||||
csvConfiguration != null
|
||||
? new ExcelParser(excelFile.FullName, csvConfiguration)
|
||||
: new ExcelParser(excelFile.FullName);
|
||||
return new CsvReader(excelParser);
|
||||
}
|
||||
|
||||
protected string[] GetNextCsvRowValues()
|
||||
{
|
||||
// get values from row
|
||||
if (!CsvReader.Read())
|
||||
return null;
|
||||
|
||||
return CsvReader.CurrentRecord;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
CsvReader.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
using CsvHelper.Configuration;
|
||||
using InventoryTraker.Web.Core;
|
||||
|
||||
namespace InventoryTraker.Web.Utilities
|
||||
{
|
||||
public class InventoryTypeParser
|
||||
{
|
||||
private readonly FileSystemInfo _excelFile;
|
||||
|
||||
private sealed class InventoryTypeMap : CsvClassMap<InventoryType>
|
||||
{
|
||||
public InventoryTypeMap()
|
||||
{
|
||||
Map(m => m.Identifier);
|
||||
Map(m => m.Name);
|
||||
Map(m => m.UnitsPerCase);
|
||||
Map(m => m.ContainerType);
|
||||
Map(m => m.WeightPerCase);
|
||||
Map(m => m.PricePerCase);
|
||||
}
|
||||
}
|
||||
|
||||
public InventoryTypeParser(FileSystemInfo excelFile)
|
||||
{
|
||||
_excelFile = excelFile;
|
||||
}
|
||||
|
||||
public IList<InventoryType> Parse()
|
||||
{
|
||||
var csvConfiguration =
|
||||
new CsvConfiguration { IsHeaderCaseSensitive = false, IgnoreReadingExceptions = true};
|
||||
csvConfiguration.RegisterClassMap<InventoryTypeMap>();
|
||||
using (var reader = ExcelParserBase.OpenExcel(_excelFile, csvConfiguration))
|
||||
{
|
||||
return reader.GetRecords<InventoryType>().ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user