Skip to content

Commit

Permalink
0.10.1
Browse files Browse the repository at this point in the history
- [New] SaveAs support POCO excel column name/ignore attribute
  • Loading branch information
shps951023 committed Apr 4, 2021
1 parent 77b2dc6 commit 740d28b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 31 deletions.
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

## Release Notes



### 0.10.1

- [New] SaveAs support POCO excel column name/ignore attribute

### 0.10.0

- [New] Query dynamic with first head will ignore blank/whitespace columns
- [New] Query type mapping support Custom POCO excel column name/ignore attribute

Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/Csv/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public IEnumerable<IDictionary<string, object>> Query(Stream stream, bool useHea
public IEnumerable<T> Query<T>(Stream stream) where T : class, new()
{
var type = typeof(T);
var props = Helpers.GetProperties(type);
var props = Helpers.GetSaveAsProperties(type);
Dictionary<int, PropertyInfo> idxProps = new Dictionary<int, PropertyInfo>();
var configuration = new CsvConfiguration();
using (var reader = configuration.GetStreamReaderFunc(stream))
Expand All @@ -76,9 +76,9 @@ public IEnumerable<IDictionary<string, object>> Query(Stream stream, bool useHea
var index = 0;
foreach (var v in read)
{
var p = props.SingleOrDefault(w => w.Name == v);
var p = props.SingleOrDefault(w => w.ExcelColumnName == v);
if (p != null)
idxProps.Add(index, p);
idxProps.Add(index, p.Property);
index++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/MiniExcel/MiniExcelLibs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Product>MiniExcel</Product>
<PackageTags>excel;xlsx;micro-helper;mini;openxml;helper;</PackageTags>
<Description>
A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without third party dependencies to create or dynamic/type POCO mapping query etc..
A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without third-party dependencies to create or dynamic/type POCO mapping query etc..

Github : https://github.com/shps951023/MiniExcel
Issues : https://github.com/shps951023/MiniExcel/issues
Expand All @@ -19,7 +19,7 @@
<RepositoryUrl>https://github.com/shps951023/MiniExcel</RepositoryUrl>
<PackageIconUrl>https://raw.githubusercontent.com/shps951023/ImageHosting/master/img/2019-01-17.13.18.32-image.png</PackageIconUrl>
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
<Version>0.10.0</Version>
<Version>0.10.1</Version>
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/shps951023/MiniExcel/tree/master/docs)</PackageReleaseNotes>
<RepositoryType>Github</RepositoryType>
</PropertyGroup>
Expand Down
16 changes: 7 additions & 9 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using static MiniExcelLibs.Utils.Helpers;

namespace MiniExcelLibs.OpenXml
{
Expand Down Expand Up @@ -50,7 +51,7 @@ public void SaveAs(Stream stream, object value)

var maxColumnIndex = 0;
List<object> keys = new List<object>();
PropertyInfo[] props = null;
List<ExcelCustomPropertyInfo> props = null;
string mode = null;

{
Expand Down Expand Up @@ -79,11 +80,8 @@ public void SaveAs(Stream stream, object value)
{
mode = "Properties";
genericType = item.GetType();
props = Helpers.GetProperties(genericType);
//props = genericType.GetProperties();
if (props.Length == 0)
throw new InvalidOperationException($"Generic Type : {genericType} valid properties count is 0, if you have trouble please issue for me.");
maxColumnIndex = props.Length;
props = Helpers.GetSaveAsProperties(genericType);
maxColumnIndex = props.Count;
}

// not re-foreach key point
Expand Down Expand Up @@ -122,7 +120,7 @@ public void SaveAs(Stream stream, object value)
foreach (var p in props)
{
var columname = ExcelOpenXmlUtils.ConvertXyToCell(cellIndex, yIndex);
writer.Write($"<x:c r=\"{columname}\" t=\"str\"><x:v>{p.Name}</x:v></x:c>");
writer.Write($"<x:c r=\"{columname}\" t=\"str\"><x:v>{p.ExcelColumnName}</x:v></x:c>");
cellIndex++;
}
}
Expand Down Expand Up @@ -248,7 +246,7 @@ internal void GenerateSheetByIDictionary(StreamWriter writer, ZipArchive archive
}
}

internal void GenerateSheetByProperties(StreamWriter writer, ZipArchive archive, IEnumerable value, Type genericType, PropertyInfo[] props, int rowCount, List<object> keys, int xIndex = 1, int yIndex = 1)
internal void GenerateSheetByProperties(StreamWriter writer, ZipArchive archive, IEnumerable value, Type genericType, List<ExcelCustomPropertyInfo> props, int rowCount, List<object> keys, int xIndex = 1, int yIndex = 1)
{
//body
foreach (var v in value)
Expand All @@ -257,7 +255,7 @@ internal void GenerateSheetByProperties(StreamWriter writer, ZipArchive archive,
var cellIndex = xIndex;
foreach (var p in props)
{
var cellValue = p.GetValue(v);
var cellValue = p.Property.GetValue(v);
var cellValueStr = ExcelOpenXmlUtils.EncodeXML(cellValue);
var t = "t=\"str\"";
{
Expand Down
26 changes: 13 additions & 13 deletions src/MiniExcel/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ static Helpers()
}
}

public static string GetAlphabetColumnName(int ColumnIndex) => _IntMappingAlphabet[ColumnIndex];
public static int GetColumnIndex(string columnName) => _AlphabetMappingInt[columnName];
internal static string GetAlphabetColumnName(int ColumnIndex) => _IntMappingAlphabet[ColumnIndex];
internal static int GetColumnIndex(string columnName) => _AlphabetMappingInt[columnName];

internal static string IntToLetters(int value)
{
Expand All @@ -37,7 +37,7 @@ internal static string IntToLetters(int value)
return result;
}

public static IDictionary<string, object> GetEmptyExpandoObject(int maxColumnIndex)
internal static IDictionary<string, object> GetEmptyExpandoObject(int maxColumnIndex)
{
// TODO: strong type mapping can ignore this
// TODO: it can recode better performance
Expand All @@ -51,7 +51,7 @@ public static IDictionary<string, object> GetEmptyExpandoObject(int maxColumnInd
return cell;
}

public static IDictionary<string, object> GetEmptyExpandoObject(Dictionary<int, string> hearrows)
internal static IDictionary<string, object> GetEmptyExpandoObject(Dictionary<int, string> hearrows)
{
// TODO: strong type mapping can ignore this
// TODO: it can recode better performance
Expand All @@ -62,11 +62,16 @@ public static IDictionary<string, object> GetEmptyExpandoObject(Dictionary<int,
return cell;
}

public static PropertyInfo[] GetProperties(this Type type)
internal static List<ExcelCustomPropertyInfo> GetSaveAsProperties(this Type type)
{
return type.GetProperties(
BindingFlags.Public |
BindingFlags.Instance);
List<ExcelCustomPropertyInfo> props = GetExcelPropertyInfo(type, BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.Property.GetGetMethod() != null && !prop.Property.GetAttributeValue((ExcelIgnoreAttribute x) => x.ExcelIgnore))
.ToList() /*ignore without set*/;

if (props.Count == 0)
throw new InvalidOperationException($"{type.Name} un-ignore properties count can't be 0");

return props;
}

internal class ExcelCustomPropertyInfo
Expand Down Expand Up @@ -108,11 +113,6 @@ private static IEnumerable<ExcelCustomPropertyInfo> GetExcelPropertyInfo(Type ty
});
}

internal static bool IsDapperRows<T>()
{
return typeof(IDictionary<string, object>).IsAssignableFrom(typeof(T));
}

private static readonly Regex EscapeRegex = new Regex("_x([0-9A-F]{4,4})_");
public static string ConvertEscapeChars(string input)
{
Expand Down
36 changes: 32 additions & 4 deletions tests/MiniExcelTests/MiniExcelOpenXmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CustomAttributeWihoutVaildPropertiesTest()
}

[Fact]
public void CustomAttributesTest()
public void QueryCustomAttributesTest()
{
var path = @"..\..\..\..\..\samples\xlsx\TestCustomExcelColumnAttribute.xlsx";
var rows = MiniExcel.Query<ExcelAttributeDemo>(path).ToList();
Expand All @@ -42,6 +42,34 @@ public void CustomAttributesTest()
Assert.Null(rows[0].Test6);
}

[Fact]
public void SaveAsCustomAttributesTest()
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
var input = Enumerable.Range(1, 3).Select(
s => new ExcelAttributeDemo
{
Test1 = "Test1",
Test2 = "Test2",
Test3 = "Test3",
Test4 = "Test4",
}
);
MiniExcel.SaveAs(path, input);
{
var rows = MiniExcel.Query(path,true).ToList();
var first = rows[0] as IDictionary<string, object>;
Assert.Equal(new[] { "Column1", "Column2", "Test4", "Test5", "Test6" },first.Keys);
Assert.Equal("Test1", rows[0].Column1);
Assert.Equal("Test2", rows[0].Column2);
Assert.Equal("Test4", rows[0].Test4);
Assert.Null(rows[0].Test5);
Assert.Null(rows[0].Test6);

Assert.Equal(3,rows.Count);
}
}

public class CustomAttributesWihoutVaildPropertiesTestPoco
{
[ExcelIgnore]
Expand Down Expand Up @@ -173,7 +201,7 @@ public void TestDynamicQueryBasic_useHeaderRow()
}

{
var rows = MiniExcel.Query(path,useHeaderRow: true).ToList();
var rows = MiniExcel.Query(path, useHeaderRow: true).ToList();

Assert.Equal("MiniExcel", rows[0].Column1);
Assert.Equal(1, rows[0].Column2);
Expand Down Expand Up @@ -715,7 +743,7 @@ public void SaveAsByIEnumerableIDictionary()
using (var stream = File.OpenRead(path))
{
var rows = stream.Query(useHeaderRow: false).ToList();
Assert.Equal(3,rows.Count);
Assert.Equal(3, rows.Count);
}

Assert.Equal("A1:B3", Helpers.GetFirstSheetDimensionRefValue(path));
Expand Down Expand Up @@ -912,7 +940,7 @@ public void SaveAsBasicCreateTest()
new { Column1 = "MiniExcel", Column2 = 1 },
new { Column1 = "Github", Column2 = 2}
});

using (var stream = File.OpenRead(path))
{
var rows = stream.Query(useHeaderRow: true).ToList();
Expand Down

0 comments on commit 740d28b

Please sign in to comment.