Skip to content

Commit

Permalink
add QueryRange test and docs.(#634) (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaolipro authored Jul 19, 2024
1 parent 7ecf0ff commit 9de96ab
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ foreach(IDictionary<string,object> row in MiniExcel.Query(path))

// or
var rows = MiniExcel.Query(path).Cast<IDictionary<string,object>>();
// or Query specified ranges (capitalized)
// A2 represents the second row of column A, C3 represents the third row of column C
// If you don't want to restrict rows, just don't include numbers
var rows = MiniExcel.QueryRange(path, startCell: "A2", endCell: "C3").Cast<IDictionary<string, object>>();
```


Expand Down
4 changes: 4 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ foreach(IDictionary<string,object> row in MiniExcel.Query(path))

// or
var rows = MiniExcel.Query(path).Cast<IDictionary<string,object>>();
// or 查询指定范围(要大写才生效哦)
// A2(左上角)代表A列的第二行,C3(右下角)代表C列的第三行
// 如果你不想限制行,就不要包含数字
var rows = MiniExcel.QueryRange(path, startCell: "A2", endCell: "C3").Cast<IDictionary<string, object>>();
```

#### 9. Query 读 Excel 返回 DataTable
Expand Down
4 changes: 4 additions & 0 deletions README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ foreach(IDictionary<string,object> row in MiniExcel.Query(path))

// or
var rows = MiniExcel.Query(path).Cast<IDictionary<string,object>>();
// or 査詢指定範圍(要大寫才生效哦)
// A2(左上角)代表A列的第二行,C3(右下角)代表C列的第三行
// 如果你不想限制行,就不要包含數位
var rows = MiniExcel.QueryRange(path, startCell: "A2", endCell: "C3").Cast<IDictionary<string, object>>();
```


Expand Down
15 changes: 15 additions & 0 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public static IEnumerable<dynamic> Query(this Stream stream, bool useHeaderRow =

#region range

/// <summary>
/// Extract the given range。 Only uppercase letters are effective。
/// e.g.
/// MiniExcel.QueryRange(path, startCell: "A2", endCell: "C3")
/// A2 represents the second row of column A, C3 represents the third row of column C
/// If you don't want to restrict rows, just don't include numbers
/// </summary>
/// <param name="path"></param>
/// <param name="useHeaderRow"></param>
/// <param name="sheetName"></param>
/// <param name="excelType"></param>
/// <param name="startCell">top left corner</param>
/// <param name="endCell">lower right corner</param>
/// <param name="configuration"></param>
/// <returns></returns>
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)
{
using (var stream = FileHelper.OpenSharedRead(path))
Expand Down
15 changes: 15 additions & 0 deletions tests/MiniExcelTests/MiniExcelOpenXmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ public void QueryCastToIDictionary()

}
}

[Fact]
public void QueryRangeToIDictionary()
{
var path = @"../../../../../samples/xlsx/TestCenterEmptyRow/TestCenterEmptyRow.xlsx";
// tips:Only uppercase letters are effective
var rows = MiniExcel.QueryRange(path, startCell: "A2", endCell: "C")
.Cast<IDictionary<string, object>>()
.ToList();
Assert.Equal(5, rows.Count);
Assert.Equal(3, rows[0].Count);
Assert.Equal(2d, rows[1]["B"]);
Assert.Equal(null!, rows[2]["A"]);
}

[Fact()]
public void CenterEmptyRowsQueryTest()
{
Expand Down

0 comments on commit 9de96ab

Please sign in to comment.