From 6d81ddc59fd34d28e29ce94195bd4b026a6a211c Mon Sep 17 00:00:00 2001 From: Amos Date: Fri, 16 Aug 2024 23:44:51 +0800 Subject: [PATCH] perf csv insert (#653) --- src/MiniExcel/MiniExcel.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/MiniExcel/MiniExcel.cs b/src/MiniExcel/MiniExcel.cs index bcfe6620..cef8a08c 100644 --- a/src/MiniExcel/MiniExcel.cs +++ b/src/MiniExcel/MiniExcel.cs @@ -27,7 +27,7 @@ public static MiniExcelDataReader GetReader(this Stream stream, bool useHeaderRo public static void Insert(string path, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) { if (Path.GetExtension(path).ToLowerInvariant() != ".csv") - throw new NotSupportedException("MiniExcel SaveAs only support csv insert now"); + throw new NotSupportedException("MiniExcel only support csv insert now"); using (var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan)) Insert(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration); @@ -35,6 +35,9 @@ public static void Insert(string path, object value, string sheetName = "Sheet1" public static void Insert(this Stream stream, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null) { + if (excelType != ExcelType.CSV) + throw new NotSupportedException("MiniExcel only support csv insert now"); + // reuse code object v = null; { @@ -43,6 +46,8 @@ public static void Insert(this Stream stream, object value, string sheetName = " else v = value; } + + stream.Seek(0, SeekOrigin.End); ExcelWriterFactory.GetProvider(stream, v, sheetName, excelType, configuration, false).Insert(); }