@@ -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