Merge branch 'master' of https://github.com/shps951023/MiniExcel into masterFork

This commit is contained in:
damiel 2021-06-21 21:51:51 +08:00
commit 67dadc2b83
10 changed files with 189 additions and 89 deletions

View File

@ -829,6 +829,28 @@ MiniExcel.SaveAs(path, value,excelType:ExcelType.CSV, configuration: config);
### Async
v0.17.0 support Async (thanks isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel))
```csharp
public static Task SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
public static Task SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null)
public static Task<IEnumerable<dynamic>> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
public static Task<IEnumerable<T>> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
public static Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
public static Task<IEnumerable<IDictionary<string, object>>> QueryAsync(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
public static Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value)
public static Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value)
public static Task SaveAsByTemplateAsync(string path, string templatePath, object value)
public static Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value)
public static Task<DataTable> QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
```
### Examples:
#### 1. SQLite & Dapper `Large Size File` SQL Insert Avoid OOM

View File

@ -827,6 +827,26 @@ MiniExcel.SaveAs(path, value,excelType:ExcelType.CSV, configuration: config);
### 异步 Async
从 v0.17.0 版本开始支持异步 (感谢[isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel))
```csharp
public static Task SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
public static Task SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null)
public static Task<IEnumerable<dynamic>> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
public static Task<IEnumerable<T>> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
public static Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
public static Task<IEnumerable<IDictionary<string, object>>> QueryAsync(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
public static Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value)
public static Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value)
public static Task SaveAsByTemplateAsync(string path, string templatePath, object value)
public static Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value)
public static Task<DataTable> QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
```
### 例子
#### 1. SQLite & Dapper 读取大数据新增到数据库

View File

@ -842,6 +842,28 @@ MiniExcel.SaveAs(path, value,excelType:ExcelType.CSV, configuration: config);
### 異步 Async
從 v0.17.0 版本開始支持異步 (感謝[isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel))
```csharp
public static Task SaveAsAsync(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
public static Task SaveAsAsync(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null)
public static Task<IEnumerable<dynamic>> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
public static Task<IEnumerable<T>> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
public static Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
public static Task<IEnumerable<IDictionary<string, object>>> QueryAsync(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
public static Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value)
public static Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value)
public static Task SaveAsByTemplateAsync(string path, string templatePath, object value)
public static Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value)
public static Task<DataTable> QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
```
### 範例
#### 1. SQLite & Dapper 讀取大數據新增到資料庫

View File

@ -16,6 +16,9 @@
---
### 0.17.0
- [New] Support Async/Task #52, contributor: [isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel)
### 0.16.1
- [New] SaveAsByTemplate support DateTime custom format #255, contributor: [网虫 (landde) - Gitee.com](https://gitee.com/landde)

View File

@ -17,6 +17,9 @@
---
### 0.17.0
- [New] 支持 Async/Task #52, contributor: [isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel)
### 0.16.1
- [New] SaveAsByTemplate 支持 DateTime 自定义格式 #255, contributor: [网虫 (landde) - Gitee.com](https://gitee.com/landde)

View File

@ -17,6 +17,9 @@
---
### 0.17.0
- [New] 支持 Async/Task #52, contributor: [isdaniel ( SHIH,BING-SIOU)](https://github.com/isdaniel)
### 0.16.1
- [New] SaveAsByTemplate 支持 DateTime 自定義格式 #255, contributor: [网虫 (landde) - Gitee.com](https://gitee.com/landde)

View File

@ -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<IEnumerable<dynamic>> 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<IEnumerable<T>> QueryAsync<T>(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<T>(sheetName, startCell, configuration);
}
public static Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
{
return Task.Run(() => Query<T>(path, sheetName, excelType, startCell, configuration));
}
public static Task<IEnumerable<IDictionary<string, object>>> 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));
}
/// <summary>
/// QueryAsDataTable is not recommended, because it'll load all data into memory.
/// </summary>
public static Task<DataTable> 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));
}
/// <summary>
/// QueryAsDataTable is not recommended, because it'll load all data into memory.
/// </summary>
public static Task<DataTable> 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));
}
}
}

View File

@ -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<T> Query<T>(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<T>(sheetName, startCell, configuration);
}
public static Task<IEnumerable<dynamic>> 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<IEnumerable<T>> QueryAsync<T>(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<T>(sheetName, startCell, configuration);
}
public static Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null) where T : class, new()
{
return Task.Run(() => Query<T>(path, sheetName, excelType, startCell, configuration));
}
public static IEnumerable<dynamic> 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<IEnumerable<IDictionary<string, object>>> 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<string> GetSheetNames(string path)
{
using (var stream = Helpers.OpenSharedRead(path))
return GetSheetNames(stream);
}
public static List<string> GetSheetNames(this Stream stream)
{
var archive = new ExcelOpenXmlZip(stream);
return ExcelOpenXmlSheetReader.GetWorkbookRels(archive.Entries).Select(s => s.Name).ToList();
}
public static ICollection<string> 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<string> 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<string, object>)?.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));
}
/// <summary>
/// QueryAsDataTable is not recommended, because it'll load all data into memory.
/// </summary>
@ -154,10 +80,6 @@
return QueryAsDataTable(stream, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration);
}
public static Task<DataTable> 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));
}
/// <summary>
/// QueryAsDataTable is not recommended, because it'll load all data into memory.
/// </summary>
@ -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<string> 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<string> 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<string> 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<string> 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<string, object>)?.Keys;
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
<Version>0.16.1</Version>
<Version>0.17.0</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniExcel</AssemblyName>

View File

@ -67,7 +67,7 @@ namespace MiniExcelLibs.OpenXml
templateStream.CopyTo(stream);
var reader = new ExcelOpenXmlSheetReader(stream);
using (var _archive = new ExcelOpenXmlZip(stream, mode: ZipArchiveMode.Update, true, Encoding.UTF8))
var _archive = new ExcelOpenXmlZip(stream, mode: ZipArchiveMode.Update, true, Encoding.UTF8);
{
//read sharedString
var sharedStrings = reader.GetSharedStrings();
@ -94,6 +94,8 @@ namespace MiniExcelLibs.OpenXml
}
}
}
_archive.ZipFile.Dispose();
}
}