Skip to content

Commit 740d28b

Browse files
committed
0.10.1
- [New] SaveAs support POCO excel column name/ignore attribute
1 parent 77b2dc6 commit 740d28b

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

docs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
## Release Notes
44

5+
6+
7+
### 0.10.1
8+
9+
- [New] SaveAs support POCO excel column name/ignore attribute
10+
511
### 0.10.0
12+
613
- [New] Query dynamic with first head will ignore blank/whitespace columns
714
- [New] Query type mapping support Custom POCO excel column name/ignore attribute
815

src/MiniExcel/Csv/CsvReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public IEnumerable<IDictionary<string, object>> Query(Stream stream, bool useHea
5959
public IEnumerable<T> Query<T>(Stream stream) where T : class, new()
6060
{
6161
var type = typeof(T);
62-
var props = Helpers.GetProperties(type);
62+
var props = Helpers.GetSaveAsProperties(type);
6363
Dictionary<int, PropertyInfo> idxProps = new Dictionary<int, PropertyInfo>();
6464
var configuration = new CsvConfiguration();
6565
using (var reader = configuration.GetStreamReaderFunc(stream))
@@ -76,9 +76,9 @@ public IEnumerable<IDictionary<string, object>> Query(Stream stream, bool useHea
7676
var index = 0;
7777
foreach (var v in read)
7878
{
79-
var p = props.SingleOrDefault(w => w.Name == v);
79+
var p = props.SingleOrDefault(w => w.ExcelColumnName == v);
8080
if (p != null)
81-
idxProps.Add(index, p);
81+
idxProps.Add(index, p.Property);
8282
index++;
8383
}
8484
}

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Product>MiniExcel</Product>
66
<PackageTags>excel;xlsx;micro-helper;mini;openxml;helper;</PackageTags>
77
<Description>
8-
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..
8+
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..
99

1010
Github : https://github.com/shps951023/MiniExcel
1111
Issues : https://github.com/shps951023/MiniExcel/issues
@@ -19,7 +19,7 @@
1919
<RepositoryUrl>https://github.com/shps951023/MiniExcel</RepositoryUrl>
2020
<PackageIconUrl>https://raw.githubusercontent.com/shps951023/ImageHosting/master/img/2019-01-17.13.18.32-image.png</PackageIconUrl>
2121
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
22-
<Version>0.10.0</Version>
22+
<Version>0.10.1</Version>
2323
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/shps951023/MiniExcel/tree/master/docs)</PackageReleaseNotes>
2424
<RepositoryType>Github</RepositoryType>
2525
</PropertyGroup>

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Reflection;
1111
using System.Text;
12+
using static MiniExcelLibs.Utils.Helpers;
1213

1314
namespace MiniExcelLibs.OpenXml
1415
{
@@ -50,7 +51,7 @@ public void SaveAs(Stream stream, object value)
5051

5152
var maxColumnIndex = 0;
5253
List<object> keys = new List<object>();
53-
PropertyInfo[] props = null;
54+
List<ExcelCustomPropertyInfo> props = null;
5455
string mode = null;
5556

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

8987
// not re-foreach key point
@@ -122,7 +120,7 @@ public void SaveAs(Stream stream, object value)
122120
foreach (var p in props)
123121
{
124122
var columname = ExcelOpenXmlUtils.ConvertXyToCell(cellIndex, yIndex);
125-
writer.Write($"<x:c r=\"{columname}\" t=\"str\"><x:v>{p.Name}</x:v></x:c>");
123+
writer.Write($"<x:c r=\"{columname}\" t=\"str\"><x:v>{p.ExcelColumnName}</x:v></x:c>");
126124
cellIndex++;
127125
}
128126
}
@@ -248,7 +246,7 @@ internal void GenerateSheetByIDictionary(StreamWriter writer, ZipArchive archive
248246
}
249247
}
250248

251-
internal void GenerateSheetByProperties(StreamWriter writer, ZipArchive archive, IEnumerable value, Type genericType, PropertyInfo[] props, int rowCount, List<object> keys, int xIndex = 1, int yIndex = 1)
249+
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)
252250
{
253251
//body
254252
foreach (var v in value)
@@ -257,7 +255,7 @@ internal void GenerateSheetByProperties(StreamWriter writer, ZipArchive archive,
257255
var cellIndex = xIndex;
258256
foreach (var p in props)
259257
{
260-
var cellValue = p.GetValue(v);
258+
var cellValue = p.Property.GetValue(v);
261259
var cellValueStr = ExcelOpenXmlUtils.EncodeXML(cellValue);
262260
var t = "t=\"str\"";
263261
{

src/MiniExcel/Utils/Helpers.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ static Helpers()
2222
}
2323
}
2424

25-
public static string GetAlphabetColumnName(int ColumnIndex) => _IntMappingAlphabet[ColumnIndex];
26-
public static int GetColumnIndex(string columnName) => _AlphabetMappingInt[columnName];
25+
internal static string GetAlphabetColumnName(int ColumnIndex) => _IntMappingAlphabet[ColumnIndex];
26+
internal static int GetColumnIndex(string columnName) => _AlphabetMappingInt[columnName];
2727

2828
internal static string IntToLetters(int value)
2929
{
@@ -37,7 +37,7 @@ internal static string IntToLetters(int value)
3737
return result;
3838
}
3939

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

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

65-
public static PropertyInfo[] GetProperties(this Type type)
65+
internal static List<ExcelCustomPropertyInfo> GetSaveAsProperties(this Type type)
6666
{
67-
return type.GetProperties(
68-
BindingFlags.Public |
69-
BindingFlags.Instance);
67+
List<ExcelCustomPropertyInfo> props = GetExcelPropertyInfo(type, BindingFlags.Public | BindingFlags.Instance)
68+
.Where(prop => prop.Property.GetGetMethod() != null && !prop.Property.GetAttributeValue((ExcelIgnoreAttribute x) => x.ExcelIgnore))
69+
.ToList() /*ignore without set*/;
70+
71+
if (props.Count == 0)
72+
throw new InvalidOperationException($"{type.Name} un-ignore properties count can't be 0");
73+
74+
return props;
7075
}
7176

7277
internal class ExcelCustomPropertyInfo
@@ -108,11 +113,6 @@ private static IEnumerable<ExcelCustomPropertyInfo> GetExcelPropertyInfo(Type ty
108113
});
109114
}
110115

111-
internal static bool IsDapperRows<T>()
112-
{
113-
return typeof(IDictionary<string, object>).IsAssignableFrom(typeof(T));
114-
}
115-
116116
private static readonly Regex EscapeRegex = new Regex("_x([0-9A-F]{4,4})_");
117117
public static string ConvertEscapeChars(string input)
118118
{

tests/MiniExcelTests/MiniExcelOpenXmlTests.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void CustomAttributeWihoutVaildPropertiesTest()
2929
}
3030

3131
[Fact]
32-
public void CustomAttributesTest()
32+
public void QueryCustomAttributesTest()
3333
{
3434
var path = @"..\..\..\..\..\samples\xlsx\TestCustomExcelColumnAttribute.xlsx";
3535
var rows = MiniExcel.Query<ExcelAttributeDemo>(path).ToList();
@@ -42,6 +42,34 @@ public void CustomAttributesTest()
4242
Assert.Null(rows[0].Test6);
4343
}
4444

45+
[Fact]
46+
public void SaveAsCustomAttributesTest()
47+
{
48+
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
49+
var input = Enumerable.Range(1, 3).Select(
50+
s => new ExcelAttributeDemo
51+
{
52+
Test1 = "Test1",
53+
Test2 = "Test2",
54+
Test3 = "Test3",
55+
Test4 = "Test4",
56+
}
57+
);
58+
MiniExcel.SaveAs(path, input);
59+
{
60+
var rows = MiniExcel.Query(path,true).ToList();
61+
var first = rows[0] as IDictionary<string, object>;
62+
Assert.Equal(new[] { "Column1", "Column2", "Test4", "Test5", "Test6" },first.Keys);
63+
Assert.Equal("Test1", rows[0].Column1);
64+
Assert.Equal("Test2", rows[0].Column2);
65+
Assert.Equal("Test4", rows[0].Test4);
66+
Assert.Null(rows[0].Test5);
67+
Assert.Null(rows[0].Test6);
68+
69+
Assert.Equal(3,rows.Count);
70+
}
71+
}
72+
4573
public class CustomAttributesWihoutVaildPropertiesTestPoco
4674
{
4775
[ExcelIgnore]
@@ -173,7 +201,7 @@ public void TestDynamicQueryBasic_useHeaderRow()
173201
}
174202

175203
{
176-
var rows = MiniExcel.Query(path,useHeaderRow: true).ToList();
204+
var rows = MiniExcel.Query(path, useHeaderRow: true).ToList();
177205

178206
Assert.Equal("MiniExcel", rows[0].Column1);
179207
Assert.Equal(1, rows[0].Column2);
@@ -715,7 +743,7 @@ public void SaveAsByIEnumerableIDictionary()
715743
using (var stream = File.OpenRead(path))
716744
{
717745
var rows = stream.Query(useHeaderRow: false).ToList();
718-
Assert.Equal(3,rows.Count);
746+
Assert.Equal(3, rows.Count);
719747
}
720748

721749
Assert.Equal("A1:B3", Helpers.GetFirstSheetDimensionRefValue(path));
@@ -912,7 +940,7 @@ public void SaveAsBasicCreateTest()
912940
new { Column1 = "MiniExcel", Column2 = 1 },
913941
new { Column1 = "Github", Column2 = 2}
914942
});
915-
943+
916944
using (var stream = File.OpenRead(path))
917945
{
918946
var rows = stream.Query(useHeaderRow: true).ToList();

0 commit comments

Comments
 (0)