44 using MiniExcelLibs . Utils ;
55 using MiniExcelLibs . Zip ;
66 using System ;
7+ using System . Collections ;
78 using System . Collections . Generic ;
89 using System . Data ;
910 using System . Dynamic ;
@@ -24,6 +25,28 @@ public static MiniExcelDataReader GetReader(this Stream stream, bool useHeaderRo
2425 return new MiniExcelDataReader ( stream , useHeaderRow , sheetName , excelType , startCell , configuration ) ;
2526 }
2627
28+ public static void Insert ( string path , object value , string sheetName = "Sheet1" , ExcelType excelType = ExcelType . UNKNOWN , IConfiguration configuration = null )
29+ {
30+ if ( Path . GetExtension ( path ) . ToLowerInvariant ( ) != ".csv" )
31+ throw new NotSupportedException ( "MiniExcel SaveAs only support csv insert now" ) ;
32+
33+ using ( var stream = new FileStream ( path , FileMode . Append , FileAccess . Write , FileShare . Read , 4096 , FileOptions . SequentialScan ) )
34+ Insert ( stream , value , sheetName , ExcelTypeHelper . GetExcelType ( path , excelType ) , configuration ) ;
35+ }
36+
37+ public static void Insert ( this Stream stream , object value , string sheetName = "Sheet1" , ExcelType excelType = ExcelType . XLSX , IConfiguration configuration = null )
38+ {
39+ // reuse code
40+ object v = null ;
41+ {
42+ if ( ! ( value is IEnumerable ) && ! ( value is IDataReader ) && ! ( value is IDictionary < string , object > ) && ! ( value is IDictionary ) )
43+ v = Enumerable . Range ( 0 , 1 ) . Select ( s => value ) ;
44+ else
45+ v = value ;
46+ }
47+ ExcelWriterFactory . GetProvider ( stream , v , sheetName , excelType , configuration , false ) . Insert ( ) ;
48+ }
49+
2750 public static void SaveAs ( string path , object value , bool printHeader = true , string sheetName = "Sheet1" , ExcelType excelType = ExcelType . UNKNOWN , IConfiguration configuration = null , bool overwriteFile = false )
2851 {
2952 if ( Path . GetExtension ( path ) . ToLowerInvariant ( ) == ".xlsm" )
0 commit comments