|
13 | 13 |
|
14 | 14 | public static partial class MiniExcel
|
15 | 15 | {
|
16 |
| - public static async Task InsertAsync(string path, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, bool printHeader = true, bool overwriteSheet = false, CancellationToken cancellationToken = default) |
| 16 | + public static async Task<int> InsertAsync(string path, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, bool printHeader = true, bool overwriteSheet = false, CancellationToken cancellationToken = default) |
17 | 17 | {
|
18 | 18 | if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")
|
19 |
| - throw new NotSupportedException("MiniExcel Insert not support xlsm"); |
| 19 | + throw new NotSupportedException("MiniExcel's Insert does not support the .xlsm format"); |
20 | 20 |
|
21 | 21 | if (!File.Exists(path))
|
22 | 22 | {
|
23 |
| - await SaveAsAsync(path, value, printHeader, sheetName, excelType, cancellationToken: cancellationToken); |
| 23 | + var rowsWritten = await SaveAsAsync(path, value, printHeader, sheetName, excelType, configuration, cancellationToken: cancellationToken); |
| 24 | + return rowsWritten.FirstOrDefault(); |
| 25 | + } |
| 26 | + |
| 27 | + if (excelType == ExcelType.CSV) |
| 28 | + { |
| 29 | + using (var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan)) |
| 30 | + return await InsertAsync(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration, printHeader, overwriteSheet, cancellationToken); |
24 | 31 | }
|
25 | 32 | else
|
26 | 33 | {
|
27 |
| - if (excelType == ExcelType.CSV) |
28 |
| - { |
29 |
| - using (var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan)) |
30 |
| - await InsertAsync(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration, printHeader, overwriteSheet, cancellationToken); |
31 |
| - } |
32 |
| - else |
33 |
| - { |
34 |
| - using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.SequentialScan)) |
35 |
| - await InsertAsync(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration, printHeader, overwriteSheet, cancellationToken); |
36 |
| - } |
| 34 | + using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.SequentialScan)) |
| 35 | + return await InsertAsync(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration, printHeader, overwriteSheet, cancellationToken); |
37 | 36 | }
|
38 | 37 | }
|
39 | 38 |
|
40 |
| - public static async Task InsertAsync(this Stream stream, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, bool printHeader = true, bool overwriteSheet = false, CancellationToken cancellationToken = default) |
| 39 | + public static async Task<int>InsertAsync(this Stream stream, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, bool printHeader = true, bool overwriteSheet = false, CancellationToken cancellationToken = default) |
41 | 40 | {
|
42 | 41 | stream.Seek(0, SeekOrigin.End);
|
43 | 42 | // reuse code
|
44 | 43 | if (excelType == ExcelType.CSV)
|
45 | 44 | {
|
46 |
| - object v = null; |
47 |
| - { |
48 |
| - if (!(value is IEnumerable) && !(value is IDataReader) && !(value is IDictionary<string, object>) && !(value is IDictionary)) |
49 |
| - v = Enumerable.Range(0, 1).Select(s => value); |
50 |
| - else |
51 |
| - v = value; |
52 |
| - } |
53 |
| - await ExcelWriterFactory.GetProvider(stream, v, sheetName, excelType, configuration, false).InsertAsync(overwriteSheet); |
| 45 | + var newValue = value is IEnumerable || value is IDataReader ? value : new[]{value}.AsEnumerable(); |
| 46 | + return await ExcelWriterFactory.GetProvider(stream, newValue, sheetName, excelType, configuration, false).InsertAsync(overwriteSheet, cancellationToken); |
54 | 47 | }
|
55 | 48 | else
|
56 | 49 | {
|
57 |
| - if (configuration == null) |
58 |
| - { |
59 |
| - configuration = new OpenXmlConfiguration { FastMode = true }; |
60 |
| - } |
61 |
| - await ExcelWriterFactory.GetProvider(stream, value, sheetName, excelType, configuration, printHeader).InsertAsync(overwriteSheet); |
| 50 | + var configOrDefault = configuration ?? new OpenXmlConfiguration { FastMode = true }; |
| 51 | + return await ExcelWriterFactory.GetProvider(stream, value, sheetName, excelType, configOrDefault, printHeader).InsertAsync(overwriteSheet, cancellationToken); |
62 | 52 | }
|
63 | 53 | }
|
64 | 54 |
|
65 |
| - public static async Task SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, bool overwriteFile = false, CancellationToken cancellationToken = default(CancellationToken)) |
| 55 | + public static async Task<int[]> SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, bool overwriteFile = false, CancellationToken cancellationToken = default(CancellationToken)) |
66 | 56 | {
|
67 | 57 | if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")
|
68 |
| - throw new NotSupportedException("MiniExcel SaveAs not support xlsm"); |
| 58 | + throw new NotSupportedException("MiniExcel's SaveAs does not support the .xlsm format"); |
69 | 59 |
|
70 | 60 | using (var stream = overwriteFile ? File.Create(path) : new FileStream(path, FileMode.CreateNew))
|
71 |
| - await SaveAsAsync(stream, value, printHeader, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration); |
| 61 | + return await SaveAsAsync(stream, value, printHeader, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration, cancellationToken); |
72 | 62 | }
|
73 | 63 |
|
74 |
| - public static async Task SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken)) |
| 64 | + public static async Task<int[]> SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken)) |
75 | 65 | {
|
76 |
| - await ExcelWriterFactory.GetProvider(stream, value, sheetName, excelType, configuration, printHeader).SaveAsAsync(cancellationToken); |
| 66 | + return await ExcelWriterFactory.GetProvider(stream, value, sheetName, excelType, configuration, printHeader).SaveAsAsync(cancellationToken); |
77 | 67 | }
|
78 | 68 |
|
79 | 69 | public static async Task MergeSameCellsAsync(string mergedFilePath, string path, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
|
@@ -108,7 +98,7 @@ public static async Task InsertAsync(this Stream stream, object value, string sh
|
108 | 98 |
|
109 | 99 | public static async Task<IEnumerable<dynamic>> QueryAsync(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
|
110 | 100 | {
|
111 |
| - TaskCompletionSource<IEnumerable<dynamic>> tcs = new TaskCompletionSource<IEnumerable<dynamic>>(); |
| 101 | + var tcs = new TaskCompletionSource<IEnumerable<dynamic>>(); |
112 | 102 | cancellationToken.Register(() =>
|
113 | 103 | {
|
114 | 104 | tcs.TrySetCanceled();
|
|
0 commit comments