Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Mar 6, 2024
2 parents 6151c3f + b17506f commit 1b8708d
Show file tree
Hide file tree
Showing 18 changed files with 751 additions and 206 deletions.
58 changes: 52 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@

---


## 01/13 希望能更換掉民進黨

---

### Introduction

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

#### 8. DynamicSheetAttribute

Since V1.31.4 we can set the attributes of Sheet dynamically. We can set sheet name and state (visibility).
```csharp
var configuration = new OpenXmlConfiguration
{
DynamicSheets = new DynamicExcelSheet[] {
new DynamicExcelSheet("usersSheet") { Name = "Users", State = SheetState.Visible },
new DynamicExcelSheet("departmentSheet") { Name = "Departments", State = SheetState.Hidden }
}
};

var users = new[] { new { Name = "Jack", Age = 25 }, new { Name = "Mike", Age = 44 } };
var department = new[] { new { ID = "01", Name = "HR" }, new { ID = "02", Name = "IT" } };
var sheets = new Dictionary<string, object>
{
["usersSheet"] = users,
["departmentSheet"] = department
};

var path = PathHelper.GetTempPath();
MiniExcel.SaveAs(path, sheets, configuration: configuration);
```

We can also use new attribute ExcelSheetAttribute:

```C#
[ExcelSheet(Name = "Departments", State = SheetState.Hidden)]
private class DepartmentDto
{
[ExcelColumn(Name = "ID",Index = 0)]
public string ID { get; set; }
[ExcelColumn(Name = "Name",Index = 1)]
public string Name { get; set; }
}
```

### Add, Delete, Update

Expand Down Expand Up @@ -1688,6 +1718,21 @@ foreach (var sheet in sheets)

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

#### Q. How to query or export information about sheet visibility?

A. `GetSheetInformations` method.



```csharp
var sheets = MiniExcel.GetSheetInformations(path);
foreach (var sheetInfo in sheets)
{
Console.WriteLine($"sheet index : {sheetInfo.Index} "); // next sheet index - numbered from 0
Console.WriteLine($"sheet name : {sheetInfo.Name} "); // sheet name
Console.WriteLine($"sheet state : {sheetInfo.State} "); // sheet visibility state - visible / hidden
}
```


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




### Benefit
Link https://github.com/mini-software/MiniExcel/issues/560#issue-2080619180

### Contributors

Expand Down
4 changes: 4 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,10 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH



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


### Contributors

![](https://contrib.rocks/image?repo=shps951023/MiniExcel)
Expand Down
3 changes: 3 additions & 0 deletions README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,9 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH

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

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



### Contributors
Expand Down
Binary file added samples/xlsx/TestDynamicSheet.xlsx
Binary file not shown.
Binary file added samples/xlsx/TestMultiSheetWithHiddenSheet.xlsx
Binary file not shown.
21 changes: 21 additions & 0 deletions src/MiniExcel/Attributes/ExcelSheetAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MiniExcelLibs.OpenXml;
using System;

namespace MiniExcelLibs.Attributes
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExcelSheetAttribute : Attribute
{
public string Name { get; set; }
public SheetState State { get; set; } = SheetState.Visible;
}

public class DynamicExcelSheet : ExcelSheetAttribute
{
public string Key { get; set; }
public DynamicExcelSheet(string key)
{
Key = key;
}
}
}
20 changes: 17 additions & 3 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace MiniExcelLibs
{
using OpenXml;
using Utils;
using Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq;
using Utils;
using Zip;

public static partial class MiniExcel
{
Expand Down Expand Up @@ -147,7 +147,7 @@ public static void MergeSameCells(this Stream stream, byte[] filePath, ExcelType
{
ExcelTemplateFactory.GetProvider(stream, configuration, excelType).MergeSameCells(filePath);
}

#endregion

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

public static List<SheetInfo> GetSheetInformations(string path, OpenXmlConfiguration config = null)
{
using (var stream = FileHelper.OpenSharedRead(path))
return GetSheetInformations(stream, config);
}

public static List<SheetInfo> GetSheetInformations(this Stream stream, OpenXmlConfiguration config = null)
{
config = config ?? OpenXmlConfiguration.DefaultConfig;

var archive = new ExcelOpenXmlZip(stream);
return new ExcelOpenXmlSheetReader(stream, config).GetWorkbookRels(archive.entries).Select((s, i) => s.ToSheetInfo((uint)i)).ToList();
}

public static ICollection<string> GetColumns(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
{
using (var stream = FileHelper.OpenSharedRead(path))
Expand Down
27 changes: 22 additions & 5 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
Expand All @@ -25,7 +24,7 @@ internal class ExcelOpenXmlSheetReader : IExcelReader
private MergeCells _mergeCells;
private ExcelOpenXmlStyles _style;
private readonly ExcelOpenXmlZip _archive;
private OpenXmlConfiguration _config;
private readonly OpenXmlConfiguration _config;

private static readonly XmlReaderSettings _xmlSettings = new XmlReaderSettings
{
Expand Down Expand Up @@ -56,10 +55,19 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
if (sheetName != null)
{
SetWorkbookRels(_archive.entries);
var s = _sheetRecords.SingleOrDefault(_ => _.Name == sheetName);
if (s == null)
var sheetRecord = _sheetRecords.SingleOrDefault(_ => _.Name == sheetName);
if (sheetRecord == null && _config.DynamicSheets != null)
{
var sheetConfig = _config.DynamicSheets.FirstOrDefault(ds => ds.Key == sheetName);
if (sheetConfig != null)
{
sheetRecord = _sheetRecords.SingleOrDefault(_ => _.Name == sheetConfig.Name);
}
}
if (sheetRecord == null)
throw new InvalidOperationException("Please check sheetName/Index is correct");
sheetEntry = sheets.Single(w => w.FullName == $"xl/{s.Path}" || w.FullName == $"/xl/{s.Path}" || w.FullName == s.Path || s.Path == $"/{w.FullName}");

sheetEntry = sheets.Single(w => w.FullName == $"xl/{sheetRecord.Path}" || w.FullName == $"/xl/{sheetRecord.Path}" || w.FullName == sheetRecord.Path || sheetRecord.Path == $"/{w.FullName}");
}
else if (sheets.Count() > 1)
{
Expand Down Expand Up @@ -402,6 +410,14 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di

public IEnumerable<T> Query<T>(string sheetName, string startCell) where T : class, new()
{
if (sheetName == null)
{
var sheetInfo = CustomPropertyHelper.GetExcellSheetInfo(typeof(T), this._config);
if (sheetInfo != null)
{
sheetName = sheetInfo.ExcelSheetName;
}
}
return ExcelOpenXmlSheetReader.QueryImpl<T>(Query(false, sheetName, startCell), startCell, this._config);
}

Expand Down Expand Up @@ -562,6 +578,7 @@ internal IEnumerable<SheetRecord> ReadWorkbook(ReadOnlyCollection<ZipArchiveEntr
{
yield return new SheetRecord(
reader.GetAttribute("name"),
reader.GetAttribute("state"),
uint.Parse(reader.GetAttribute("sheetId")),
XmlReaderHelper.GetAttribute(reader, "id", _relationshiopNs)
);
Expand Down
Loading

0 comments on commit 1b8708d

Please sign in to comment.