Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

優化 workflow 的使用者體驗 #14

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions Excely.ClosedXML/Workflows/XlsxImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@

namespace Excely.Workflows
{
/// <summary>
/// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。
/// </summary>
public class XlsxImporter : ExcelyImporterBase<IXLWorksheet>
{
protected XlsxTableFactory XlsxTableFactory { get; set; } = new XlsxTableFactory();
/// <summary>
/// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。
/// </summary>
public class XlsxImporter : ExcelyImporterBase<IXLWorksheet>
{
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);
}
}
}
51 changes: 30 additions & 21 deletions Excely.EPPlus.LGPL/Workflows/XlsxImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@

namespace Excely.Workflows
{
/// <summary>
/// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。
/// </summary>
public class XlsxImporter : ExcelyImporterBase<ExcelWorksheet>
{
protected XlsxTableFactory XlsxTableFactory { get; set; } = new XlsxTableFactory();
/// <summary>
/// 提供快速建立從 Excel 到指定資料結構之 Importer 的方法。
/// </summary>
public class XlsxImporter : ExcelyImporterBase<ExcelWorksheet>
{
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];
}
}
}
33 changes: 21 additions & 12 deletions Excely/Workflows/CsvStringImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

namespace Excely.Workflows
{
/// <summary>
/// 以 Csv 字串為來源資料的 Importer。
/// </summary>
public class CsvStringImporter : ExcelyImporterBase<string>
{
protected CsvStringTableFactory CsvTableFactory { get; set; } = new();
/// <summary>
/// 以 Csv 字串為來源資料的 Importer。
/// </summary>
public class CsvStringImporter : ExcelyImporterBase<string>
{
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();
}
}
}
96 changes: 57 additions & 39 deletions Excely/Workflows/ExcelyImporterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,63 @@

namespace Excely.Workflows
{
/// <summary>
/// 提供從資料輸入到轉換為資料結構的完整工作流程之基底類別。
/// </summary>
/// <typeparam name="TInput">資料的輸入型別</typeparam>
public abstract class ExcelyImporterBase<TInput>
{
protected abstract ExcelyTable GetTable(TInput input);
/// <summary>
/// 提供從資料輸入到轉換為資料結構的完整工作流程之基底類別。
/// </summary>
/// <typeparam name="TInput">資料的輸入型別</typeparam>
public abstract class ExcelyImporterBase<TInput>
{
protected abstract ExcelyTable GetTable(TInput input);

/// <summary>
/// 將資料匯入為物件列表。
/// </summary>
/// <typeparam name="TClass">目標類別</typeparam>
/// <param name="dataSource">資料來源</param>
/// <param name="options">匯入邏輯</param>
/// <returns>匯入結果</returns>
public IEnumerable<TClass> ToClassList<TClass>(
TInput dataSource,
ClassListTableConverterOptions<TClass>? options = null)
where TClass : class, new()
{
var table = GetTable(dataSource);
var converter = options == null ? new ClassListTableConverter<TClass>() : new ClassListTableConverter<TClass>(options);
return converter.ConvertFrom(table);
}
/// <summary>
/// 將資料匯入為物件列表。
/// </summary>
/// <typeparam name="TClass">目標類別</typeparam>
/// <param name="dataSource">資料來源</param>
/// <param name="options">匯入邏輯</param>
/// <returns>匯入結果</returns>
public IEnumerable<TClass> ToClassList<TClass>(
TInput dataSource,
ClassListTableConverterOptions<TClass>? options = null)
where TClass : class, new()
{
var table = GetTable(dataSource);
var converter = options == null ? new ClassListTableConverter<TClass>() : new ClassListTableConverter<TClass>(options);
return converter.ConvertFrom(table);
}

/// <summary>
/// 將資料匯入為字典列表。
/// </summary>=
/// <param name="dataSource">資料來源</param>
/// <param name="options">匯入邏輯</param>
/// <returns>匯入結果</returns>
public IEnumerable<Dictionary<string, object?>> ToDictionaryList(
TInput dataSource,
DictionaryListTableConverterOptions? options = null)
{
var table = GetTable(dataSource);
var converter = options == null ? new DictionaryListTableConverter() : new DictionaryListTableConverter(options);
return converter.ConvertFrom(table);
}
}
}
/// <summary>
/// 將資料匯入為字典列表。
/// </summary>=
/// <param name="dataSource">資料來源</param>
/// <param name="options">匯入邏輯</param>
/// <returns>匯入結果</returns>
public IEnumerable<Dictionary<string, object?>> 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<TClass> ToClassList<TClass>(
string filePath,
ClassListTableConverterOptions<TClass>? options = null)
where TClass : class, new()
{
var dataSource = GetDataSource(filePath);
return ToClassList(dataSource, options);
}

public IEnumerable<Dictionary<string, object?>> ToDictionaryList(
string filePath,
DictionaryListTableConverterOptions? options = null)
{
var dataSource = GetDataSource(filePath);
return ToDictionaryList(dataSource, options);
}
}
}
Loading