Skip to content

Commit 1b8708d

Browse files
committed
Merge branch 'master' of https://github.com/shps951023/MiniExcel
2 parents 6151c3f + b17506f commit 1b8708d

18 files changed

+751
-206
lines changed

README.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020

2121
---
2222

23-
24-
## 01/13 希望能更換掉民進黨
25-
26-
---
27-
2823
### Introduction
2924

3025
MiniExcel is simple and efficient to avoid OOM's .NET processing Excel tool.
@@ -1126,7 +1121,42 @@ Since V1.26.0, we can set the attributes of Column dynamically
11261121
```
11271122
![image](https://user-images.githubusercontent.com/12729184/164510353-5aecbc4e-c3ce-41e8-b6cf-afd55eb23b68.png)
11281123

1124+
#### 8. DynamicSheetAttribute
1125+
1126+
Since V1.31.4 we can set the attributes of Sheet dynamically. We can set sheet name and state (visibility).
1127+
```csharp
1128+
var configuration = new OpenXmlConfiguration
1129+
{
1130+
DynamicSheets = new DynamicExcelSheet[] {
1131+
new DynamicExcelSheet("usersSheet") { Name = "Users", State = SheetState.Visible },
1132+
new DynamicExcelSheet("departmentSheet") { Name = "Departments", State = SheetState.Hidden }
1133+
}
1134+
};
1135+
1136+
var users = new[] { new { Name = "Jack", Age = 25 }, new { Name = "Mike", Age = 44 } };
1137+
var department = new[] { new { ID = "01", Name = "HR" }, new { ID = "02", Name = "IT" } };
1138+
var sheets = new Dictionary<string, object>
1139+
{
1140+
["usersSheet"] = users,
1141+
["departmentSheet"] = department
1142+
};
11291143

1144+
var path = PathHelper.GetTempPath();
1145+
MiniExcel.SaveAs(path, sheets, configuration: configuration);
1146+
```
1147+
1148+
We can also use new attribute ExcelSheetAttribute:
1149+
1150+
```C#
1151+
[ExcelSheet(Name = "Departments", State = SheetState.Hidden)]
1152+
private class DepartmentDto
1153+
{
1154+
[ExcelColumn(Name = "ID",Index = 0)]
1155+
public string ID { get; set; }
1156+
[ExcelColumn(Name = "Name",Index = 1)]
1157+
public string Name { get; set; }
1158+
}
1159+
```
11301160

11311161
### Add, Delete, Update
11321162

@@ -1688,6 +1718,21 @@ foreach (var sheet in sheets)
16881718

16891719
![image](https://user-images.githubusercontent.com/12729184/116199841-2a1f5300-a76a-11eb-90a3-6710561cf6db.png)
16901720

1721+
#### Q. How to query or export information about sheet visibility?
1722+
1723+
A. `GetSheetInformations` method.
1724+
1725+
1726+
1727+
```csharp
1728+
var sheets = MiniExcel.GetSheetInformations(path);
1729+
foreach (var sheetInfo in sheets)
1730+
{
1731+
Console.WriteLine($"sheet index : {sheetInfo.Index} "); // next sheet index - numbered from 0
1732+
Console.WriteLine($"sheet name : {sheetInfo.Name} "); // sheet name
1733+
Console.WriteLine($"sheet state : {sheetInfo.State} "); // sheet visibility state - visible / hidden
1734+
}
1735+
```
16911736

16921737

16931738
#### Q. Whether to use Count will load all data into the memory?
@@ -1859,7 +1904,8 @@ Thanks for providing a free All product IDE for this project ([License](https://
18591904

18601905

18611906

1862-
1907+
### Benefit
1908+
Link https://github.com/mini-software/MiniExcel/issues/560#issue-2080619180
18631909

18641910
### Contributors
18651911

README.zh-CN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,10 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH
18101810

18111811

18121812

1813+
### 收益流水
1814+
目前收益 https://github.com/mini-software/MiniExcel/issues/560#issue-2080619180
1815+
1816+
18131817
### Contributors
18141818

18151819
![](https://contrib.rocks/image?repo=shps951023/MiniExcel)

README.zh-Hant.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,9 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH
17881788

17891789
感謝提供免費IDE支持此專案 ([License](https://user-images.githubusercontent.com/12729184/123988233-6ab17c00-d9fa-11eb-8739-2a08c6a4a263.png))
17901790

1791+
### 收益流水
1792+
目前收益 https://github.com/mini-software/MiniExcel/issues/560#issue-2080619180
1793+
17911794

17921795

17931796
### Contributors

samples/xlsx/TestDynamicSheet.xlsx

9.16 KB
Binary file not shown.
10.6 KB
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using MiniExcelLibs.OpenXml;
2+
using System;
3+
4+
namespace MiniExcelLibs.Attributes
5+
{
6+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
7+
public class ExcelSheetAttribute : Attribute
8+
{
9+
public string Name { get; set; }
10+
public SheetState State { get; set; } = SheetState.Visible;
11+
}
12+
13+
public class DynamicExcelSheet : ExcelSheetAttribute
14+
{
15+
public string Key { get; set; }
16+
public DynamicExcelSheet(string key)
17+
{
18+
Key = key;
19+
}
20+
}
21+
}

src/MiniExcel/MiniExcel.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
namespace MiniExcelLibs
22
{
33
using OpenXml;
4-
using Utils;
5-
using Zip;
64
using System;
75
using System.Collections;
86
using System.Collections.Generic;
97
using System.Data;
108
using System.Dynamic;
119
using System.IO;
1210
using System.Linq;
11+
using Utils;
12+
using Zip;
1313

1414
public static partial class MiniExcel
1515
{
@@ -147,7 +147,7 @@ public static void MergeSameCells(this Stream stream, byte[] filePath, ExcelType
147147
{
148148
ExcelTemplateFactory.GetProvider(stream, configuration, excelType).MergeSameCells(filePath);
149149
}
150-
150+
151151
#endregion
152152

153153
/// <summary>
@@ -217,6 +217,20 @@ public static List<string> GetSheetNames(this Stream stream, OpenXmlConfiguratio
217217
return new ExcelOpenXmlSheetReader(stream, config).GetWorkbookRels(archive.entries).Select(s => s.Name).ToList();
218218
}
219219

220+
public static List<SheetInfo> GetSheetInformations(string path, OpenXmlConfiguration config = null)
221+
{
222+
using (var stream = FileHelper.OpenSharedRead(path))
223+
return GetSheetInformations(stream, config);
224+
}
225+
226+
public static List<SheetInfo> GetSheetInformations(this Stream stream, OpenXmlConfiguration config = null)
227+
{
228+
config = config ?? OpenXmlConfiguration.DefaultConfig;
229+
230+
var archive = new ExcelOpenXmlZip(stream);
231+
return new ExcelOpenXmlSheetReader(stream, config).GetWorkbookRels(archive.entries).Select((s, i) => s.ToSheetInfo((uint)i)).ToList();
232+
}
233+
220234
public static ICollection<string> GetColumns(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
221235
{
222236
using (var stream = FileHelper.OpenSharedRead(path))

src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.IO;
99
using System.IO.Compression;
1010
using System.Linq;
11-
using System.Text;
1211
using System.Threading;
1312
using System.Threading.Tasks;
1413
using System.Xml;
@@ -25,7 +24,7 @@ internal class ExcelOpenXmlSheetReader : IExcelReader
2524
private MergeCells _mergeCells;
2625
private ExcelOpenXmlStyles _style;
2726
private readonly ExcelOpenXmlZip _archive;
28-
private OpenXmlConfiguration _config;
27+
private readonly OpenXmlConfiguration _config;
2928

3029
private static readonly XmlReaderSettings _xmlSettings = new XmlReaderSettings
3130
{
@@ -56,10 +55,19 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
5655
if (sheetName != null)
5756
{
5857
SetWorkbookRels(_archive.entries);
59-
var s = _sheetRecords.SingleOrDefault(_ => _.Name == sheetName);
60-
if (s == null)
58+
var sheetRecord = _sheetRecords.SingleOrDefault(_ => _.Name == sheetName);
59+
if (sheetRecord == null && _config.DynamicSheets != null)
60+
{
61+
var sheetConfig = _config.DynamicSheets.FirstOrDefault(ds => ds.Key == sheetName);
62+
if (sheetConfig != null)
63+
{
64+
sheetRecord = _sheetRecords.SingleOrDefault(_ => _.Name == sheetConfig.Name);
65+
}
66+
}
67+
if (sheetRecord == null)
6168
throw new InvalidOperationException("Please check sheetName/Index is correct");
62-
sheetEntry = sheets.Single(w => w.FullName == $"xl/{s.Path}" || w.FullName == $"/xl/{s.Path}" || w.FullName == s.Path || s.Path == $"/{w.FullName}");
69+
70+
sheetEntry = sheets.Single(w => w.FullName == $"xl/{sheetRecord.Path}" || w.FullName == $"/xl/{sheetRecord.Path}" || w.FullName == sheetRecord.Path || sheetRecord.Path == $"/{w.FullName}");
6371
}
6472
else if (sheets.Count() > 1)
6573
{
@@ -402,6 +410,14 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
402410

403411
public IEnumerable<T> Query<T>(string sheetName, string startCell) where T : class, new()
404412
{
413+
if (sheetName == null)
414+
{
415+
var sheetInfo = CustomPropertyHelper.GetExcellSheetInfo(typeof(T), this._config);
416+
if (sheetInfo != null)
417+
{
418+
sheetName = sheetInfo.ExcelSheetName;
419+
}
420+
}
405421
return ExcelOpenXmlSheetReader.QueryImpl<T>(Query(false, sheetName, startCell), startCell, this._config);
406422
}
407423

@@ -562,6 +578,7 @@ internal IEnumerable<SheetRecord> ReadWorkbook(ReadOnlyCollection<ZipArchiveEntr
562578
{
563579
yield return new SheetRecord(
564580
reader.GetAttribute("name"),
581+
reader.GetAttribute("state"),
565582
uint.Parse(reader.GetAttribute("sheetId")),
566583
XmlReaderHelper.GetAttribute(reader, "id", _relationshiopNs)
567584
);

0 commit comments

Comments
 (0)