Make System.DateOnly available as date in Excel (#576)

* Format the code and remove trailing whitespaces

* Update Visual Studio gitignore template

* Refactor: rename variables, add comments

* Support writing DateOnly type to Excel

---------

Co-authored-by: Jef Van den Brandt <Jef.VandenBrandt@cheops.be>
This commit is contained in:
Jef 2024-03-31 17:49:31 +02:00 committed by GitHub
parent 9eb23d9bdf
commit b27b169e2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
52 changed files with 610 additions and 473 deletions

179
.gitignore vendored
View File

@ -1,7 +1,10 @@
## Ignore Visual Studio temporary files, build results, and ## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons. ## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
# User-specific files # User-specific files
*.rsuser
*.suo *.suo
*.user *.user
*.userosscache *.userosscache
@ -10,6 +13,9 @@
# User-specific files (MonoDevelop/Xamarin Studio) # User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs *.userprefs
# Mono auto generated files
mono_crash.*
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
[Dd]ebugPublic/ [Dd]ebugPublic/
@ -17,42 +23,62 @@
[Rr]eleases/ [Rr]eleases/
x64/ x64/
x86/ x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/ bld/
[Bb]in/ [Bb]in/
[Oo]bj/ [Oo]bj/
[Ll]og/ [Ll]og/
[Ll]ogs/
# Visual Studio 2015 cache/options directory # Visual Studio 2015/2017 cache/options directory
.vs/ .vs/
# Uncomment if you have tasks that create the project's static files in wwwroot # Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/ #wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results # MSTest test Results
[Tt]est[Rr]esult*/ [Tt]est[Rr]esult*/
[Bb]uild[Ll]og.* [Bb]uild[Ll]og.*
# NUNIT # NUnit
*.VisualState.xml *.VisualState.xml
TestResult.xml TestResult.xml
nunit-*.xml
# Build Results of an ATL Project # Build Results of an ATL Project
[Dd]ebugPS/ [Dd]ebugPS/
[Rr]eleasePS/ [Rr]eleasePS/
dlldata.c dlldata.c
# DNX # Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json project.lock.json
project.fragment.lock.json project.fragment.lock.json
artifacts/ artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c *_i.c
*_p.c *_p.c
*_i.h *_h.h
*.ilk *.ilk
*.meta *.meta
*.obj *.obj
*.iobj
*.pch *.pch
*.pdb *.pdb
*.ipdb
*.pgc *.pgc
*.pgd *.pgd
*.rsp *.rsp
@ -62,7 +88,9 @@ artifacts/
*.tlh *.tlh
*.tmp *.tmp
*.tmp_proj *.tmp_proj
*_wpftmp.csproj
*.log *.log
*.tlog
*.vspscc *.vspscc
*.vssscc *.vssscc
.builds .builds
@ -90,6 +118,9 @@ ipch/
*.vspx *.vspx
*.sap *.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace # TFS 2012 Local Workspace
$tf/ $tf/
@ -101,15 +132,25 @@ _ReSharper*/
*.[Rr]e[Ss]harper *.[Rr]e[Ss]harper
*.DotSettings.user *.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in # TeamCity is a build add-in
_TeamCity* _TeamCity*
# DotCover is a Code Coverage Tool # DotCover is a Code Coverage Tool
*.dotCover *.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch # NCrunch
_NCrunch_* _NCrunch_*
.*crunch*.local.xml .*crunch*.local.xml
@ -141,9 +182,9 @@ publish/
# Publish Web Output # Publish Web Output
*.[Pp]ublish.xml *.[Pp]ublish.xml
*.azurePubxml *.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings # Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted # but database connection strings (with potential passwords) will be unencrypted
#*.pubxml *.pubxml
*.publishproj *.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to # Microsoft Azure Web App publish settings. Comment the next line if you want to
@ -153,13 +194,15 @@ PublishScripts/
# NuGet Packages # NuGet Packages
*.nupkg *.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore # The packages folder can be ignored because of Package Restore
**/packages/* **/[Pp]ackages/*
# except build/, which is used as an MSBuild target. # except build/, which is used as an MSBuild target.
!**/packages/build/ !**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed # Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config #!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignoreable files # NuGet v3's project.json files produces more ignorable files
*.nuget.props *.nuget.props
*.nuget.targets *.nuget.targets
@ -176,12 +219,15 @@ AppPackages/
BundleArtifacts/ BundleArtifacts/
Package.StoreAssociation.xml Package.StoreAssociation.xml
_pkginfo.txt _pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files # Visual Studio cache files
# files ending in .cache can be ignored # files ending in .cache can be ignored
*.[Cc]ache *.[Cc]ache
# but keep track of directories ending in .cache # but keep track of directories ending in .cache
!*.[Cc]ache/ !?*.[Cc]ache/
# Others # Others
ClientBin/ ClientBin/
@ -192,9 +238,12 @@ ClientBin/
*.jfm *.jfm
*.pfx *.pfx
*.publishsettings *.publishsettings
node_modules/
orleans.codegen.cs orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components # Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/ #bower_components/
@ -209,15 +258,22 @@ _UpgradeReport_Files/
Backup*/ Backup*/
UpgradeLog*.XML UpgradeLog*.XML
UpgradeLog*.htm UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files # SQL Server files
*.mdf *.mdf
*.ldf *.ldf
*.ndf
# Business Intelligence projects # Business Intelligence projects
*.rdl.data *.rdl.data
*.bim.layout *.bim.layout
*.bim_*.settings *.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes # Microsoft Fakes
FakesAssemblies/ FakesAssemblies/
@ -227,6 +283,7 @@ FakesAssemblies/
# Node.js Tools for Visual Studio # Node.js Tools for Visual Studio
.ntvs_analysis.dat .ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log # Visual Studio 6 build log
*.plg *.plg
@ -234,6 +291,20 @@ FakesAssemblies/
# Visual Studio 6 workspace options file # Visual Studio 6 workspace options file
*.opt *.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output # Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts **/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts **/*.DesktopClient/GeneratedArtifacts
@ -249,17 +320,83 @@ paket-files/
# FAKE - F# Make # FAKE - F# Make
.fake/ .fake/
# JetBrains Rider # CodeRush personal settings
.idea/ .cr/personal
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS) # Python Tools for Visual Studio (PTVS)
__pycache__/ __pycache__/
*.pyc *.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
/BenchmarkDotNet.Artifacts /BenchmarkDotNet.Artifacts
/tests/MiniExcel.Tests.AspNetMvc/packages /tests/MiniExcel.Tests.AspNetMvc/packages
/TestTemplate /TestTemplate

View File

@ -1,5 +1,5 @@
using System; using MiniExcelLibs.Utils;
using MiniExcelLibs.Utils; using System;
namespace MiniExcelLibs.Attributes namespace MiniExcelLibs.Attributes
{ {

View File

@ -12,14 +12,14 @@
.GetColumnIndex(columnName), columnName); .GetColumnIndex(columnName), columnName);
public ExcelColumnIndexAttribute(int columnIndex) => Init(columnIndex); public ExcelColumnIndexAttribute(int columnIndex) => Init(columnIndex);
private void Init(int columnIndex,string columnName=null) private void Init(int columnIndex, string columnName = null)
{ {
if (columnIndex < 0) if (columnIndex < 0)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, $"Column index {columnIndex} must be greater or equal to zero."); throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, $"Column index {columnIndex} must be greater or equal to zero.");
} }
if (ExcelXName == null) if (ExcelXName == null)
if(columnName != null) if (columnName != null)
ExcelXName = columnName; ExcelXName = columnName;
else else
ExcelXName = ColumnHelper.GetAlphabetColumnName(columnIndex); ExcelXName = ColumnHelper.GetAlphabetColumnName(columnIndex);

View File

@ -1,13 +1,12 @@
namespace MiniExcelLibs.Attributes namespace MiniExcelLibs.Attributes
{ {
using MiniExcelLibs.Utils;
using System; using System;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnNameAttribute : Attribute public class ExcelColumnNameAttribute : Attribute
{ {
public string ExcelColumnName { get; set; } public string ExcelColumnName { get; set; }
public string[] Aliases { get; set; } public string[] Aliases { get; set; }
public ExcelColumnNameAttribute(string excelColumnName, string[] aliases=null) public ExcelColumnNameAttribute(string excelColumnName, string[] aliases = null)
{ {
ExcelColumnName = excelColumnName; ExcelColumnName = excelColumnName;
Aliases = aliases; Aliases = aliases;

View File

@ -2,7 +2,6 @@ using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Utils; using MiniExcelLibs.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -24,7 +23,7 @@ namespace MiniExcelLibs.Csv
{ {
if (startCell != "A1") if (startCell != "A1")
throw new NotImplementedException("CSV not Implement startCell"); throw new NotImplementedException("CSV not Implement startCell");
if(_stream.CanSeek) if (_stream.CanSeek)
_stream.Position = 0; _stream.Position = 0;
var reader = _config.StreamReaderFunc(_stream); var reader = _config.StreamReaderFunc(_stream);
{ {
@ -94,14 +93,14 @@ namespace MiniExcelLibs.Csv
} }
} }
public Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool UseHeaderRow, string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken)) public Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool UseHeaderRow, string sheetName, string startCell, CancellationToken cancellationToken = default(CancellationToken))
{ {
return Task.Run(() => Query(UseHeaderRow, sheetName, startCell),cancellationToken); return Task.Run(() => Query(UseHeaderRow, sheetName, startCell), cancellationToken);
} }
public Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken)) where T : class, new() public Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new()
{ {
return Task.Run(() => Query<T>(sheetName, startCell),cancellationToken); return Task.Run(() => Query<T>(sheetName, startCell), cancellationToken);
} }
public void Dispose() public void Dispose()

View File

@ -143,7 +143,7 @@ namespace MiniExcelLibs.Csv
public async Task SaveAsAsync(CancellationToken cancellationToken = default(CancellationToken)) public async Task SaveAsAsync(CancellationToken cancellationToken = default(CancellationToken))
{ {
await Task.Run(() => SaveAs(),cancellationToken).ConfigureAwait(false); await Task.Run(() => SaveAs(), cancellationToken).ConfigureAwait(false);
} }
private void GenerateSheetByIDataReader(object value, string seperator, string newLine, StreamWriter writer) private void GenerateSheetByIDataReader(object value, string seperator, string newLine, StreamWriter writer)
@ -162,7 +162,7 @@ namespace MiniExcelLibs.Csv
if (i != 0) if (i != 0)
writer.Write(seperator); writer.Write(seperator);
writer.Write(CsvHelpers.ConvertToCsvValue(ToCsvString(columnName,null), _configuration.AlwaysQuote, _configuration.Seperator)); writer.Write(CsvHelpers.ConvertToCsvValue(ToCsvString(columnName, null), _configuration.AlwaysQuote, _configuration.Seperator));
} }
writer.Write(newLine); writer.Write(newLine);
} }
@ -174,7 +174,7 @@ namespace MiniExcelLibs.Csv
var cellValue = reader.GetValue(i); var cellValue = reader.GetValue(i);
if (i != 0) if (i != 0)
writer.Write(seperator); writer.Write(seperator);
writer.Write(CsvHelpers.ConvertToCsvValue(ToCsvString(cellValue,null), _configuration.AlwaysQuote, _configuration.Seperator)); writer.Write(CsvHelpers.ConvertToCsvValue(ToCsvString(cellValue, null), _configuration.AlwaysQuote, _configuration.Seperator));
} }
writer.Write(newLine); writer.Write(newLine);
} }
@ -192,7 +192,7 @@ namespace MiniExcelLibs.Csv
var first = true; var first = true;
for (int j = 0; j < dt.Columns.Count; j++) for (int j = 0; j < dt.Columns.Count; j++)
{ {
var cellValue = CsvHelpers.ConvertToCsvValue(ToCsvString(dt.Rows[i][j],null), _configuration.AlwaysQuote, _configuration.Seperator); var cellValue = CsvHelpers.ConvertToCsvValue(ToCsvString(dt.Rows[i][j], null), _configuration.AlwaysQuote, _configuration.Seperator);
if (!first) if (!first)
writer.Write(seperator); writer.Write(seperator);
writer.Write(cellValue); writer.Write(cellValue);
@ -206,7 +206,7 @@ namespace MiniExcelLibs.Csv
{ {
foreach (var v in value) foreach (var v in value)
{ {
var values = props.Select(s => CsvHelpers.ConvertToCsvValue(ToCsvString(s?.Property.GetValue(v),s), _configuration.AlwaysQuote, _configuration.Seperator)); var values = props.Select(s => CsvHelpers.ConvertToCsvValue(ToCsvString(s?.Property.GetValue(v), s), _configuration.AlwaysQuote, _configuration.Seperator));
writer.Write(string.Join(seperator, values)); writer.Write(string.Join(seperator, values));
writer.Write(newLine); writer.Write(newLine);
} }
@ -216,7 +216,7 @@ namespace MiniExcelLibs.Csv
{ {
foreach (IDictionary v in value) foreach (IDictionary v in value)
{ {
var values = keys.Select(key => CsvHelpers.ConvertToCsvValue(ToCsvString(v[key],null), _configuration.AlwaysQuote, _configuration.Seperator)); var values = keys.Select(key => CsvHelpers.ConvertToCsvValue(ToCsvString(v[key], null), _configuration.AlwaysQuote, _configuration.Seperator));
writer.Write(string.Join(seperator, values)); writer.Write(string.Join(seperator, values));
writer.Write(newLine); writer.Write(newLine);
} }
@ -226,7 +226,7 @@ namespace MiniExcelLibs.Csv
{ {
foreach (IDictionary<string, object> v in value) foreach (IDictionary<string, object> v in value)
{ {
var values = keys.Select(key => CsvHelpers.ConvertToCsvValue(ToCsvString(v[key],null), _configuration.AlwaysQuote, _configuration.Seperator)); var values = keys.Select(key => CsvHelpers.ConvertToCsvValue(ToCsvString(v[key], null), _configuration.AlwaysQuote, _configuration.Seperator));
writer.Write(string.Join(seperator, values)); writer.Write(string.Join(seperator, values));
writer.Write(newLine); writer.Write(newLine);
} }

View File

@ -1,14 +1,13 @@
namespace MiniExcelLibs namespace MiniExcelLibs
{ {
using MiniExcelLibs.Csv;
using MiniExcelLibs.OpenXml; using MiniExcelLibs.OpenXml;
using System; using System;
using MiniExcelLibs.Csv;
using System.IO; using System.IO;
using System.Globalization;
internal class ExcelReaderFactory internal class ExcelReaderFactory
{ {
internal static IExcelReader GetProvider(Stream stream, ExcelType excelType,IConfiguration configuration) internal static IExcelReader GetProvider(Stream stream, ExcelType excelType, IConfiguration configuration)
{ {
switch (excelType) switch (excelType)
{ {
@ -45,7 +44,7 @@
internal class ExcelTemplateFactory internal class ExcelTemplateFactory
{ {
internal static IExcelTemplateAsync GetProvider(Stream stream, IConfiguration configuration, ExcelType excelType= ExcelType.XLSX) internal static IExcelTemplateAsync GetProvider(Stream stream, IConfiguration configuration, ExcelType excelType = ExcelType.XLSX)
{ {
switch (excelType) switch (excelType)
{ {

View File

@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiniExcelLibs.Exceptions namespace MiniExcelLibs.Exceptions
{ {
@ -12,7 +8,7 @@ namespace MiniExcelLibs.Exceptions
public int Row { get; set; } public int Row { get; set; }
public object Value { get; set; } public object Value { get; set; }
public Type InvalidCastType { get; set; } public Type InvalidCastType { get; set; }
public ExcelInvalidCastException(string columnName, int row,object value,Type invalidCastType, string message) : base(message) public ExcelInvalidCastException(string columnName, int row, object value, Type invalidCastType, string message) : base(message)
{ {
ColumnName = columnName; ColumnName = columnName;
Row = row; Row = row;

View File

@ -1,20 +1,16 @@
using System; using System;
using MiniExcelLibs.Utils;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MiniExcelLibs namespace MiniExcelLibs
{ {
internal interface IExcelReader: IDisposable internal interface IExcelReader : IDisposable
{ {
IEnumerable<IDictionary<string, object>> Query(bool UseHeaderRow, string sheetName,string startCell); IEnumerable<IDictionary<string, object>> Query(bool UseHeaderRow, string sheetName, string startCell);
IEnumerable<T> Query<T>(string sheetName, string startCell) where T : class, new(); IEnumerable<T> Query<T>(string sheetName, string startCell) where T : class, new();
Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool UseHeaderRow, string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken)); Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool UseHeaderRow, string sheetName, string startCell, CancellationToken cancellationToken = default(CancellationToken));
Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken)) where T : class, new(); Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new();
IEnumerable<IDictionary<string, object>> QueryRange(bool UseHeaderRow, string sheetName, string startCell, string endCell); IEnumerable<IDictionary<string, object>> QueryRange(bool UseHeaderRow, string sheetName, string startCell, string endCell);
IEnumerable<T> QueryRange<T>(string sheetName, string startCell, string endCell) where T : class, new(); IEnumerable<T> QueryRange<T>(string sheetName, string startCell, string endCell) where T : class, new();
Task<IEnumerable<IDictionary<string, object>>> QueryAsyncRange(bool UseHeaderRow, string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default(CancellationToken)); Task<IEnumerable<IDictionary<string, object>>> QueryAsyncRange(bool UseHeaderRow, string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default(CancellationToken));

View File

@ -1,5 +1,4 @@
using System.IO; using System.Threading;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MiniExcelLibs namespace MiniExcelLibs
@ -14,8 +13,8 @@ namespace MiniExcelLibs
internal interface IExcelTemplateAsync : IExcelTemplate internal interface IExcelTemplateAsync : IExcelTemplate
{ {
Task SaveAsByTemplateAsync(string templatePath, object value,CancellationToken cancellationToken = default(CancellationToken)); Task SaveAsByTemplateAsync(string templatePath, object value, CancellationToken cancellationToken = default(CancellationToken));
Task SaveAsByTemplateAsync(byte[] templateBtyes, object value,CancellationToken cancellationToken = default(CancellationToken)); Task SaveAsByTemplateAsync(byte[] templateBtyes, object value, CancellationToken cancellationToken = default(CancellationToken));
Task MergeSameCellsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)); Task MergeSameCellsAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
Task MergeSameCellsAsync(byte[] fileInBytes, CancellationToken cancellationToken = default(CancellationToken)); Task MergeSameCellsAsync(byte[] fileInBytes, CancellationToken cancellationToken = default(CancellationToken));
} }

View File

@ -1,5 +1,4 @@
using System.IO; using System.Threading;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MiniExcelLibs namespace MiniExcelLibs

View File

@ -1,16 +1,16 @@
namespace MiniExcelLibs namespace MiniExcelLibs
{ {
using Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Utils;
public static partial class MiniExcel public static partial class MiniExcel
{ {
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)) 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))
{ {
if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm") if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")
throw new NotSupportedException("MiniExcel SaveAs not support xlsm"); throw new NotSupportedException("MiniExcel SaveAs not support xlsm");
@ -19,14 +19,14 @@
await SaveAsAsync(stream, value, printHeader, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration); await SaveAsAsync(stream, value, printHeader, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration);
} }
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)) 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))
{ {
await ExcelWriterFactory.GetProvider(stream, value, sheetName, excelType, configuration, printHeader).SaveAsAsync(cancellationToken); await ExcelWriterFactory.GetProvider(stream, value, sheetName, excelType, configuration, printHeader).SaveAsAsync(cancellationToken);
} }
public static async Task MergeSameCellsAsync(string mergedFilePath, string path, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task MergeSameCellsAsync(string mergedFilePath, string path, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
await Task.Run(() => MergeSameCells(mergedFilePath, path, excelType, configuration) ,cancellationToken).ConfigureAwait(false); await Task.Run(() => MergeSameCells(mergedFilePath, path, excelType, configuration), cancellationToken).ConfigureAwait(false);
} }
public static async Task MergeSameCellsAsync(this Stream stream, string path, ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken)) public static async Task MergeSameCellsAsync(this Stream stream, string path, ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
@ -39,25 +39,26 @@
await ExcelTemplateFactory.GetProvider(stream, configuration, excelType).MergeSameCellsAsync(fileBytes, cancellationToken); await ExcelTemplateFactory.GetProvider(stream, configuration, excelType).MergeSameCellsAsync(fileBytes, cancellationToken);
} }
public static async Task<IEnumerable<dynamic>> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task<IEnumerable<dynamic>> QueryAsync(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
return await Task.Run(() => Query(path, useHeaderRow, sheetName, excelType, startCell, configuration),cancellationToken); return await Task.Run(() => Query(path, useHeaderRow, sheetName, excelType, startCell, configuration), cancellationToken);
} }
public static async Task<IEnumerable<T>> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) where T : class, new() public static async Task<IEnumerable<T>> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new()
{ {
return await ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).QueryAsync<T>(sheetName, startCell,cancellationToken).ConfigureAwait(false); return await ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).QueryAsync<T>(sheetName, startCell, cancellationToken).ConfigureAwait(false);
} }
public static async Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) where T : class, new() public static async Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new()
{ {
return await Task.Run(() => Query<T>(path, sheetName, excelType, startCell, configuration),cancellationToken).ConfigureAwait(false); return await Task.Run(() => Query<T>(path, sheetName, excelType, startCell, configuration), cancellationToken).ConfigureAwait(false);
} }
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)) 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))
{ {
TaskCompletionSource<IEnumerable<dynamic>> tcs = new TaskCompletionSource<IEnumerable<dynamic>>(); TaskCompletionSource<IEnumerable<dynamic>> tcs = new TaskCompletionSource<IEnumerable<dynamic>>();
cancellationToken.Register(() => { cancellationToken.Register(() =>
{
tcs.TrySetCanceled(); tcs.TrySetCanceled();
}); });
@ -76,42 +77,42 @@
return await tcs.Task; return await tcs.Task;
} }
public static async Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value, IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
await ExcelTemplateFactory.GetProvider(stream, configuration).SaveAsByTemplateAsync(templatePath, value,cancellationToken).ConfigureAwait(false); await ExcelTemplateFactory.GetProvider(stream, configuration).SaveAsByTemplateAsync(templatePath, value, cancellationToken).ConfigureAwait(false);
} }
public static async Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value, IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
await ExcelTemplateFactory.GetProvider(stream, configuration).SaveAsByTemplateAsync(templateBytes, value,cancellationToken).ConfigureAwait(false); await ExcelTemplateFactory.GetProvider(stream, configuration).SaveAsByTemplateAsync(templateBytes, value, cancellationToken).ConfigureAwait(false);
} }
public static async Task SaveAsByTemplateAsync(string path, string templatePath, object value, IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task SaveAsByTemplateAsync(string path, string templatePath, object value, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
await Task.Run(() => SaveAsByTemplate(path, templatePath, value, configuration),cancellationToken).ConfigureAwait(false); await Task.Run(() => SaveAsByTemplate(path, templatePath, value, configuration), cancellationToken).ConfigureAwait(false);
} }
public static async Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value, IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value, IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
await Task.Run(() => SaveAsByTemplate(path, templateBytes, value, configuration),cancellationToken).ConfigureAwait(false); await Task.Run(() => SaveAsByTemplate(path, templateBytes, value, configuration), cancellationToken).ConfigureAwait(false);
} }
/// <summary> /// <summary>
/// QueryAsDataTable is not recommended, because it'll load all data into memory. /// QueryAsDataTable is not recommended, because it'll load all data into memory.
/// </summary> /// </summary>
[Obsolete("QueryAsDataTable is not recommended, because it'll load all data into memory.")] [Obsolete("QueryAsDataTable is not recommended, because it'll load all data into memory.")]
public static async Task<DataTable> QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task<DataTable> QueryAsDataTableAsync(string path, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
return await Task.Run(() => QueryAsDataTable(path, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration),cancellationToken).ConfigureAwait(false); return await Task.Run(() => QueryAsDataTable(path, useHeaderRow, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration), cancellationToken).ConfigureAwait(false);
} }
/// <summary> /// <summary>
/// QueryAsDataTable is not recommended, because it'll load all data into memory. /// QueryAsDataTable is not recommended, because it'll load all data into memory.
/// </summary> /// </summary>
[Obsolete("QueryAsDataTable is not recommended, because it'll load all data into memory.")] [Obsolete("QueryAsDataTable is not recommended, because it'll load all data into memory.")]
public static async Task<DataTable> QueryAsDataTableAsync(this Stream stream, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null,CancellationToken cancellationToken = default(CancellationToken)) public static async Task<DataTable> QueryAsDataTableAsync(this Stream stream, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default(CancellationToken))
{ {
return await Task.Run(() => QueryAsDataTable(stream, useHeaderRow, sheetName, excelType, startCell, configuration),cancellationToken).ConfigureAwait(false); return await Task.Run(() => QueryAsDataTable(stream, useHeaderRow, sheetName, excelType, startCell, configuration), cancellationToken).ConfigureAwait(false);
} }
} }
} }

View File

@ -1,8 +1,5 @@
namespace MiniExcelLibs namespace MiniExcelLibs
{ {
using System;
using System.Linq;
public static partial class MiniExcel public static partial class MiniExcel
{ {
public static string LISENCE_CODE = null; public static string LISENCE_CODE = null;

View File

@ -8,8 +8,6 @@
using System.Dynamic; using System.Dynamic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Utils; using Utils;
using Zip; using Zip;

View File

@ -1,6 +1,5 @@
namespace MiniExcelLibs namespace MiniExcelLibs
{ {
using MiniExcelLibs.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;

View File

@ -54,4 +54,9 @@ Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true</De
<ItemGroup Condition="'$(GITHUB_ACTIONS)' == 'true'"> <ItemGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Portable.System.DateTimeOnly">
<Version>8.0.0</Version>
</PackageReference>
</ItemGroup>
</Project> </Project>

View File

@ -1,10 +1,6 @@
using MiniExcelLibs.Zip; using MiniExcelLibs.Zip;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Text;
using System.Xml;
namespace MiniExcelLibs.OpenXml namespace MiniExcelLibs.OpenXml
{ {
@ -191,13 +187,13 @@ namespace MiniExcelLibs.OpenXml
CreateZipEntry("xl/sharedStrings.xml", "application/vnd.openxmlformats-package.relationships+xml", ExcelOpenXmlSheetWriter._defaultSharedString); CreateZipEntry("xl/sharedStrings.xml", "application/vnd.openxmlformats-package.relationships+xml", ExcelOpenXmlSheetWriter._defaultSharedString);
} }
private void CreateZipEntry(string path,string contentType,string content) private void CreateZipEntry(string path, string contentType, string content)
{ {
ZipArchiveEntry entry = _archive.CreateEntry(path, CompressionLevel.Fastest); ZipArchiveEntry entry = _archive.CreateEntry(path, CompressionLevel.Fastest);
using (var zipStream = entry.Open()) using (var zipStream = entry.Open())
using (MiniExcelStreamWriter writer = new MiniExcelStreamWriter(zipStream, _utf8WithBom,_configuration.BufferSize)) using (MiniExcelStreamWriter writer = new MiniExcelStreamWriter(zipStream, _utf8WithBom, _configuration.BufferSize))
writer.Write(content); writer.Write(content);
if(!string.IsNullOrEmpty(contentType)) if (!string.IsNullOrEmpty(contentType))
_zipDictionary.Add(path, new ZipPackageInfo(entry, contentType)); _zipDictionary.Add(path, new ZipPackageInfo(entry, contentType));
} }
@ -205,7 +201,7 @@ namespace MiniExcelLibs.OpenXml
{ {
ZipArchiveEntry entry = _archive.CreateEntry(path, CompressionLevel.Fastest); ZipArchiveEntry entry = _archive.CreateEntry(path, CompressionLevel.Fastest);
using (var zipStream = entry.Open()) using (var zipStream = entry.Open())
zipStream.Write(content,0, content.Length); zipStream.Write(content, 0, content.Length);
} }
} }
} }

View File

@ -9,8 +9,6 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static MiniExcelLibs.Utils.ImageHelper; using static MiniExcelLibs.Utils.ImageHelper;
namespace MiniExcelLibs.OpenXml namespace MiniExcelLibs.OpenXml
@ -382,9 +380,9 @@ namespace MiniExcelLibs.OpenXml
writer.Write($"<x:row r=\"{yIndex}\">"); writer.Write($"<x:row r=\"{yIndex}\">");
var cellIndex = xIndex; var cellIndex = xIndex;
foreach (var p in props) foreach (var columnInfo in props)
{ {
if (p == null) //reason:https://github.com/shps951023/MiniExcel/issues/142 if (columnInfo == null) //reason:https://github.com/shps951023/MiniExcel/issues/142
{ {
cellIndex++; cellIndex++;
continue; continue;
@ -392,20 +390,20 @@ namespace MiniExcelLibs.OpenXml
object cellValue = null; object cellValue = null;
if (isDic) if (isDic)
{ {
cellValue = ((IDictionary)v)[p.Key]; cellValue = ((IDictionary)v)[columnInfo.Key];
//WriteCell(writer, yIndex, cellIndex, cellValue, null); // why null because dictionary that needs to check type every time //WriteCell(writer, yIndex, cellIndex, cellValue, null); // why null because dictionary that needs to check type every time
//TODO: user can specefic type to optimize efficiency //TODO: user can specefic type to optimize efficiency
} }
else if (isDapperRow) else if (isDapperRow)
{ {
cellValue = ((IDictionary<string, object>)v)[p.Key.ToString()]; cellValue = ((IDictionary<string, object>)v)[columnInfo.Key.ToString()];
} }
else else
{ {
cellValue = p.Property.GetValue(v); cellValue = columnInfo.Property.GetValue(v);
} }
WriteCell(writer, yIndex, cellIndex, cellValue, p);
WriteCell(writer, yIndex, cellIndex, cellValue, columnInfo);
cellIndex++; cellIndex++;
} }
@ -416,54 +414,57 @@ namespace MiniExcelLibs.OpenXml
return yIndex - 1; return yIndex - 1;
} }
private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex, object value, ExcelColumnInfo p) private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex, object value, ExcelColumnInfo columnInfo)
{ {
var columname = ExcelOpenXmlUtils.ConvertXyToCell(cellIndex, rowIndex); var columname = ExcelOpenXmlUtils.ConvertXyToCell(cellIndex, rowIndex);
var s = "2";
var valueIsNull = value is null || value is DBNull; var valueIsNull = value is null || value is DBNull;
if (_configuration.EnableWriteNullValueCell && valueIsNull) if (_configuration.EnableWriteNullValueCell && valueIsNull)
{ {
writer.Write($"<x:c r=\"{columname}\" s=\"{s}\"></x:c>"); writer.Write($"<x:c r=\"{columname}\" s=\"2\"></x:c>"); // s: style index
return; return;
} }
var tuple = GetCellValue(rowIndex, cellIndex, value, p, valueIsNull); var tuple = GetCellValue(rowIndex, cellIndex, value, columnInfo, valueIsNull);
s = tuple.Item1; var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
var t = tuple.Item2; var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1
var v = tuple.Item3; var cellValue = tuple.Item3;
if (v != null && (v.StartsWith(" ", StringComparison.Ordinal) || v.EndsWith(" ", StringComparison.Ordinal))) /*Prefix and suffix blank space will lost after SaveAs #294*/ if (cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal))) /*Prefix and suffix blank space will lost after SaveAs #294*/
writer.Write($"<x:c r=\"{columname}\" {(t == null ? "" : $"t =\"{t}\"")} s=\"{s}\" xml:space=\"preserve\"><x:v>{v}</x:v></x:c>"); {
writer.Write($"<x:c r=\"{columname}\" {(dataType == null ? "" : $"t =\"{dataType}\"")} s=\"{styleIndex}\" xml:space=\"preserve\"><x:v>{cellValue}</x:v></x:c>");
}
else else
{
//t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png) //t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png)
writer.Write($"<x:c r=\"{columname}\" {(t == null ? "" : $"t =\"{t}\"")} s=\"{s}\"><x:v>{v}</x:v></x:c>"); writer.Write($"<x:c r=\"{columname}\" {(dataType == null ? "" : $"t =\"{dataType}\"")} s=\"{styleIndex}\"><x:v>{cellValue}</x:v></x:c>");
}
} }
private Tuple<string, string, string> GetCellValue(int rowIndex, int cellIndex, object value, ExcelColumnInfo p, bool valueIsNull) private Tuple<string, string, string> GetCellValue(int rowIndex, int cellIndex, object value, ExcelColumnInfo columnInfo, bool valueIsNull)
{ {
var s = "2"; var styleIndex = "2"; // format code: 0.00
var v = string.Empty; var cellValue = string.Empty;
var t = "str"; var dataType = "str";
if (valueIsNull) if (valueIsNull)
{ {
// use defaults
} }
else if (value is string str) else if (value is string str)
{ {
v = ExcelOpenXmlUtils.EncodeXML(str); cellValue = ExcelOpenXmlUtils.EncodeXML(str);
} }
else if (p?.ExcelFormat != null && value is IFormattable formattableValue) else if (columnInfo?.ExcelFormat != null && value is IFormattable formattableValue)
{ {
var formattedStr = formattableValue.ToString(p.ExcelFormat, _configuration.Culture); var formattedStr = formattableValue.ToString(columnInfo.ExcelFormat, _configuration.Culture);
v = ExcelOpenXmlUtils.EncodeXML(formattedStr); cellValue = ExcelOpenXmlUtils.EncodeXML(formattedStr);
} }
else else
{ {
Type type = null; Type type;
if (p == null || p.Key != null) if (columnInfo == null || columnInfo.Key != null)
{ {
// TODO: need to optimize // TODO: need to optimize
// Dictionary need to check type every time, so it's slow.. // Dictionary need to check type every time, so it's slow..
@ -472,52 +473,52 @@ namespace MiniExcelLibs.OpenXml
} }
else else
{ {
type = p.ExcludeNullableType; //sometime it doesn't need to re-get type like prop type = columnInfo.ExcludeNullableType; //sometime it doesn't need to re-get type like prop
} }
if (type.IsEnum) if (type.IsEnum)
{ {
t = "str"; dataType = "str";
var description = CustomPropertyHelper.DescriptionAttr(type, value); //TODO: need to optimze var description = CustomPropertyHelper.DescriptionAttr(type, value); //TODO: need to optimze
if (description != null) if (description != null)
v = description; cellValue = description;
else else
v = value.ToString(); cellValue = value.ToString();
} }
else if (TypeHelper.IsNumericType(type)) else if (TypeHelper.IsNumericType(type))
{ {
if (_configuration.Culture != CultureInfo.InvariantCulture) if (_configuration.Culture != CultureInfo.InvariantCulture)
t = "str"; //TODO: add style format dataType = "str"; //TODO: add style format
else else
t = "n"; dataType = "n";
if (type.IsAssignableFrom(typeof(decimal))) if (type.IsAssignableFrom(typeof(decimal)))
v = ((decimal)value).ToString(_configuration.Culture); cellValue = ((decimal)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(Int32))) else if (type.IsAssignableFrom(typeof(Int32)))
v = ((Int32)value).ToString(_configuration.Culture); cellValue = ((Int32)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(Double))) else if (type.IsAssignableFrom(typeof(Double)))
v = ((Double)value).ToString(_configuration.Culture); cellValue = ((Double)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(Int64))) else if (type.IsAssignableFrom(typeof(Int64)))
v = ((Int64)value).ToString(_configuration.Culture); cellValue = ((Int64)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(UInt32))) else if (type.IsAssignableFrom(typeof(UInt32)))
v = ((UInt32)value).ToString(_configuration.Culture); cellValue = ((UInt32)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(UInt16))) else if (type.IsAssignableFrom(typeof(UInt16)))
v = ((UInt16)value).ToString(_configuration.Culture); cellValue = ((UInt16)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(UInt64))) else if (type.IsAssignableFrom(typeof(UInt64)))
v = ((UInt64)value).ToString(_configuration.Culture); cellValue = ((UInt64)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(Int16))) else if (type.IsAssignableFrom(typeof(Int16)))
v = ((Int16)value).ToString(_configuration.Culture); cellValue = ((Int16)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(Single))) else if (type.IsAssignableFrom(typeof(Single)))
v = ((Single)value).ToString(_configuration.Culture); cellValue = ((Single)value).ToString(_configuration.Culture);
else if (type.IsAssignableFrom(typeof(Single))) else if (type.IsAssignableFrom(typeof(Single)))
v = ((Single)value).ToString(_configuration.Culture); cellValue = ((Single)value).ToString(_configuration.Culture);
else else
v = (decimal.Parse(value.ToString())).ToString(_configuration.Culture); cellValue = (decimal.Parse(value.ToString())).ToString(_configuration.Culture);
} }
else if (type == typeof(bool)) else if (type == typeof(bool))
{ {
t = "b"; dataType = "b";
v = (bool)value ? "1" : "0"; cellValue = (bool)value ? "1" : "0";
} }
else if (type == typeof(byte[]) && _configuration.EnableConvertByteArray) else if (type == typeof(byte[]) && _configuration.EnableConvertByteArray)
{ {
@ -549,38 +550,61 @@ namespace MiniExcelLibs.OpenXml
//TODO:Convert to base64 //TODO:Convert to base64
var base64 = $"@@@fileid@@@,{file.Path}"; var base64 = $"@@@fileid@@@,{file.Path}";
v = ExcelOpenXmlUtils.EncodeXML(base64); cellValue = ExcelOpenXmlUtils.EncodeXML(base64);
s = "4"; styleIndex = "4";
} }
} }
else if (type == typeof(DateTime)) else if (type == typeof(DateTime))
{ {
if (_configuration.Culture != CultureInfo.InvariantCulture) if (_configuration.Culture != CultureInfo.InvariantCulture)
{ {
t = "str"; dataType = "str";
v = ((DateTime)value).ToString(_configuration.Culture); cellValue = ((DateTime)value).ToString(_configuration.Culture);
} }
else if (p == null || p.ExcelFormat == null) else if (columnInfo == null || columnInfo.ExcelFormat == null)
{ {
t = null; dataType = null;
s = "3"; styleIndex = "3";
v = ((DateTime)value).ToOADate().ToString(CultureInfo.InvariantCulture); cellValue = ((DateTime)value).ToOADate().ToString(CultureInfo.InvariantCulture);
} }
else else
{ {
// TODO: now it'll lose date type information // TODO: now it'll lose date type information
t = "str"; dataType = "str";
v = ((DateTime)value).ToString(p.ExcelFormat, _configuration.Culture); cellValue = ((DateTime)value).ToString(columnInfo.ExcelFormat, _configuration.Culture);
} }
} }
#if NETSTANDARD2_0_OR_GREATER
else if (type == typeof(DateOnly))
{
if (_configuration.Culture != CultureInfo.InvariantCulture)
{
dataType = "str";
cellValue = ((DateOnly)value).ToString(_configuration.Culture);
}
else if (columnInfo == null || columnInfo.ExcelFormat == null)
{
var day = (DateOnly)value;
dataType = "n";
styleIndex = "3";
cellValue = day.ToDateTime(TimeOnly.MinValue).ToOADate().ToString(CultureInfo.InvariantCulture);
}
else
{
// TODO: now it'll lose date type information
dataType = "str";
cellValue = ((DateOnly)value).ToString(columnInfo.ExcelFormat, _configuration.Culture);
}
}
#endif
else else
{ {
//TODO: _configuration.Culture //TODO: _configuration.Culture
v = ExcelOpenXmlUtils.EncodeXML(value.ToString()); cellValue = ExcelOpenXmlUtils.EncodeXML(value.ToString());
} }
} }
return Tuple.Create(s, t, v); return Tuple.Create(styleIndex, dataType, cellValue);
} }
private void GenerateSheetByDataTable(MiniExcelStreamWriter writer, DataTable value) private void GenerateSheetByDataTable(MiniExcelStreamWriter writer, DataTable value)
@ -631,7 +655,7 @@ namespace MiniExcelLibs.OpenXml
for (int j = 0; j < value.Columns.Count; j++) for (int j = 0; j < value.Columns.Count; j++)
{ {
var cellValue = value.Rows[i][j]; var cellValue = value.Rows[i][j];
WriteCell(writer, yIndex, xIndex, cellValue, null); WriteCell(writer, yIndex, xIndex, cellValue, columnInfo: null);
xIndex++; xIndex++;
} }
writer.Write($"</x:row>"); writer.Write($"</x:row>");
@ -683,7 +707,7 @@ namespace MiniExcelLibs.OpenXml
for (int i = 0; i < fieldCount; i++) for (int i = 0; i < fieldCount; i++)
{ {
var cellValue = reader.GetValue(i); var cellValue = reader.GetValue(i);
WriteCell(writer, yIndex, xIndex, cellValue, null); WriteCell(writer, yIndex, xIndex, cellValue, columnInfo: null);
xIndex++; xIndex++;
} }
writer.Write($"</x:row>"); writer.Write($"</x:row>");

View File

@ -199,11 +199,11 @@ namespace MiniExcelLibs.OpenXml
ColIndex = StringHelper.GetLetter(att), ColIndex = StringHelper.GetLetter(att),
RowIndex = StringHelper.GetNumber(att) RowIndex = StringHelper.GetNumber(att)
}; };
}).OrderBy(x=>x.RowIndex).ToList(); }).OrderBy(x => x.RowIndex).ToList();
var mergeColumns = columns.Where(s => s.InnerText.Contains("@merge")).ToList(); var mergeColumns = columns.Where(s => s.InnerText.Contains("@merge")).ToList();
var endMergeColumns = columns.Where(s => s.InnerText.Contains("@endmerge")).ToList(); var endMergeColumns = columns.Where(s => s.InnerText.Contains("@endmerge")).ToList();
var mergeLimitColumn = mergeColumns.FirstOrDefault(x=>x.InnerText.Contains("@mergelimit")); var mergeLimitColumn = mergeColumns.FirstOrDefault(x => x.InnerText.Contains("@mergelimit"));
foreach (var mergeColumn in mergeColumns) foreach (var mergeColumn in mergeColumns)
{ {
@ -242,7 +242,7 @@ namespace MiniExcelLibs.OpenXml
var childNodeLetter = StringHelper.GetLetter(att); var childNodeLetter = StringHelper.GetLetter(att);
var childNodeNumber = StringHelper.GetNumber(att); var childNodeNumber = StringHelper.GetNumber(att);
if(!string.IsNullOrEmpty(childNode.InnerText)) if (!string.IsNullOrEmpty(childNode.InnerText))
{ {
var xmlNodes = calculatedColumns var xmlNodes = calculatedColumns
.Where(j => j.InnerText == childNode.InnerText && j.ColIndex == childNodeLetter) .Where(j => j.InnerText == childNode.InnerText && j.ColIndex == childNodeLetter)
@ -356,7 +356,7 @@ namespace MiniExcelLibs.OpenXml
{ {
isHeaderRow = true; isHeaderRow = true;
} }
else if(row.InnerText.Contains("@merge") && mergeCells) else if (row.InnerText.Contains("@merge") && mergeCells)
{ {
mergeRowCount++; mergeRowCount++;
continue; continue;
@ -616,7 +616,7 @@ namespace MiniExcelLibs.OpenXml
rowXml.Replace(extract, newCellValue); rowXml.Replace(extract, newCellValue);
} }
foreach (var propInfo in rowInfo.PropsMap ) foreach (var propInfo in rowInfo.PropsMap)
{ {
var prop = propInfo.Value.PropertyInfo; var prop = propInfo.Value.PropertyInfo;
@ -950,9 +950,12 @@ namespace MiniExcelLibs.OpenXml
foreach (var p in props) foreach (var p in props)
{ {
values.Add(p.Name, new PropInfo { PropertyInfo = p, values.Add(p.Name, new PropInfo
{
PropertyInfo = p,
PropertyInfoOrFieldInfo = PropertyInfoOrFieldInfo.PropertyInfo, PropertyInfoOrFieldInfo = PropertyInfoOrFieldInfo.PropertyInfo,
UnderlyingTypePropType = Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType }); UnderlyingTypePropType = Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType
});
} }
var fields = xRowInfo.IEnumerableGenricType.GetFields(); var fields = xRowInfo.IEnumerableGenricType.GetFields();
@ -961,9 +964,12 @@ namespace MiniExcelLibs.OpenXml
if (!values.ContainsKey(f.Name)) if (!values.ContainsKey(f.Name))
{ {
values.Add(f.Name, new PropInfo { FieldInfo = f, values.Add(f.Name, new PropInfo
{
FieldInfo = f,
PropertyInfoOrFieldInfo = PropertyInfoOrFieldInfo.FieldInfo, PropertyInfoOrFieldInfo = PropertyInfoOrFieldInfo.FieldInfo,
UnderlyingTypePropType = Nullable.GetUnderlyingType(f.FieldType) ?? f.FieldType }); UnderlyingTypePropType = Nullable.GetUnderlyingType(f.FieldType) ?? f.FieldType
});
} }
} }

View File

@ -1,4 +1,6 @@
using System; using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
@ -6,8 +8,6 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
namespace MiniExcelLibs.OpenXml namespace MiniExcelLibs.OpenXml
{ {
@ -66,12 +66,12 @@ namespace MiniExcelLibs.OpenXml
public Task MergeSameCellsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)) public Task MergeSameCellsAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
{ {
return Task.Run(() => MergeSameCells(path),cancellationToken); return Task.Run(() => MergeSameCells(path), cancellationToken);
} }
public Task MergeSameCellsAsync(byte[] fileInBytes, CancellationToken cancellationToken = default(CancellationToken)) public Task MergeSameCellsAsync(byte[] fileInBytes, CancellationToken cancellationToken = default(CancellationToken))
{ {
return Task.Run(() => MergeSameCells(fileInBytes),cancellationToken); return Task.Run(() => MergeSameCells(fileInBytes), cancellationToken);
} }
} }
} }

View File

@ -4,14 +4,12 @@ namespace MiniExcelLibs.OpenXml
using MiniExcelLibs.Utils; using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip; using MiniExcelLibs.Zip;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
@ -127,7 +125,7 @@ namespace MiniExcelLibs.OpenXml
} }
} }
var calcChain = _archive.zipFile.Entries.FirstOrDefault( e => e.FullName.Contains("xl/calcChain.xml")); var calcChain = _archive.zipFile.Entries.FirstOrDefault(e => e.FullName.Contains("xl/calcChain.xml"));
if (calcChain != null) if (calcChain != null)
{ {
string calcChainPathname = calcChain.FullName; string calcChainPathname = calcChain.FullName;

View File

@ -4,11 +4,11 @@
namespace MiniExcelLibs.OpenXml namespace MiniExcelLibs.OpenXml
{ {
using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System;
using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
internal class MiniExcelAsyncStreamWriter : IDisposable internal class MiniExcelAsyncStreamWriter : IDisposable
{ {

View File

@ -10,7 +10,7 @@ namespace MiniExcelLibs.OpenXml
private readonly Encoding _encoding; private readonly Encoding _encoding;
private readonly StreamWriter _streamWriter; private readonly StreamWriter _streamWriter;
private bool disposedValue; private bool disposedValue;
public MiniExcelStreamWriter(Stream stream,Encoding encoding, int bufferSize) public MiniExcelStreamWriter(Stream stream, Encoding encoding, int bufferSize)
{ {
this._stream = stream; this._stream = stream;
this._encoding = encoding; this._encoding = encoding;

View File

@ -10,7 +10,7 @@ namespace MiniExcelLibs
{ {
} }
public class Property: Member public class Property : Member
{ {
private static readonly ConcurrentDictionary<Type, Property[]> m_cached = new ConcurrentDictionary<Type, Property[]>(); private static readonly ConcurrentDictionary<Type, Property[]> m_cached = new ConcurrentDictionary<Type, Property[]>();

View File

@ -1,8 +1,6 @@
namespace MiniExcelLibs.Utils namespace MiniExcelLibs.Utils
{ {
using MiniExcelLibs.Utils;
using System; using System;
using System.Globalization;
#if DEBUG #if DEBUG
public public

View File

@ -658,10 +658,10 @@ namespace MiniExcelLibs.Utils
public static bool IsLiteral(string token) public static bool IsLiteral(string token)
{ {
return return
token.StartsWith("_",StringComparison.Ordinal) || token.StartsWith("_", StringComparison.Ordinal) ||
token.StartsWith("\\",StringComparison.Ordinal) || token.StartsWith("\\", StringComparison.Ordinal) ||
token.StartsWith("\"",StringComparison.Ordinal) || token.StartsWith("\"", StringComparison.Ordinal) ||
token.StartsWith("*",StringComparison.Ordinal) || token.StartsWith("*", StringComparison.Ordinal) ||
token == "," || token == "," ||
token == "!" || token == "!" ||
token == "&" || token == "&" ||

View File

@ -1,9 +1,5 @@
namespace MiniExcelLibs.Utils namespace MiniExcelLibs.Utils
{ {
using System;
using System.Linq;
using System.Text;
internal class ImageHelper internal class ImageHelper
{ {
public enum ImageFormat public enum ImageFormat

View File

@ -31,12 +31,12 @@
while (!reader.EOF) while (!reader.EOF)
{ {
if (XmlReaderHelper.IsStartElement(reader,"t", _ns)) if (XmlReaderHelper.IsStartElement(reader, "t", _ns))
{ {
// There are multiple <t> in a <si>. Concatenate <t> within an <si>. // There are multiple <t> in a <si>. Concatenate <t> within an <si>.
result.Append(reader.ReadElementContentAsString()); result.Append(reader.ReadElementContentAsString());
} }
else if (XmlReaderHelper.IsStartElement(reader,"r", _ns)) else if (XmlReaderHelper.IsStartElement(reader, "r", _ns))
{ {
result.Append(ReadRichTextRun(reader)); result.Append(ReadRichTextRun(reader));
} }
@ -60,7 +60,7 @@
while (!reader.EOF) while (!reader.EOF)
{ {
if (XmlReaderHelper.IsStartElement(reader,"t", _ns)) if (XmlReaderHelper.IsStartElement(reader, "t", _ns))
{ {
result.Append(reader.ReadElementContentAsString()); result.Append(reader.ReadElementContentAsString());
} }

View File

@ -1,6 +1,5 @@
namespace MiniExcelLibs.Utils namespace MiniExcelLibs.Utils
{ {
using MiniExcelLibs.Attributes;
using MiniExcelLibs.Exceptions; using MiniExcelLibs.Exceptions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,11 +1,5 @@
using System; using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.ComTypes;
using System.Text; using System.Text;
using System.Threading.Tasks;
using System.Xml; using System.Xml;
namespace MiniExcelLibs.Utils namespace MiniExcelLibs.Utils

View File

@ -1,5 +1,4 @@
using System; using System.IO.Compression;
using System.IO.Compression;
namespace MiniExcelLibs.Zip namespace MiniExcelLibs.Zip
{ {

View File

@ -22,9 +22,9 @@ namespace MiniExcelLibs.Tests
var path = PathHelper.GetFile("csv/gb2312_Encoding_Read_Test.csv"); var path = PathHelper.GetFile("csv/gb2312_Encoding_Read_Test.csv");
var config = new MiniExcelLibs.Csv.CsvConfiguration() var config = new MiniExcelLibs.Csv.CsvConfiguration()
{ {
StreamReaderFunc = (stream) => new StreamReader(stream,encoding: Encoding.GetEncoding("gb2312")) StreamReaderFunc = (stream) => new StreamReader(stream, encoding: Encoding.GetEncoding("gb2312"))
}; };
var rows = MiniExcel.Query(path, true,excelType:ExcelType.CSV,configuration: config).ToList(); var rows = MiniExcel.Query(path, true, excelType: ExcelType.CSV, configuration: config).ToList();
Assert.Equal("世界你好", rows[0].1); Assert.Equal("世界你好", rows[0].1);
} }
@ -37,7 +37,7 @@ namespace MiniExcelLibs.Tests
new Dictionary<string,object>{{ "a", @"""<>+-*//}{\\n" }, { "b", 1234567890 },{ "c", true },{ "d", new DateTime(2021, 1, 1) } }, new Dictionary<string,object>{{ "a", @"""<>+-*//}{\\n" }, { "b", 1234567890 },{ "c", true },{ "d", new DateTime(2021, 1, 1) } },
new Dictionary<string,object>{{ "a", @"<test>Hello World</test>" }, { "b", -1234567890 },{ "c", false },{ "d", new DateTime(2021, 1, 2) } }, new Dictionary<string,object>{{ "a", @"<test>Hello World</test>" }, { "b", -1234567890 },{ "c", false },{ "d", new DateTime(2021, 1, 2) } },
}; };
MiniExcel.SaveAs(path, values,configuration: new MiniExcelLibs.Csv.CsvConfiguration() {Seperator=';'}); MiniExcel.SaveAs(path, values, configuration: new MiniExcelLibs.Csv.CsvConfiguration() { Seperator = ';' });
var expected = @"a;b;c;d var expected = @"a;b;c;d
""""""<>+-*//}{\\n"";1234567890;True;""2021-01-01 00:00:00"" """"""<>+-*//}{\\n"";1234567890;True;""2021-01-01 00:00:00""
""<test>Hello World</test>"";-1234567890;False;""2021-01-02 00:00:00"" ""<test>Hello World</test>"";-1234567890;False;""2021-01-02 00:00:00""
@ -54,7 +54,7 @@ namespace MiniExcelLibs.Tests
new Dictionary<string,object>{{ "a", @"""<>+-*//}{\\n" }, { "b", 1234567890 },{ "c", true },{ "d", new DateTime(2021, 1, 1) } }, new Dictionary<string,object>{{ "a", @"""<>+-*//}{\\n" }, { "b", 1234567890 },{ "c", true },{ "d", new DateTime(2021, 1, 1) } },
new Dictionary<string,object>{{ "a", @"<test>Hello World</test>" }, { "b", -1234567890 },{ "c", false },{ "d", new DateTime(2021, 1, 2) } }, new Dictionary<string,object>{{ "a", @"<test>Hello World</test>" }, { "b", -1234567890 },{ "c", false },{ "d", new DateTime(2021, 1, 2) } },
}; };
MiniExcel.SaveAs(path, values,configuration: new MiniExcelLibs.Csv.CsvConfiguration() {AlwaysQuote = true}); MiniExcel.SaveAs(path, values, configuration: new MiniExcelLibs.Csv.CsvConfiguration() { AlwaysQuote = true });
var expected = @"""a"",""b"",""c"",""d"" var expected = @"""a"",""b"",""c"",""d""
""""""<>+-*//}{\\n"",""1234567890"",""True"",""2021-01-01 00:00:00"" """"""<>+-*//}{\\n"",""1234567890"",""True"",""2021-01-01 00:00:00""
""<test>Hello World</test>"",""-1234567890"",""False"",""2021-01-02 00:00:00"" ""<test>Hello World</test>"",""-1234567890"",""False"",""2021-01-02 00:00:00""
@ -70,7 +70,7 @@ namespace MiniExcelLibs.Tests
{ {
new Dictionary<string,object>{{ "a", @"potato,banana" }, { "b", "text\ntest" },{ "c", "text\rpotato" },{ "d", new DateTime(2021, 1, 1) } }, new Dictionary<string,object>{{ "a", @"potato,banana" }, { "b", "text\ntest" },{ "c", "text\rpotato" },{ "d", new DateTime(2021, 1, 1) } },
}; };
MiniExcel.SaveAs(path, values,configuration: new MiniExcelLibs.Csv.CsvConfiguration()); MiniExcel.SaveAs(path, values, configuration: new MiniExcelLibs.Csv.CsvConfiguration());
var expected = "a,b,c,d\r\n\"potato,banana\",\"text\ntest\",\"text\rpotato\",\"2021-01-01 00:00:00\"\r\n"; var expected = "a,b,c,d\r\n\"potato,banana\",\"text\ntest\",\"text\rpotato\",\"2021-01-01 00:00:00\"\r\n";
Assert.Equal(expected, File.ReadAllText(path)); Assert.Equal(expected, File.ReadAllText(path));
} }
@ -259,7 +259,7 @@ namespace MiniExcelLibs.Tests
using (var stream = File.OpenRead(path)) using (var stream = File.OpenRead(path))
{ {
var rows = stream.Query(useHeaderRow: true,excelType:ExcelType.CSV).ToList(); var rows = stream.Query(useHeaderRow: true, excelType: ExcelType.CSV).ToList();
Assert.Equal("A1", rows[0].c1); Assert.Equal("A1", rows[0].c1);
Assert.Equal("B1", rows[0].c2); Assert.Equal("B1", rows[0].c2);
Assert.Equal("A2", rows[1].c1); Assert.Equal("A2", rows[1].c1);
@ -267,7 +267,7 @@ namespace MiniExcelLibs.Tests
} }
{ {
var rows = MiniExcel.Query(path,useHeaderRow: true, excelType: ExcelType.CSV).ToList(); var rows = MiniExcel.Query(path, useHeaderRow: true, excelType: ExcelType.CSV).ToList();
Assert.Equal("A1", rows[0].c1); Assert.Equal("A1", rows[0].c1);
Assert.Equal("B1", rows[0].c2); Assert.Equal("B1", rows[0].c2);
Assert.Equal("A2", rows[1].c1); Assert.Equal("A2", rows[1].c1);
@ -398,7 +398,7 @@ namespace MiniExcelLibs.Tests
using (var stream = File.Create(path)) using (var stream = File.Create(path))
{ {
stream.SaveAs(records,excelType:ExcelType.CSV); stream.SaveAs(records, excelType: ExcelType.CSV);
} }
var content = File.ReadAllText(path); var content = File.ReadAllText(path);

View File

@ -158,14 +158,16 @@ namespace MiniExcelLibs.Tests
{ {
var path = PathHelper.GetFile("xls/TestIssue242.xls"); var path = PathHelper.GetFile("xls/TestIssue242.xls");
await Assert.ThrowsAsync<NotSupportedException>(async () => { await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
var q = await MiniExcel.QueryAsync(path); var q = await MiniExcel.QueryAsync(path);
q.ToList(); q.ToList();
}); });
using (var stream = File.OpenRead(path)) using (var stream = File.OpenRead(path))
{ {
await Assert.ThrowsAsync<NotSupportedException>(async () => { await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
var q = await stream.QueryAsync(); var q = await stream.QueryAsync();
q.ToList(); q.ToList();
}); });
@ -801,7 +803,7 @@ namespace MiniExcelLibs.Tests
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
stream.SaveAs(new[] { new { V = "test1" }, new { V = "test2" } }); stream.SaveAs(new[] { new { V = "test1" }, new { V = "test2" } });
var q = (await stream.QueryAsync(true)).Cast<IDictionary<string,object>>(); var q = (await stream.QueryAsync(true)).Cast<IDictionary<string, object>>();
var rows = q.ToList(); var rows = q.ToList();
Assert.Equal("test1", rows[0]["V"]); Assert.Equal("test1", rows[0]["V"]);
Assert.Equal("test2", rows[1]["V"]); Assert.Equal("test2", rows[1]["V"]);
@ -1369,7 +1371,7 @@ MyProperty4,MyProperty1,MyProperty5,MyProperty2,MyProperty6,,MyProperty3
{ {
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx"); var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx");
//MiniExcel.SaveAs(path, new[] { "1", "2" }); //MiniExcel.SaveAs(path, new[] { "1", "2" });
await Assert.ThrowsAnyAsync<NotImplementedException>(async() => await MiniExcel.SaveAsAsync(path, new[] { 1, 2 })); await Assert.ThrowsAnyAsync<NotImplementedException>(async () => await MiniExcel.SaveAsAsync(path, new[] { 1, 2 }));
File.Delete(path); File.Delete(path);
await Assert.ThrowsAnyAsync<NotImplementedException>(async () => await MiniExcel.SaveAsAsync(path, new[] { "1", "2" })); await Assert.ThrowsAnyAsync<NotImplementedException>(async () => await MiniExcel.SaveAsAsync(path, new[] { "1", "2" }));
File.Delete(path); File.Delete(path);

View File

@ -1,10 +1,13 @@
using Dapper; using Dapper;
using MiniExcelLibs.Attributes; using MiniExcelLibs.Attributes;
using MiniExcelLibs.Csv;
using MiniExcelLibs.Exceptions;
using MiniExcelLibs.OpenXml; using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Tests.Utils; using MiniExcelLibs.Tests.Utils;
using Newtonsoft.Json; using Newtonsoft.Json;
using OfficeOpenXml; using OfficeOpenXml;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -13,28 +16,23 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
using static MiniExcelLibs.Tests.MiniExcelOpenXmlTests; using static MiniExcelLibs.Tests.MiniExcelOpenXmlTests;
using System.Collections;
using MiniExcelLibs.Exceptions;
using System.Text.RegularExpressions;
using MiniExcelLibs.Csv;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace MiniExcelLibs.Tests namespace MiniExcelLibs.Tests
{ {
public partial class MiniExcelIssueTests public partial class MiniExcelIssueTests
{ {
private readonly ITestOutputHelper output; private readonly ITestOutputHelper output;
public MiniExcelIssueTests(ITestOutputHelper output) public MiniExcelIssueTests(ITestOutputHelper output)
{ {
this.output = output; this.output = output;
} }
[Fact] [Fact]
public void TestIssue24020201() public void TestIssue24020201()
{ {
@ -43,7 +41,7 @@ namespace MiniExcelLibs.Tests
var templatePath = PathHelper.GetFile("xlsx/TestIssue24020201.xlsx"); var templatePath = PathHelper.GetFile("xlsx/TestIssue24020201.xlsx");
var data = new Dictionary<string, object>() var data = new Dictionary<string, object>()
{ {
["title"]="This's title", ["title"] = "This's title",
["B"] = new List<Dictionary<string, object>>() ["B"] = new List<Dictionary<string, object>>()
{ {
new Dictionary<string, object>(){ { "specialMark", 1 } }, new Dictionary<string, object>(){ { "specialMark", 1 } },
@ -60,9 +58,12 @@ namespace MiniExcelLibs.Tests
{ {
var path = PathHelper.GetTempFilePath(); var path = PathHelper.GetTempFilePath();
var templatePath = PathHelper.GetFile("xlsx/TestIssue553.xlsx"); var templatePath = PathHelper.GetFile("xlsx/TestIssue553.xlsx");
var data = new { B = new[] { var data = new
{
B = new[] {
new{ ITM=1 },new{ ITM=2 }, new{ ITM=3 }, new{ ITM=1 },new{ ITM=2 }, new{ ITM=3 },
} }; }
};
MiniExcel.SaveAsByTemplate(path, templatePath, data); MiniExcel.SaveAsByTemplate(path, templatePath, data);
} }
@ -132,7 +133,7 @@ namespace MiniExcelLibs.Tests
", content); ", content);
} }
{ {
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) }; var value = new { ID = 3, Name = "Mike", InDate = new DateTime(2021, 04, 23) };
MiniExcel.Insert(path, value); MiniExcel.Insert(path, value);
var content = File.ReadAllText(path); var content = File.ReadAllText(path);
Assert.Equal(@"ID,Name,InDate Assert.Equal(@"ID,Name,InDate
@ -194,7 +195,7 @@ namespace MiniExcelLibs.Tests
var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx"; var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
DataTableReader reader = table.CreateDataReader(); DataTableReader reader = table.CreateDataReader();
var config = new OpenXmlConfiguration() { FastMode = true }; var config = new OpenXmlConfiguration() { FastMode = true };
MiniExcel.SaveAs(path, reader,configuration:config); MiniExcel.SaveAs(path, reader, configuration: config);
var xml = Helpers.GetZipFileContent(path, "xl/worksheets/sheet1.xml"); var xml = Helpers.GetZipFileContent(path, "xl/worksheets/sheet1.xml");
Assert.Contains("<x:autoFilter ref=\"A1:B3\" />", xml); Assert.Contains("<x:autoFilter ref=\"A1:B3\" />", xml);
Assert.Contains("<x:dimension ref=\"A1:B3\" />", xml); Assert.Contains("<x:dimension ref=\"A1:B3\" />", xml);

View File

@ -1,21 +1,21 @@
using Xunit; using ClosedXML.Excel;
using System;
using System.Linq;
using System.IO;
using OfficeOpenXml;
using ClosedXML.Excel;
using System.IO.Packaging;
using System.Data;
using ExcelDataReader;
using System.Collections.Generic;
using System.Data.SQLite;
using Dapper; using Dapper;
using System.Globalization; using ExcelDataReader;
using static MiniExcelLibs.Tests.Utils.MiniExcelOpenXml;
using MiniExcelLibs.Tests.Utils;
using MiniExcelLibs.Attributes; using MiniExcelLibs.Attributes;
using System.Threading.Tasks; using MiniExcelLibs.Tests.Utils;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Globalization;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Xunit;
using static MiniExcelLibs.Tests.Utils.MiniExcelOpenXml;
namespace MiniExcelLibs.Tests namespace MiniExcelLibs.Tests
{ {
@ -208,7 +208,7 @@ namespace MiniExcelLibs.Tests
var path = @"../../../../../samples/xlsx/TestCenterEmptyRow/TestCenterEmptyRow.xlsx"; var path = @"../../../../../samples/xlsx/TestCenterEmptyRow/TestCenterEmptyRow.xlsx";
using (var stream = File.OpenRead(path)) using (var stream = File.OpenRead(path))
{ {
var d = (await stream.QueryAsync()).Cast<IDictionary<string,object>>(); var d = (await stream.QueryAsync()).Cast<IDictionary<string, object>>();
var rows = d.ToList(); var rows = d.ToList();
Assert.Equal("a", rows[0]["A"]); Assert.Equal("a", rows[0]["A"]);
Assert.Equal("b", rows[0]["B"]); Assert.Equal("b", rows[0]["B"]);
@ -279,7 +279,7 @@ namespace MiniExcelLibs.Tests
var path = @"../../../../../samples/xlsx/TestDynamicQueryBasic_WithoutHead.xlsx"; var path = @"../../../../../samples/xlsx/TestDynamicQueryBasic_WithoutHead.xlsx";
using (var stream = File.OpenRead(path)) using (var stream = File.OpenRead(path))
{ {
var d= (await stream.QueryAsync()).Cast<IDictionary<string, object>>(); var d = (await stream.QueryAsync()).Cast<IDictionary<string, object>>();
var rows = d.ToList(); var rows = d.ToList();
Assert.Equal("MiniExcel", rows[0]["A"]); Assert.Equal("MiniExcel", rows[0]["A"]);
Assert.Equal(1d, rows[0]["B"]); Assert.Equal(1d, rows[0]["B"]);
@ -377,7 +377,7 @@ namespace MiniExcelLibs.Tests
var path = @"../../../../../samples/xlsx/TestTypeMapping_AutoCheckFormat.xlsx"; var path = @"../../../../../samples/xlsx/TestTypeMapping_AutoCheckFormat.xlsx";
using (var stream = FileHelper.OpenRead(path)) using (var stream = FileHelper.OpenRead(path))
{ {
var d= await stream.QueryAsync<AutoCheckType>(); var d = await stream.QueryAsync<AutoCheckType>();
d.ToList(); d.ToList();
} }
} }
@ -661,14 +661,14 @@ namespace MiniExcelLibs.Tests
var rows = d.ToList(); var rows = d.ToList();
Assert.Equal(2, rows.Count); Assert.Equal(2, rows.Count);
Assert.Equal(@"""<>+-*//}{\\n", rows[0]["a"]); Assert.Equal(@"""<>+-*//}{\\n", rows[0]["a"]);
Assert.Equal(1234567890d,rows[0]["b"]); Assert.Equal(1234567890d, rows[0]["b"]);
Assert.Null(rows[0]["c"]); Assert.Null(rows[0]["c"]);
Assert.Null(rows[0]["d"]); Assert.Null(rows[0]["d"]);
} }
using (var stream = File.OpenRead(path)) using (var stream = File.OpenRead(path))
{ {
var d= (await stream.QueryAsync()).Cast<IDictionary<string, object>>(); var d = (await stream.QueryAsync()).Cast<IDictionary<string, object>>();
var rows = d.ToList(); var rows = d.ToList();
Assert.Equal(3, rows.Count); Assert.Equal(3, rows.Count);
Assert.Equal("a", rows[0]["A"]); Assert.Equal("a", rows[0]["A"]);
@ -1020,7 +1020,7 @@ namespace MiniExcelLibs.Tests
{ {
var rows = (await stream.QueryAsync()).Cast<IDictionary<string, object>>(); var rows = (await stream.QueryAsync()).Cast<IDictionary<string, object>>();
foreach (var row in rows) foreach (var row in rows)
connection.Execute("insert into T (A,B) values (@A,@B)", new { A = row["A"],B = row["B"] }, transaction: transaction); connection.Execute("insert into T (A,B) values (@A,@B)", new { A = row["A"], B = row["B"] }, transaction: transaction);
transaction.Commit(); transaction.Commit();
} }
} }
@ -1205,7 +1205,8 @@ namespace MiniExcelLibs.Tests
[Fact()] [Fact()]
public async Task ReadBigExcel_TakeCancel_Throws_TaskCanceledException() public async Task ReadBigExcel_TakeCancel_Throws_TaskCanceledException()
{ {
await Assert.ThrowsAsync<TaskCanceledException>(async () => { await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
CancellationTokenSource cts = new CancellationTokenSource(); CancellationTokenSource cts = new CancellationTokenSource();
var path = @"../../../../../samples/xlsx/bigExcel.xlsx"; var path = @"../../../../../samples/xlsx/bigExcel.xlsx";
@ -1222,7 +1223,8 @@ namespace MiniExcelLibs.Tests
[Fact()] [Fact()]
public async Task ReadBigExcel_Prcoessing_TakeCancel_Throws_TaskCanceledException() public async Task ReadBigExcel_Prcoessing_TakeCancel_Throws_TaskCanceledException()
{ {
await Assert.ThrowsAsync<OperationCanceledException>(async () => { await Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
CancellationTokenSource cts = new CancellationTokenSource(); CancellationTokenSource cts = new CancellationTokenSource();
var path = @"../../../../../samples/xlsx/bigExcel.xlsx"; var path = @"../../../../../samples/xlsx/bigExcel.xlsx";

View File

@ -1,9 +1,9 @@
using Xunit; using System;
using System.Linq;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace MiniExcelLibs.Tests namespace MiniExcelLibs.Tests
{ {
@ -35,8 +35,9 @@ namespace MiniExcelLibs.Tests
Assert.Equal(2, rows[0].B); Assert.Equal(2, rows[0].B);
} }
{ {
await Assert.ThrowsAsync<InvalidOperationException>(async() => { await Assert.ThrowsAsync<InvalidOperationException>(async () =>
var q= await MiniExcel.QueryAsync(path, sheetName: "xxxx"); {
var q = await MiniExcel.QueryAsync(path, sheetName: "xxxx");
q.ToList(); q.ToList();
}); });
} }

View File

@ -1,22 +1,22 @@
using Xunit; using ClosedXML.Excel;
using System;
using System.Linq;
using System.IO;
using OfficeOpenXml;
using ClosedXML.Excel;
using System.IO.Packaging;
using System.Data;
using ExcelDataReader;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Diagnostics;
using Dapper; using Dapper;
using System.Globalization; using ExcelDataReader;
using static MiniExcelLibs.Tests.Utils.MiniExcelOpenXml;
using MiniExcelLibs.Tests.Utils;
using MiniExcelLibs.Attributes; using MiniExcelLibs.Attributes;
using MiniExcelLibs.OpenXml; using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Tests.Utils;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
using static MiniExcelLibs.Tests.Utils.MiniExcelOpenXml;
namespace MiniExcelLibs.Tests namespace MiniExcelLibs.Tests
{ {
@ -1177,12 +1177,12 @@ namespace MiniExcelLibs.Tests
Assert.Equal(new[] { "A", "B", "C" }, columns); Assert.Equal(new[] { "A", "B", "C" }, columns);
var rows = MiniExcel.Query(path).ToList(); var rows = MiniExcel.Query(path).ToList();
Assert.Equal(rows[0].A , "title1"); Assert.Equal(rows[0].A, "title1");
Assert.Equal(rows[0].B , "title2"); Assert.Equal(rows[0].B, "title2");
Assert.Equal(rows[0].C , "title3"); Assert.Equal(rows[0].C, "title3");
Assert.Equal(rows[1].A , "value1"); Assert.Equal(rows[1].A, "value1");
Assert.Equal(rows[1].B , "value2"); Assert.Equal(rows[1].B, "value2");
Assert.Equal(rows[1].C , "value3"); Assert.Equal(rows[1].C, "value3");
} }
[Fact] [Fact]
@ -1250,7 +1250,7 @@ namespace MiniExcelLibs.Tests
using (var stream = File.OpenRead(path)) using (var stream = File.OpenRead(path))
{ {
var rows = stream.Query(useHeaderRow: true) var rows = stream.Query(useHeaderRow: true)
.Select(x => (IDictionary<string,object>)x) .Select(x => (IDictionary<string, object>)x)
.ToList(); .ToList();
Assert.Contains("Name of something", rows[0]); Assert.Contains("Name of something", rows[0]);

View File

@ -8,8 +8,6 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml;
using MiniExcelLibs.OpenXml;
using Xunit; using Xunit;
namespace MiniExcelTests namespace MiniExcelTests
@ -890,7 +888,8 @@ namespace MiniExcelTests
[Fact()] [Fact()]
public async Task SaveAsByTemplateAsync_TakeCancel_Throws_TaskCanceledException() public async Task SaveAsByTemplateAsync_TakeCancel_Throws_TaskCanceledException()
{ {
await Assert.ThrowsAsync<TaskCanceledException>(async () => { await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
CancellationTokenSource cts = new CancellationTokenSource(); CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel(); cts.Cancel();

View File

@ -4,10 +4,9 @@
internal static class Db internal static class Db
{ {
internal static SQLiteConnection GetConnection(string connectionString= "Data Source=:memory:") internal static SQLiteConnection GetConnection(string connectionString = "Data Source=:memory:")
{ {
return new SQLiteConnection(connectionString); return new SQLiteConnection(connectionString);
} }
} }
} }

View File

@ -1,6 +1,5 @@
namespace MiniExcelLibs.Tests.Utils namespace MiniExcelLibs.Tests.Utils
{ {
using MiniExcelLibs.OpenXml;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -69,14 +68,14 @@
} }
return result; return result;
} }
internal static string GetZipFileContent(string zipPath,string filePath) internal static string GetZipFileContent(string zipPath, string filePath)
{ {
var ns = new XmlNamespaceManager(new NameTable()); var ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"); ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
using (var stream = File.OpenRead(zipPath)) using (var stream = File.OpenRead(zipPath))
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, false, Encoding.UTF8)) using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, false, Encoding.UTF8))
{ {
var sheet = archive.Entries.Single(w => w.FullName== filePath); var sheet = archive.Entries.Single(w => w.FullName == filePath);
using (var sheetStream = sheet.Open()) using (var sheetStream = sheet.Open())
{ {
var doc = XDocument.Load(sheetStream); var doc = XDocument.Load(sheetStream);
@ -124,7 +123,7 @@
doc.Load(sheetStream); doc.Load(sheetStream);
var mergeCells = doc.SelectSingleNode($"/x:worksheet/x:mergeCells", ns)?.Cast<XmlElement>().ToList(); var mergeCells = doc.SelectSingleNode($"/x:worksheet/x:mergeCells", ns)?.Cast<XmlElement>().ToList();
if(mergeCells is { Count: > 0 }) if (mergeCells is { Count: > 0 })
{ {
for (int i = 0; i < mergeCells.Count; i++) for (int i = 0; i < mergeCells.Count; i++)
{ {