Skip to content

Commit c46a03d

Browse files
committed
[New] Support GetReader method #328 #290
1 parent d016c2c commit c46a03d

File tree

10 files changed

+316
-1
lines changed

10 files changed

+316
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,25 @@ MiniExcel.SaveAs(path, value,excelType:ExcelType.CSV, configuration: config);
910910

911911

912912

913+
### DataReader
914+
915+
#### 1. GetReader
916+
Since 1.23.0, you can GetDataReader
917+
918+
```csharp
919+
using (var reader = MiniExcel.GetReader(path,true))
920+
{
921+
while (reader.Read())
922+
{
923+
for (int i = 0; i < reader.FieldCount; i++)
924+
{
925+
var value = reader.GetValue(i);
926+
}
927+
}
928+
}
929+
```
930+
931+
913932

914933
### Async
915934

@@ -993,6 +1012,8 @@ MiniExcel.Query(path, configuration: config);
9931012

9941013

9951014

1015+
1016+
9961017
### Examples:
9971018

9981019
#### 1. SQLite & Dapper `Large Size File` SQL Insert Avoid OOM

README.zh-CN.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,29 @@ MiniExcel.SaveAs(path, value,excelType:ExcelType.CSV, configuration: config);
914914

915915

916916

917+
### DataReader
918+
919+
#### 1. GetReader
920+
921+
从 1.23.0 版本开始能获取 DataReader
922+
923+
```csharp
924+
using (var reader = MiniExcel.GetReader(path,true))
925+
{
926+
while (reader.Read())
927+
{
928+
for (int i = 0; i < reader.FieldCount; i++)
929+
{
930+
var value = reader.GetValue(i);
931+
}
932+
}
933+
}
934+
```
935+
936+
937+
938+
939+
917940
### 异步 Async
918941

919942
从 v0.17.0 版本开始支持异步 (感谢[isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel))

README.zh-Hant.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,26 @@ MiniExcel.SaveAs(path, value,excelType:ExcelType.CSV, configuration: config);
922922
```
923923

924924

925+
### DataReader
926+
927+
#### 1. GetReader
928+
929+
从 1.23.0 版本开始能获取 DataReader
930+
931+
```csharp
932+
using (var reader = MiniExcel.GetReader(path,true))
933+
{
934+
while (reader.Read())
935+
{
936+
for (int i = 0; i < reader.FieldCount; i++)
937+
{
938+
var value = reader.GetValue(i);
939+
}
940+
}
941+
}
942+
```
943+
944+
925945

926946
### 異步 Async
927947

docs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
---
1818

19+
20+
### 1.23.0
21+
- [New] Support `GetReader` method #328 #290 (Thanks [杨福来 Yang](https://github.com/yfl8910) )
22+
23+
1924
### 1.22.0
25+
2026
- [New] SaveAs support to custom CultureInfo #316
2127
- [New] Query support to custom CultureInfo #316
2228
- [New] New efficiency byte array Converter #327

docs/README.zh-CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
---
2626

27+
### 1.23.0
28+
- [New] 新增 `GetReader` 方法 #328 #290 (感谢 [杨福来 Yang](https://github.com/yfl8910) )
29+
2730
### 1.22.0
2831
- [New] SaveAs 支持自定义 CultureInfo #316
2932
- [New] Query 支持自定义 CultureInfo #316

docs/README.zh-Hant.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717

1818
---
1919

20+
21+
22+
### 1.23.0
23+
- [New] 新增 `GetReader` 方法 #328 #290 (感謝 [楊福來 Yang](https://github.com/yfl8910) )
24+
2025
### 1.22.0
26+
2127
- [New] SaveAs 支持自定義 CultureInfo #316
2228
- [New] Query 支持自定義 CultureInfo #316
2329
- [New] 新 byte array 轉換器 #327

src/MiniExcel/MiniExcel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212

1313
public static partial class MiniExcel
1414
{
15+
public static MiniExcelDataReader GetReader(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
16+
{
17+
var stream = FileHelper.OpenSharedRead(path);
18+
return new MiniExcelDataReader(stream, useHeaderRow, sheetName, excelType, startCell, configuration);
19+
}
20+
21+
public static MiniExcelDataReader GetDataReader(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
22+
{
23+
return new MiniExcelDataReader(stream, useHeaderRow, sheetName, excelType, startCell, configuration);
24+
}
25+
1526
public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
1627
{
1728
if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")

src/MiniExcel/MiniExcelDataReader.cs

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
namespace MiniExcelLibs
2+
{
3+
using MiniExcelLibs.Utils;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Data;
7+
using System.IO;
8+
using System.Linq;
9+
10+
public class MiniExcelDataReader : IDataReader
11+
{
12+
private readonly IEnumerator<IDictionary<string, object>> _source;
13+
private readonly int _fieldCount;
14+
private readonly List<string> _keys;
15+
private readonly Stream _stream;
16+
private bool _isFirst = true;
17+
18+
internal MiniExcelDataReader(Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
19+
{
20+
_stream = stream;
21+
_source = MiniExcel.Query(_stream, useHeaderRow, sheetName, excelType, startCell, configuration).Cast<IDictionary<string, object>>().GetEnumerator();
22+
var isNext = _source.MoveNext();
23+
if (isNext)
24+
{
25+
_keys = _source.Current.Keys.ToList();
26+
_fieldCount = _keys.Count;
27+
}
28+
}
29+
30+
public void Dispose()
31+
{
32+
_stream.Dispose();
33+
}
34+
35+
public object GetValue(int i)
36+
{
37+
return _source.Current[_keys[i]];
38+
}
39+
40+
public int FieldCount
41+
{
42+
get { return _fieldCount; }
43+
}
44+
45+
public bool Read()
46+
{
47+
if (_isFirst)
48+
{
49+
_isFirst = false;
50+
return true;
51+
}
52+
return _source.MoveNext();
53+
}
54+
55+
public string GetName(int i)
56+
{
57+
return _keys[i];
58+
}
59+
60+
public int GetOrdinal(string name)
61+
{
62+
var i = _keys.IndexOf(name);
63+
return _keys.IndexOf(name);
64+
}
65+
66+
public void Close()
67+
{
68+
throw new NotImplementedException();
69+
}
70+
71+
public int Depth => throw new NotImplementedException();
72+
73+
public bool IsClosed => throw new NotImplementedException();
74+
75+
public int RecordsAffected => throw new NotImplementedException();
76+
77+
public object this[string name] => throw new NotImplementedException();
78+
79+
public object this[int i] => throw new NotImplementedException();
80+
81+
public DataTable GetSchemaTable()
82+
{
83+
throw new NotImplementedException();
84+
}
85+
86+
public bool NextResult()
87+
{
88+
throw new NotImplementedException();
89+
}
90+
91+
public bool GetBoolean(int i)
92+
{
93+
throw new NotImplementedException();
94+
}
95+
96+
public byte GetByte(int i)
97+
{
98+
throw new NotImplementedException();
99+
}
100+
101+
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
102+
{
103+
throw new NotImplementedException();
104+
}
105+
106+
public char GetChar(int i)
107+
{
108+
throw new NotImplementedException();
109+
}
110+
111+
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
112+
{
113+
throw new NotImplementedException();
114+
}
115+
116+
public IDataReader GetData(int i)
117+
{
118+
throw new NotImplementedException();
119+
}
120+
121+
public string GetDataTypeName(int i)
122+
{
123+
throw new NotImplementedException();
124+
}
125+
126+
public DateTime GetDateTime(int i)
127+
{
128+
throw new NotImplementedException();
129+
}
130+
131+
public decimal GetDecimal(int i)
132+
{
133+
throw new NotImplementedException();
134+
}
135+
136+
public double GetDouble(int i)
137+
{
138+
throw new NotImplementedException();
139+
}
140+
141+
public Type GetFieldType(int i)
142+
{
143+
throw new NotImplementedException();
144+
}
145+
146+
public float GetFloat(int i)
147+
{
148+
throw new NotImplementedException();
149+
}
150+
151+
public Guid GetGuid(int i)
152+
{
153+
throw new NotImplementedException();
154+
}
155+
156+
public short GetInt16(int i)
157+
{
158+
throw new NotImplementedException();
159+
}
160+
161+
public int GetInt32(int i)
162+
{
163+
throw new NotImplementedException();
164+
}
165+
166+
public long GetInt64(int i)
167+
{
168+
throw new NotImplementedException();
169+
}
170+
171+
public string GetString(int i)
172+
{
173+
throw new NotImplementedException();
174+
}
175+
176+
public int GetValues(object[] values)
177+
{
178+
throw new NotImplementedException();
179+
}
180+
181+
public bool IsDBNull(int i)
182+
{
183+
throw new NotImplementedException();
184+
}
185+
}
186+
}

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
4-
<Version>1.22.0</Version>
4+
<Version>1.23.0</Version>
55
</PropertyGroup>
66
<PropertyGroup>
77
<AssemblyName>MiniExcel</AssemblyName>

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@ public MiniExcelIssueTests(ITestOutputHelper output)
3131
this.output = output;
3232
}
3333

34+
[Fact]
35+
public void TestIssue328()
36+
{
37+
var path = PathHelper.GetTempFilePath();
38+
var value = new[] {
39+
new { id=1,name="Jack",indate=new DateTime(2022,5,13), file = File.ReadAllBytes(PathHelper.GetFile("images/TestIssue327.png")) },
40+
new { id=2,name="Henry",indate=new DateTime(2022,4,10), file = File.ReadAllBytes(PathHelper.GetFile("other/TestIssue327.txt")) },
41+
};
42+
MiniExcel.SaveAs(path, value);
43+
44+
var rowIndx = 0;
45+
using (var reader = MiniExcel.GetReader(path,true))
46+
{
47+
Assert.Equal("id", reader.GetName(0));
48+
Assert.Equal("name", reader.GetName(1));
49+
Assert.Equal("indate", reader.GetName(2));
50+
Assert.Equal("file", reader.GetName(3));
51+
52+
while (reader.Read())
53+
{
54+
for (int i = 0; i < reader.FieldCount; i++)
55+
{
56+
var v = reader.GetValue(i);
57+
if (rowIndx==0 && i==0) Assert.Equal((double)1,v);
58+
if (rowIndx == 0 && i == 1) Assert.Equal("Jack", v);
59+
if (rowIndx == 0 && i == 2) Assert.Equal(new DateTime(2022, 5, 13), v);
60+
if (rowIndx == 0 && i == 3) Assert.Equal(File.ReadAllBytes(PathHelper.GetFile("images/TestIssue327.png")), v);
61+
if (rowIndx == 1 && i == 0) Assert.Equal((double)2, v);
62+
if (rowIndx == 1 && i == 1) Assert.Equal("Henry", v);
63+
if (rowIndx == 1 && i == 2) Assert.Equal(new DateTime(2022, 4, 10), v);
64+
if (rowIndx == 1 && i == 3) Assert.Equal(File.ReadAllBytes(PathHelper.GetFile("other/TestIssue327.txt")), v);
65+
}
66+
rowIndx++;
67+
}
68+
}
69+
70+
//TODO:How to resolve empty body sheet?
71+
}
72+
3473
[Fact]
3574
public void TestIssue327()
3675
{

0 commit comments

Comments
 (0)