@@ -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+ }
0 commit comments