From 9de96abba848f45116aee6181a05a530fcf3cb88 Mon Sep 17 00:00:00 2001 From: Jed Samok <49677180+xiaolipro@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:04:44 +0800 Subject: [PATCH] =?UTF-8?q?add=20QueryRange=20test=20and=20docs.=EF=BC=88#?= =?UTF-8?q?634=EF=BC=89=20(#635)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ README.zh-CN.md | 4 ++++ README.zh-Hant.md | 4 ++++ src/MiniExcel/MiniExcel.cs | 15 +++++++++++++++ tests/MiniExcelTests/MiniExcelOpenXmlTests.cs | 15 +++++++++++++++ 5 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 0bdfedf..4fe6a63 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,10 @@ foreach(IDictionary row in MiniExcel.Query(path)) // or var rows = MiniExcel.Query(path).Cast>(); +// 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>(); ``` diff --git a/README.zh-CN.md b/README.zh-CN.md index b82d906..354f0c7 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -267,6 +267,10 @@ foreach(IDictionary row in MiniExcel.Query(path)) // or var rows = MiniExcel.Query(path).Cast>(); +// or 查询指定范围(要大写才生效哦) +// A2(左上角)代表A列的第二行,C3(右下角)代表C列的第三行 +// 如果你不想限制行,就不要包含数字 +var rows = MiniExcel.QueryRange(path, startCell: "A2", endCell: "C3").Cast>(); ``` #### 9. Query 读 Excel 返回 DataTable diff --git a/README.zh-Hant.md b/README.zh-Hant.md index f0eddf3..d0f043d 100644 --- a/README.zh-Hant.md +++ b/README.zh-Hant.md @@ -265,6 +265,10 @@ foreach(IDictionary row in MiniExcel.Query(path)) // or var rows = MiniExcel.Query(path).Cast>(); +// or 査詢指定範圍(要大寫才生效哦) +// A2(左上角)代表A列的第二行,C3(右下角)代表C列的第三行 +// 如果你不想限制行,就不要包含數位 +var rows = MiniExcel.QueryRange(path, startCell: "A2", endCell: "C3").Cast>(); ``` diff --git a/src/MiniExcel/MiniExcel.cs b/src/MiniExcel/MiniExcel.cs index abf5649..c508f65 100644 --- a/src/MiniExcel/MiniExcel.cs +++ b/src/MiniExcel/MiniExcel.cs @@ -91,6 +91,21 @@ #region range + /// + /// 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 + /// + /// + /// + /// + /// + /// top left corner + /// lower right corner + /// + /// public static IEnumerable 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)) diff --git a/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs b/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs index 5155b3c..6afa983 100644 --- a/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs +++ b/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs @@ -166,6 +166,21 @@ namespace MiniExcelLibs.Tests } } + + [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>() + .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() {