Skip to content

Commit 0fd519c

Browse files
authored
Merge branch 'mini-software:master' into master
2 parents e04ef32 + d6da62a commit 0fd519c

18 files changed

Lines changed: 734 additions & 164 deletions

.github/workflows/dotnet.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ name: .NET
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ "master" ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ "master" ]
88

99
jobs:
1010
build:
1111

1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616
- name: Setup .NET
17-
uses: actions/setup-dotnet@v1
17+
uses: actions/setup-dotnet@v3
1818
with:
19-
dotnet-version: 5.0.x
19+
dotnet-version: 6.0.x
2020
- name: Restore dependencies
2121
run: dotnet restore
2222
- name: Build

docs/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@
2424

2525

2626

27-
## 1.28.0
27+
### 1.28.2
28+
29+
- [New] Support Assembly Strong Name Signature #450
30+
- [New] Support QueryRange (via @1ras1)
31+
32+
### 1.28.1
33+
34+
- [Optimization] Reduce string memory allocation when template save #439 (via @cupsos)
35+
- [Optimization] Remove dependency System.Memory #441 (via @ping9719)
36+
37+
### 1.28.0
2838

2939
- [New] Support CSV Insert #I4X92G (via @shps951023)
3040

docs/README.zh-CN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@
2727

2828

2929

30+
### 1.28.2
3031

32+
- [New] 支持 Assembly Strong Name Signature #450
33+
- [New] 支持 QueryRange (via @1ras1)
3134

32-
## 1.28.0
35+
### 1.28.1
36+
37+
- [Optimization] 减少 template save string memory allocation #439 (via @cupsos)
38+
- [Optimization] 移除 System.Memory 依赖 #441 (via @ping9719)
39+
40+
### 1.28.0
3341

3442
- [New] 支持 CSV Insert 方法 #I4X92G (via @shps951023)
3543

docs/README.zh-Hant.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@
2626

2727

2828

29-
## 1.28.0
29+
30+
31+
### 1.28.2
32+
33+
- [New] 支持 Assembly Strong Name Signature #450
34+
- [New] 支持 QueryRange (via @1ras1)
35+
36+
### 1.28.1
37+
38+
- [Optimization] 減少 template save string memory allocation #439 (via @cupsos)
39+
- [Optimization] 移除 System.Memory 依賴 #441 (via @ping9719)
40+
41+
### 1.28.0
3042

3143
- [New] 支持 CSV Insert 方法 #I4X92G (via @shps951023)
3244

src/MiniExcel/Csv/CsvReader.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,67 @@ private string[] Split(string row)
9191
public void Dispose()
9292
{
9393
}
94+
95+
#region Range
96+
public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, string sheetName, string startCell, string endCell)
97+
{
98+
if (startCell != "A1")
99+
throw new NotImplementedException("CSV not Implement startCell");
100+
if (_stream.CanSeek)
101+
_stream.Position = 0;
102+
var reader = _config.StreamReaderFunc(_stream);
103+
{
104+
var row = string.Empty;
105+
string[] read;
106+
var firstRow = true;
107+
Dictionary<int, string> headRows = new Dictionary<int, string>();
108+
while ((row = reader.ReadLine()) != null)
109+
{
110+
read = Split(row);
111+
112+
//header
113+
if (useHeaderRow)
114+
{
115+
if (firstRow)
116+
{
117+
firstRow = false;
118+
for (int i = 0; i <= read.Length - 1; i++)
119+
headRows.Add(i, read[i]);
120+
continue;
121+
}
122+
123+
var cell = CustomPropertyHelper.GetEmptyExpandoObject(headRows);
124+
for (int i = 0; i <= read.Length - 1; i++)
125+
cell[headRows[i]] = read[i];
126+
127+
yield return cell;
128+
continue;
129+
}
130+
131+
132+
//body
133+
{
134+
var cell = CustomPropertyHelper.GetEmptyExpandoObject(read.Length - 1, 0);
135+
for (int i = 0; i <= read.Length - 1; i++)
136+
cell[ColumnHelper.GetAlphabetColumnName(i)] = read[i];
137+
yield return cell;
138+
}
139+
}
140+
}
141+
}
142+
public IEnumerable<T> QueryRange<T>(string sheetName, string startCell, string endCel) where T : class, new()
143+
{
144+
return ExcelOpenXmlSheetReader.QueryImplRange<T>(QueryRange(false, sheetName, startCell, endCel), startCell, endCel, this._config);
145+
}
146+
public Task<IEnumerable<IDictionary<string, object>>> QueryAsyncRange(bool UseHeaderRow, string sheetName, string startCell, string endCel, CancellationToken cancellationToken = default(CancellationToken))
147+
{
148+
return Task.Run(() => QueryRange(UseHeaderRow, sheetName, startCell, endCel), cancellationToken);
149+
}
150+
151+
public Task<IEnumerable<T>> QueryAsyncRange<T>(string sheetName, string startCell, string endCel, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new()
152+
{
153+
return Task.Run(() => Query<T>(sheetName, startCell), cancellationToken);
154+
}
155+
#endregion
94156
}
95157
}

src/MiniExcel/Csv/CsvWriter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ internal class CsvWriter : IExcelWriter, IDisposable
2121
private object _value;
2222
private readonly StreamWriter _writer;
2323
private bool disposedValue;
24-
private object _insertValue;
2524

2625
public CsvWriter(Stream stream, object value, IConfiguration configuration, bool printHeader)
2726
{

src/MiniExcel/IExcelReader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ internal interface IExcelReader: IDisposable
1515
IEnumerable<T> Query<T>(string sheetName, string startCell) where T : class, new();
1616
Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool UseHeaderRow, string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken));
1717
Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken)) where T : class, new();
18+
IEnumerable<IDictionary<string, object>> QueryRange(bool UseHeaderRow, string sheetName, string startCell, string endCell);
19+
IEnumerable<T> QueryRange<T>(string sheetName, string startCell, string endCell) where T : class, new();
20+
Task<IEnumerable<IDictionary<string, object>>> QueryAsyncRange(bool UseHeaderRow, string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default(CancellationToken));
21+
Task<IEnumerable<T>> QueryAsyncRange<T>(string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new();
1822
}
1923
}

src/MiniExcel/MiniExcel.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void Insert(this Stream stream, object value, string sheetName = "
4747
ExcelWriterFactory.GetProvider(stream, v, sheetName, excelType, configuration, false).Insert();
4848
}
4949

50-
public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null,bool overwriteFile = false)
50+
public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, bool overwriteFile = false)
5151
{
5252
if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")
5353
throw new NotSupportedException("MiniExcel SaveAs not support xlsm");
@@ -76,14 +76,12 @@ public static void SaveAs(this Stream stream, object value, bool printHeader = t
7676
yield return item;
7777
}
7878
}
79-
8079
public static IEnumerable<dynamic> Query(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
8180
{
8281
using (var stream = FileHelper.OpenSharedRead(path))
8382
foreach (var item in Query(stream, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration))
8483
yield return item;
8584
}
86-
8785
public static IEnumerable<dynamic> Query(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
8886
{
8987
using (var excelReader = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration))
@@ -92,10 +90,29 @@ public static IEnumerable<dynamic> Query(this Stream stream, bool useHeaderRow =
9290
(dict, p) => { dict.Add(p); return dict; });
9391
}
9492

93+
#region range
94+
95+
public static IEnumerable<dynamic> QueryRange(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "a1", string endCell = "", IConfiguration configuration = null)
96+
{
97+
using (var stream = FileHelper.OpenSharedRead(path))
98+
foreach (var item in QueryRange(stream, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell == "" ? "a1" : startCell, endCell, configuration))
99+
yield return item;
100+
}
101+
102+
public static IEnumerable<dynamic> QueryRange(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "a1", string endCell = "", IConfiguration configuration = null)
103+
{
104+
using (var excelReader = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration))
105+
foreach (var item in excelReader.QueryRange(useHeaderRow, sheetName, startCell == "" ? "a1" : startCell, endCell))
106+
yield return item.Aggregate(new ExpandoObject() as IDictionary<string, object>,
107+
(dict, p) => { dict.Add(p); return dict; });
108+
}
109+
110+
#endregion range
111+
95112
public static void SaveAsByTemplate(string path, string templatePath, object value, IConfiguration configuration = null)
96113
{
97114
using (var stream = File.Create(path))
98-
SaveAsByTemplate(stream, templatePath, value,configuration);
115+
SaveAsByTemplate(stream, templatePath, value, configuration);
99116
}
100117

101118
public static void SaveAsByTemplate(string path, byte[] templateBytes, object value, IConfiguration configuration = null)
@@ -122,17 +139,18 @@ public static DataTable QueryAsDataTable(string path, bool useHeaderRow = true,
122139
{
123140
using (var stream = FileHelper.OpenSharedRead(path))
124141
{
125-
return QueryAsDataTable(stream, useHeaderRow, sheetName, excelType:ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration);
142+
return QueryAsDataTable(stream, useHeaderRow, sheetName, excelType: ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration);
126143
}
127144
}
145+
128146
public static DataTable QueryAsDataTable(this Stream stream, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
129147
{
130148
if (sheetName == null && excelType != ExcelType.CSV) /*Issue #279*/
131149
sheetName = stream.GetSheetNames().First();
132150

133151
var dt = new DataTable(sheetName);
134152
var first = true;
135-
var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType),configuration).Query(useHeaderRow, sheetName, startCell);
153+
var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).Query(useHeaderRow, sheetName, startCell);
136154

137155
var keys = new List<string>();
138156
foreach (IDictionary<string, object> row in rows)
@@ -175,7 +193,7 @@ public static List<string> GetSheetNames(string path)
175193
public static List<string> GetSheetNames(this Stream stream)
176194
{
177195
var archive = new ExcelOpenXmlZip(stream);
178-
return new ExcelOpenXmlSheetReader(stream,null).GetWorkbookRels(archive.entries).Select(s => s.Name).ToList();
196+
return new ExcelOpenXmlSheetReader(stream, null).GetWorkbookRels(archive.entries).Select(s => s.Name).ToList();
179197
}
180198

181199
public static ICollection<string> GetColumns(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
@@ -217,4 +235,4 @@ public static void ConvertXlsxToCsv(Stream xlsx, Stream csv)
217235
SaveAs(csv, value, printHeader: false, excelType: ExcelType.CSV);
218236
}
219237
}
220-
}
238+
}

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
4-
<Version>1.28.0</Version>
4+
<Version>1.28.5</Version>
55
</PropertyGroup>
66
<PropertyGroup>
77
<AssemblyName>MiniExcel</AssemblyName>
8+
<Company>Mini-Software</Company>
89
<Title>MiniExcel</Title>
910
<Product>MiniExcel</Product>
1011
<PackageTags>excel;xlsx;csv;micro-helper;mini;openxml;helper;</PackageTags>
11-
<Description>
12-
Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet
13-
14-
Github : https://github.com/mini-software/MiniExcel
15-
Gitee : https://gitee.com/dotnetchina/MiniExcel
16-
Issues : https://github.com/mini-software/MiniExcel/issues
17-
Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true
18-
</Description>
19-
<Authors>LIN,WEI-HAN</Authors>
12+
<Description>Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet
13+
Github : https://github.com/mini-software/MiniExcel
14+
Gitee : https://gitee.com/dotnetchina/MiniExcel
15+
Issues : https://github.com/mini-software/MiniExcel/issues
16+
Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true</Description>
17+
<Authors>LIN,WEI-HAN, Mini-Software team</Authors>
2018
<PackageId>MiniExcel</PackageId>
2119
<Copyright>LIN,WEI-HAN, 2021 onwards</Copyright>
2220
<NeutralLanguage>en</NeutralLanguage>
@@ -25,9 +23,16 @@
2523
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
2624
<PackageProjectUrl>https://github.com/mini-software/MiniExcel</PackageProjectUrl>
2725
<RepositoryUrl>https://github.com/mini-software/MiniExcel</RepositoryUrl>
26+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2827
<PackageIcon>icon.png</PackageIcon>
2928
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/mini-software/MiniExcel/tree/master/docs)</PackageReleaseNotes>
3029
<RepositoryType>Github</RepositoryType>
30+
<AssemblyOriginatorKeyFile>miniexcel.snk</AssemblyOriginatorKeyFile>
31+
<SignAssembly>True</SignAssembly>
32+
<ProjectGuid>{70FA8638-157A-4380-A4F6-8E2C3EE92CAE}</ProjectGuid>
33+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
34+
<IncludeSymbols>true</IncludeSymbols>
35+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
3136
</PropertyGroup>
3237
<ItemGroup Condition=" '$(TargetFramework)' == 'net461'">
3338
<Reference Include="System.IO.Compression" />
@@ -39,6 +44,9 @@
3944
<None Update="icon.png">
4045
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4146
</None>
47+
<None Update="miniexcel.snk">
48+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
49+
</None>
4250
</ItemGroup>
4351
<ItemGroup>
4452
<None Include="icon.png" Pack="true" PackagePath="\" />

0 commit comments

Comments
 (0)