Skip to content

Commit 91b854f

Browse files
shps951023gitee-org
authored andcommitted
!8 Support QueryRange and upper Key name
Merge pull request !8 from 离歌笑/master
2 parents 65f6998 + 9257aa9 commit 91b854f

5 files changed

Lines changed: 584 additions & 29 deletions

File tree

src/MiniExcel/Csv/CsvReader.cs

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

src/MiniExcel/IExcelReader.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,15 @@ 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+
19+
//
20+
IEnumerable<IDictionary<string, object>> QueryRange(bool UseHeaderRow, string sheetName, string startCell, string endCell);
21+
IEnumerable<T> QueryRange<T>(string sheetName, string startCell, string endCell) where T : class, new();
22+
Task<IEnumerable<IDictionary<string, object>>> QueryAsyncRange(bool UseHeaderRow, string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default(CancellationToken));
23+
Task<IEnumerable<T>> QueryAsyncRange<T>(string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new();
24+
//
25+
26+
27+
1828
}
1929
}

src/MiniExcel/MiniExcel.cs

Lines changed: 41 additions & 6 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");
@@ -77,13 +77,15 @@ public static void SaveAs(this Stream stream, object value, bool printHeader = t
7777
}
7878
}
7979

80+
//1
8081
public static IEnumerable<dynamic> Query(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
8182
{
8283
using (var stream = FileHelper.OpenSharedRead(path))
8384
foreach (var item in Query(stream, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration))
8485
yield return item;
8586
}
8687

88+
//2
8789
public static IEnumerable<dynamic> Query(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
8890
{
8991
using (var excelReader = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration))
@@ -92,10 +94,42 @@ public static IEnumerable<dynamic> Query(this Stream stream, bool useHeaderRow =
9294
(dict, p) => { dict.Add(p); return dict; });
9395
}
9496

97+
#region range
98+
99+
//3
100+
/// <summary>
101+
///
102+
/// </summary>
103+
/// <param name="path">路径</param>
104+
/// <param name="useHeaderRow">表头</param>
105+
/// <param name="sheetName">表名称</param>
106+
/// <param name="excelType">excel类型</param>
107+
/// <param name="startCell">开始单元格,支持为空读所有,默认A1,或者B列,或者B2单元格</param>
108+
/// <param name="endCell">结束单元格,支持为空读所有,或者为D别,或者D2单元格</param>
109+
/// <param name="configuration">配置</param>
110+
/// <returns></returns>
111+
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)
112+
{
113+
using (var stream = FileHelper.OpenSharedRead(path))
114+
foreach (var item in QueryRange(stream, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell == "" ? "a1" : startCell, endCell, configuration))
115+
yield return item;
116+
}
117+
118+
//4
119+
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)
120+
{
121+
using (var excelReader = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration))
122+
foreach (var item in excelReader.QueryRange(useHeaderRow, sheetName, startCell == "" ? "a1" : startCell, endCell))
123+
yield return item.Aggregate(new ExpandoObject() as IDictionary<string, object>,
124+
(dict, p) => { dict.Add(p); return dict; });
125+
}
126+
127+
#endregion range
128+
95129
public static void SaveAsByTemplate(string path, string templatePath, object value, IConfiguration configuration = null)
96130
{
97131
using (var stream = File.Create(path))
98-
SaveAsByTemplate(stream, templatePath, value,configuration);
132+
SaveAsByTemplate(stream, templatePath, value, configuration);
99133
}
100134

101135
public static void SaveAsByTemplate(string path, byte[] templateBytes, object value, IConfiguration configuration = null)
@@ -122,17 +156,18 @@ public static DataTable QueryAsDataTable(string path, bool useHeaderRow = true,
122156
{
123157
using (var stream = FileHelper.OpenSharedRead(path))
124158
{
125-
return QueryAsDataTable(stream, useHeaderRow, sheetName, excelType:ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration);
159+
return QueryAsDataTable(stream, useHeaderRow, sheetName, excelType: ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration);
126160
}
127161
}
162+
128163
public static DataTable QueryAsDataTable(this Stream stream, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
129164
{
130165
if (sheetName == null && excelType != ExcelType.CSV) /*Issue #279*/
131166
sheetName = stream.GetSheetNames().First();
132167

133168
var dt = new DataTable(sheetName);
134169
var first = true;
135-
var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType),configuration).Query(useHeaderRow, sheetName, startCell);
170+
var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).Query(useHeaderRow, sheetName, startCell);
136171

137172
var keys = new List<string>();
138173
foreach (IDictionary<string, object> row in rows)
@@ -175,7 +210,7 @@ public static List<string> GetSheetNames(string path)
175210
public static List<string> GetSheetNames(this Stream stream)
176211
{
177212
var archive = new ExcelOpenXmlZip(stream);
178-
return new ExcelOpenXmlSheetReader(stream,null).GetWorkbookRels(archive.entries).Select(s => s.Name).ToList();
213+
return new ExcelOpenXmlSheetReader(stream, null).GetWorkbookRels(archive.entries).Select(s => s.Name).ToList();
179214
}
180215

181216
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 +252,4 @@ public static void ConvertXlsxToCsv(Stream xlsx, Stream csv)
217252
SaveAs(csv, value, printHeader: false, excelType: ExcelType.CSV);
218253
}
219254
}
220-
}
255+
}

0 commit comments

Comments
 (0)