diff --git a/Excely.ClosedXML/Workflows/XlsxImporter.cs b/Excely.ClosedXML/Workflows/XlsxImporter.cs index 88dfe5a..1ba6252 100644 --- a/Excely.ClosedXML/Workflows/XlsxImporter.cs +++ b/Excely.ClosedXML/Workflows/XlsxImporter.cs @@ -3,27 +3,36 @@ namespace Excely.Workflows { - /// - /// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。 - /// - public class XlsxImporter : ExcelyImporterBase - { - protected XlsxTableFactory XlsxTableFactory { get; set; } = new XlsxTableFactory(); + /// + /// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。 + /// + public class XlsxImporter : ExcelyImporterBase + { + protected XlsxTableFactory XlsxTableFactory { get; set; } = new XlsxTableFactory(); - #region === 建構子 == - public XlsxImporter() { } + #region === 建構子 == - public XlsxImporter(CellLocation? startCell, CellLocation? endCell) - { - XlsxTableFactory = new XlsxTableFactory(); - if (startCell != null) - { - XlsxTableFactory.StartCell = startCell.Value; - } - XlsxTableFactory.EndCell = endCell; - } - #endregion + public XlsxImporter() + { } - protected override ExcelyTable GetTable(IXLWorksheet input) => XlsxTableFactory.GetTable(input); - } -} + public XlsxImporter(CellLocation? startCell, CellLocation? endCell) + { + XlsxTableFactory = new XlsxTableFactory(); + if (startCell != null) + { + XlsxTableFactory.StartCell = startCell.Value; + } + XlsxTableFactory.EndCell = endCell; + } + + #endregion === 建構子 == + + protected override ExcelyTable GetTable(IXLWorksheet input) => XlsxTableFactory.GetTable(input); + + protected override IXLWorksheet GetDataSource(string filePath) + { + var workbook = new XLWorkbook(filePath); + return workbook.Worksheet(0); + } + } +} \ No newline at end of file diff --git a/Excely.EPPlus.LGPL/Workflows/XlsxImporter.cs b/Excely.EPPlus.LGPL/Workflows/XlsxImporter.cs index 52c480a..8930151 100644 --- a/Excely.EPPlus.LGPL/Workflows/XlsxImporter.cs +++ b/Excely.EPPlus.LGPL/Workflows/XlsxImporter.cs @@ -3,27 +3,36 @@ namespace Excely.Workflows { - /// - /// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。 - /// - public class XlsxImporter : ExcelyImporterBase - { - protected XlsxTableFactory XlsxTableFactory { get; set; } = new XlsxTableFactory(); + /// + /// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。 + /// + public class XlsxImporter : ExcelyImporterBase + { + protected XlsxTableFactory XlsxTableFactory { get; set; } = new XlsxTableFactory(); - #region === 建構子 == - public XlsxImporter() { } + #region === 建構子 == - public XlsxImporter(CellLocation? startCell, CellLocation? endCell) - { - XlsxTableFactory = new XlsxTableFactory(); - if (startCell != null) - { - XlsxTableFactory.StartCell = startCell.Value; - } - XlsxTableFactory.EndCell = endCell; - } - #endregion + public XlsxImporter() + { } - protected override ExcelyTable GetTable(ExcelWorksheet input) => XlsxTableFactory.GetTable(input); - } -} + public XlsxImporter(CellLocation? startCell, CellLocation? endCell) + { + XlsxTableFactory = new XlsxTableFactory(); + if (startCell != null) + { + XlsxTableFactory.StartCell = startCell.Value; + } + XlsxTableFactory.EndCell = endCell; + } + + #endregion === 建構子 == + + protected override ExcelyTable GetTable(ExcelWorksheet input) => XlsxTableFactory.GetTable(input); + + protected override ExcelWorksheet GetDataSource(string filePath) + { + var package = new ExcelPackage(new FileInfo(filePath)); + return package.Workbook.Worksheets[0]; + } + } +} \ No newline at end of file diff --git a/Excely/Workflows/CsvStringImporter.cs b/Excely/Workflows/CsvStringImporter.cs index 15c5446..2210a81 100644 --- a/Excely/Workflows/CsvStringImporter.cs +++ b/Excely/Workflows/CsvStringImporter.cs @@ -2,17 +2,26 @@ namespace Excely.Workflows { - /// - /// 以 Csv 字串為來源資料的 Importer。 - /// - public class CsvStringImporter : ExcelyImporterBase - { - protected CsvStringTableFactory CsvTableFactory { get; set; } = new(); + /// + /// 以 Csv 字串為來源資料的 Importer。 + /// + public class CsvStringImporter : ExcelyImporterBase + { + protected CsvStringTableFactory CsvTableFactory { get; set; } = new(); - #region === 建構子 == - public CsvStringImporter() { } - #endregion + #region === 建構子 == - protected override ExcelyTable GetTable(string input) => CsvTableFactory.GetTable(input); - } -} + public CsvStringImporter() + { } + + #endregion === 建構子 == + + protected override ExcelyTable GetTable(string input) => CsvTableFactory.GetTable(input); + + protected override string GetDataSource(string filePath) + { + using var streamReader = new StreamReader(filePath); + return streamReader.ReadToEnd(); + } + } +} \ No newline at end of file diff --git a/Excely/Workflows/ExcelyImporterBase.cs b/Excely/Workflows/ExcelyImporterBase.cs index c2a29d4..573ac95 100644 --- a/Excely/Workflows/ExcelyImporterBase.cs +++ b/Excely/Workflows/ExcelyImporterBase.cs @@ -2,45 +2,63 @@ namespace Excely.Workflows { - /// - /// 提供從資料輸入到轉換為資料結構的完整工作流程之基底類別。 - /// - /// 資料的輸入型別 - public abstract class ExcelyImporterBase - { - protected abstract ExcelyTable GetTable(TInput input); + /// + /// 提供從資料輸入到轉換為資料結構的完整工作流程之基底類別。 + /// + /// 資料的輸入型別 + public abstract class ExcelyImporterBase + { + protected abstract ExcelyTable GetTable(TInput input); - /// - /// 將資料匯入為物件列表。 - /// - /// 目標類別 - /// 資料來源 - /// 匯入邏輯 - /// 匯入結果 - public IEnumerable ToClassList( - TInput dataSource, - ClassListTableConverterOptions? options = null) - where TClass : class, new() - { - var table = GetTable(dataSource); - var converter = options == null ? new ClassListTableConverter() : new ClassListTableConverter(options); - return converter.ConvertFrom(table); - } + /// + /// 將資料匯入為物件列表。 + /// + /// 目標類別 + /// 資料來源 + /// 匯入邏輯 + /// 匯入結果 + public IEnumerable ToClassList( + TInput dataSource, + ClassListTableConverterOptions? options = null) + where TClass : class, new() + { + var table = GetTable(dataSource); + var converter = options == null ? new ClassListTableConverter() : new ClassListTableConverter(options); + return converter.ConvertFrom(table); + } - /// - /// 將資料匯入為字典列表。 - /// = - /// 資料來源 - /// 匯入邏輯 - /// 匯入結果 - public IEnumerable> ToDictionaryList( - TInput dataSource, - DictionaryListTableConverterOptions? options = null) - { - var table = GetTable(dataSource); - var converter = options == null ? new DictionaryListTableConverter() : new DictionaryListTableConverter(options); - return converter.ConvertFrom(table); - } - } -} + /// + /// 將資料匯入為字典列表。 + /// = + /// 資料來源 + /// 匯入邏輯 + /// 匯入結果 + public IEnumerable> ToDictionaryList( + TInput dataSource, + DictionaryListTableConverterOptions? options = null) + { + var table = GetTable(dataSource); + var converter = options == null ? new DictionaryListTableConverter() : new DictionaryListTableConverter(options); + return converter.ConvertFrom(table); + } + protected abstract TInput GetDataSource(string filePath); + + public IEnumerable ToClassList( + string filePath, + ClassListTableConverterOptions? options = null) + where TClass : class, new() + { + var dataSource = GetDataSource(filePath); + return ToClassList(dataSource, options); + } + + public IEnumerable> ToDictionaryList( + string filePath, + DictionaryListTableConverterOptions? options = null) + { + var dataSource = GetDataSource(filePath); + return ToDictionaryList(dataSource, options); + } + } +} \ No newline at end of file