From 8b6414e99e2f477ce66563a6947d53cf77ff246b Mon Sep 17 00:00:00 2001 From: wei Date: Mon, 21 Jun 2021 13:14:37 +0800 Subject: [PATCH] - Add QueryAsDataTableAsync(this Stream stream..) - Code Refacturing : MiniExcel.cs --- src/MiniExcel/MiniExcel.Async.cs | 95 ++++++++++++++++++++++++++++ src/MiniExcel/MiniExcel.cs | 104 +++++-------------------------- 2 files changed, 112 insertions(+), 87 deletions(-) create mode 100644 src/MiniExcel/MiniExcel.Async.cs diff --git a/src/MiniExcel/MiniExcel.Async.cs b/src/MiniExcel/MiniExcel.Async.cs new file mode 100644 index 0000000..4fc936e --- /dev/null +++ b/src/MiniExcel/MiniExcel.Async.cs @@ -0,0 +1,95 @@ +namespace MiniExcelLibs +{ + using MiniExcelLibs.OpenXml; + using MiniExcelLibs.Utils; + using MiniExcelLibs.Zip; + using System; + using System.Collections.Generic; + using System.Data; + using System.IO; + using System.Linq; + using System.Threading.Tasks; + + public static partial class MiniExcel + { + public static Task SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) + { + return Task.Run(() => SaveAs(path, value, printHeader, sheetName, excelType , configuration)); + } + + public static Task SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null) + { + return GetWriterProvider(stream, sheetName, excelType).SaveAsAsync(value, sheetName, printHeader, configuration); + } + + public static Task> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) + { + return Task.Run(() => Query(path, useHeaderRow, sheetName, excelType, startCell, configuration)); + } + + public static Task> QueryAsync(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new() + { + return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)).QueryAsync(sheetName, startCell, configuration); + } + + public static Task> QueryAsync(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new() + { + return Task.Run(() => Query(path, sheetName, excelType, startCell, configuration)); + } + + public static Task>> QueryAsync(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) + { + return GetReaderProvider(stream, excelType).QueryAsync(useHeaderRow, sheetName, startCell, configuration); + } + public static Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value) + { + return ExcelTemplateFactory.GetProvider(stream).SaveAsByTemplateAsync(templatePath, value); + } + + public static Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value) + { + return ExcelTemplateFactory.GetProvider(stream).SaveAsByTemplateAsync(templateBytes, value); + } + + public static Task SaveAsByTemplateAsync(string path, string templatePath, object value) + { + return Task.Run(() => SaveAsByTemplate(path, templatePath, value)); + } + + public static Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value) + { + return Task.Run(() => SaveAsByTemplate(path, templateBytes, value)); + } + + /// + /// QueryAsDataTable is not recommended, because it'll load all data into memory. + /// + public static Task QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) + { + return Task.Run(() => QueryAsDataTable(path, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration)); + } + + /// + /// QueryAsDataTable is not recommended, because it'll load all data into memory. + /// + public static Task QueryAsDataTableAsync(this Stream stream, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) + { + return Task.Run(() => ExcelOpenXmlSheetReader.QueryAsDataTableImpl(stream, useHeaderRow, ref sheetName, excelType, startCell, configuration)); + } + + private static IExcelWriterAsync GetWriterProvider(Stream stream, string sheetName, ExcelType excelType) + { + if (string.IsNullOrEmpty(sheetName)) + throw new InvalidDataException("Sheet name can not be empty or null"); + if (excelType == ExcelType.UNKNOWN) + throw new InvalidDataException("Please specify excelType"); + + return ExcelWriterFactory.GetProvider(stream, excelType); + } + + private static IExcelReaderAsync GetReaderProvider(Stream stream, ExcelType excelType) + { + return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)); + } + } +} diff --git a/src/MiniExcel/MiniExcel.cs b/src/MiniExcel/MiniExcel.cs index c2cc193..78897e8 100644 --- a/src/MiniExcel/MiniExcel.cs +++ b/src/MiniExcel/MiniExcel.cs @@ -20,21 +20,11 @@ SaveAs(stream, value, printHeader, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration); } - public static Task SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) - { - return Task.Run(() => SaveAs(path, value, printHeader, sheetName, excelType , configuration)); - } - public static void SaveAs(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null) { GetWriterProvider(stream, sheetName, excelType).SaveAs(value, sheetName, printHeader, configuration); } - public static Task SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null) - { - return GetWriterProvider(stream, sheetName, excelType).SaveAsAsync(value, sheetName, printHeader, configuration); - } - public static IEnumerable Query(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new() { using (var stream = Helpers.OpenSharedRead(path)) @@ -47,21 +37,6 @@ return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)).Query(sheetName, startCell, configuration); } - public static Task> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) - { - return Task.Run(() => Query(path, useHeaderRow, sheetName, excelType, startCell, configuration)); - } - - public static Task> QueryAsync(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new() - { - return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)).QueryAsync(sheetName, startCell, configuration); - } - - public static Task> QueryAsync(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new() - { - return Task.Run(() => Query(path, sheetName, excelType, startCell, configuration)); - } - public static IEnumerable Query(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) { using (var stream = Helpers.OpenSharedRead(path)) @@ -74,34 +49,6 @@ return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)).Query(useHeaderRow, sheetName, startCell, configuration); } - public static Task>> QueryAsync(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) - { - return GetReaderProvider(stream, excelType).QueryAsync(useHeaderRow, sheetName, startCell, configuration); - } - - public static List GetSheetNames(string path) - { - using (var stream = Helpers.OpenSharedRead(path)) - return GetSheetNames(stream); - } - - public static List GetSheetNames(this Stream stream) - { - var archive = new ExcelOpenXmlZip(stream); - return ExcelOpenXmlSheetReader.GetWorkbookRels(archive.Entries).Select(s => s.Name).ToList(); - } - - public static ICollection GetColumns(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) - { - using (var stream = Helpers.OpenSharedRead(path)) - return GetColumns(stream, useHeaderRow, sheetName, excelType, startCell, configuration); - } - - public static ICollection GetColumns(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) - { - return (Query(stream, useHeaderRow, sheetName, excelType, startCell, configuration).FirstOrDefault() as IDictionary)?.Keys; - } - public static void SaveAsByTemplate(string path, string templatePath, object value) { using (var stream = File.Create(path)) @@ -124,27 +71,6 @@ ExcelTemplateFactory.GetProvider(stream).SaveAsByTemplate(templateBytes, value); } - public static Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value) - { - return ExcelTemplateFactory.GetProvider(stream).SaveAsByTemplateAsync(templatePath, value); - } - - public static Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value) - { - return ExcelTemplateFactory.GetProvider(stream).SaveAsByTemplateAsync(templateBytes, value); - } - - public static Task SaveAsByTemplateAsync(string path, string templatePath, object value) - { - return Task.Run(() => SaveAsByTemplate(path, templatePath, value)); - } - - public static Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value) - { - return Task.Run(() => SaveAsByTemplate(path, templateBytes, value)); - } - - /// /// QueryAsDataTable is not recommended, because it'll load all data into memory. /// @@ -154,10 +80,6 @@ return QueryAsDataTable(stream, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration); } - public static Task QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) - { - return Task.Run(() => QueryAsDataTable(path, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration)); - } /// /// QueryAsDataTable is not recommended, because it'll load all data into memory. /// @@ -166,19 +88,27 @@ return ExcelOpenXmlSheetReader.QueryAsDataTableImpl(stream, useHeaderRow, ref sheetName, excelType, startCell, configuration); } - private static IExcelWriterAsync GetWriterProvider(Stream stream, string sheetName, ExcelType excelType) + public static List GetSheetNames(string path) { - if (string.IsNullOrEmpty(sheetName)) - throw new InvalidDataException("Sheet name can not be empty or null"); - if (excelType == ExcelType.UNKNOWN) - throw new InvalidDataException("Please specify excelType"); - - return ExcelWriterFactory.GetProvider(stream, excelType); + using (var stream = Helpers.OpenSharedRead(path)) + return GetSheetNames(stream); } - private static IExcelReaderAsync GetReaderProvider(Stream stream, ExcelType excelType) + public static List GetSheetNames(this Stream stream) { - return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)); + var archive = new ExcelOpenXmlZip(stream); + return ExcelOpenXmlSheetReader.GetWorkbookRels(archive.Entries).Select(s => s.Name).ToList(); + } + + public static ICollection GetColumns(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) + { + using (var stream = Helpers.OpenSharedRead(path)) + return GetColumns(stream, useHeaderRow, sheetName, excelType, startCell, configuration); + } + + public static ICollection GetColumns(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) + { + return (Query(stream, useHeaderRow, sheetName, excelType, startCell, configuration).FirstOrDefault() as IDictionary)?.Keys; } } }