feat(TableExport): add DataReaderBase class (#4074)

* refactor: 使用基类精简代码

* chore: update excel package
This commit is contained in:
Argo Zhang 2024-08-15 22:20:26 +08:00 committed by GitHub
parent c99b66075a
commit acac1b9d5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 136 deletions

View File

@ -11,7 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="8.5.1" /> <PackageReference Include="BootstrapBlazor" Version="8.5.1" />
<PackageReference Include="MiniExcel" Version="1.31.3" /> <PackageReference Include="MiniExcel" Version="1.34.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -3,11 +3,12 @@
// Website: https://www.blazor.zone or https://argozhang.github.io/ // Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components.Extensions; using BootstrapBlazor.Components.Extensions;
using MiniExcelLibs;
using System.Data; using System.Data;
namespace BootstrapBlazor.Components; namespace BootstrapBlazor.Components;
class ExportDataReader<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn> cols, TableExportOptions options) : IDataReader class ExportDataReader<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn> cols, TableExportOptions options) : MiniExcelDataReaderBase
{ {
private int _rowIndex = -1; private int _rowIndex = -1;
private readonly IEnumerable<TModel> _rows = items; private readonly IEnumerable<TModel> _rows = items;
@ -15,7 +16,12 @@ class ExportDataReader<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColu
private readonly TableExportOptions _options = options; private readonly TableExportOptions _options = options;
private readonly int _rowCount = items.Count(); private readonly int _rowCount = items.Count();
public object this[int i] /// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override object this[int i]
{ {
get get
{ {
@ -29,7 +35,12 @@ class ExportDataReader<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColu
} }
} }
public object this[string name] /// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public override object this[string name]
{ {
get get
{ {
@ -43,132 +54,23 @@ class ExportDataReader<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColu
} }
} }
public int Depth { get; } /// <summary>
/// <inheritdoc/>
public bool IsClosed { get; } /// </summary>
/// <param name="i"></param>
public int RecordsAffected { get; } /// <returns></returns>
public override string GetName(int i)
public int FieldCount { get; } = cols.Count();
public bool GetBoolean(int i)
{
throw new NotImplementedException();
}
public byte GetByte(int i)
{
throw new NotImplementedException();
}
public long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferOffset, int length)
{
throw new NotImplementedException();
}
public char GetChar(int i)
{
throw new NotImplementedException();
}
public long GetChars(int i, long fieldOffset, char[]? buffer, int bufferOffset, int length)
{
throw new NotImplementedException();
}
public IDataReader GetData(int i)
{
throw new NotImplementedException();
}
public string GetDataTypeName(int i)
{
throw new NotImplementedException();
}
public DateTime GetDateTime(int i)
{
throw new NotImplementedException();
}
public decimal GetDecimal(int i)
{
throw new NotImplementedException();
}
public double GetDouble(int i)
{
throw new NotImplementedException();
}
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)]
public Type GetFieldType(int i)
{
throw new NotImplementedException();
}
public float GetFloat(int i)
{
throw new NotImplementedException();
}
public Guid GetGuid(int i)
{
throw new NotImplementedException();
}
public short GetInt16(int i)
{
throw new NotImplementedException();
}
public int GetInt32(int i)
{
throw new NotImplementedException();
}
public long GetInt64(int i)
{
throw new NotImplementedException();
}
public int GetOrdinal(string name)
{
throw new NotImplementedException();
}
public DataTable? GetSchemaTable()
{
throw new NotImplementedException();
}
public string GetString(int i)
{
throw new NotImplementedException();
}
public int GetValues(object[] values)
{
throw new NotImplementedException();
}
public bool IsDBNull(int i)
{
throw new NotImplementedException();
}
public bool NextResult()
{
throw new NotImplementedException();
}
public string GetName(int i)
{ {
var col = _columns.ElementAtOrDefault(i); var col = _columns.ElementAtOrDefault(i);
return col?.GetDisplayName() ?? string.Empty; return col?.GetDisplayName() ?? string.Empty;
} }
public object GetValue(int i) /// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override object GetValue(int i)
{ {
object? v = null; object? v = null;
var row = _rows.ElementAtOrDefault(_rowIndex); var row = _rows.ElementAtOrDefault(_rowIndex);
@ -188,19 +90,13 @@ class ExportDataReader<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColu
return v!; return v!;
} }
public bool Read() /// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public override bool Read()
{ {
_rowIndex++; _rowIndex++;
return _rowIndex < _rowCount; return _rowIndex < _rowCount;
} }
public void Close()
{
}
public void Dispose()
{
}
} }