diff --git a/README.md b/README.md index 8296d28..57c9023 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ [![NuGet](https://img.shields.io/nuget/v/MiniExcel.svg)](https://www.nuget.org/packages/MiniExcel) [![](https://img.shields.io/nuget/dt/MiniExcel.svg)](https://www.nuget.org/packages/MiniExcel) [![Build status](https://ci.appveyor.com/api/projects/status/b2vustrwsuqx45f4/branch/master?svg=true)](https://ci.appveyor.com/project/shps951023/miniexcel/branch/master) ---- - A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without any third party dependencies to create file or dynamic/type mapping query etc.. ### Features @@ -21,7 +19,7 @@ You can install the package [from NuGet](https://www.nuget.org/packages/MiniExce Please Check [Release Notes](https://github.com/shps951023/MiniExcel/tree/master/docs) -### Execute a query and map the results to a strongly typed IEnumerable +### Execute a query and map the results to a strongly typed IEnumerable [[Try it]](https://dotnetfiddle.net/3QDl6P) ```C# public class UserAccount @@ -41,7 +39,7 @@ using (var stream = File.OpenRead(path)) ![image](https://user-images.githubusercontent.com/12729184/111107423-c8c46b80-8591-11eb-982f-c97a2dafb379.png) -### Execute a query and map it to a list of dynamic objects without using head +### Execute a query and map it to a list of dynamic objects without using head [[Try it]](https://dotnetfiddle.net/3QDl6P) * dynamic key is `A.B.C.D..` @@ -61,7 +59,7 @@ using (var stream = File.OpenRead(path)) } ``` -### Execute a query with first header row +### Execute a query with first header row [[Try it]](https://dotnetfiddle.net/3QDl6P) | Column1 | Column2 | | -------- | -------- | @@ -92,9 +90,9 @@ performance: MiniExcel/ExcelDataReader/ClosedXML/EPPlus ![queryfirst](https://user-images.githubusercontent.com/12729184/111072392-6037a900-8515-11eb-9693-5ce2dad1e460.gif) -### Create Excel Xlsx file +### Create Excel Xlsx file [[Try it]](https://dotnetfiddle.net/3QDl6P) -Anonymous or strongly type: +Anonymous or strongly type: ```C# var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx"); MiniExcel.SaveAs(path, new[] { @@ -143,10 +141,10 @@ Create File Result : | MiniExcel | 1 | | Github | 2 | -### SaveAs Stream +### SaveAs Stream [[Try it]](https://dotnetfiddle.net/JOen0e) ```C# -using (var stream = new FileStream(path, FileMode.CreateNew)) +using (var stream = File.Create(path)) { stream.SaveAs(values); } @@ -201,6 +199,10 @@ Default system will auto check file path or stream is from xlsx or csv, but if y stream.SaveAs(excelType:ExcelType.CSV); //or stream.SaveAs(excelType:ExcelType.XLSX); +//or +stream.Query(excelType:ExcelType.CSV); +//or +stream.Query(excelType:ExcelType.XLSX); ``` ### TODO diff --git a/drafts/【.NET Fiddle】CSV Query&SaveAs Demo.linq b/drafts/【.NET Fiddle】CSV Query&SaveAs Demo.linq new file mode 100644 index 0000000..ad2c792 --- /dev/null +++ b/drafts/【.NET Fiddle】CSV Query&SaveAs Demo.linq @@ -0,0 +1,59 @@ + + Dapper + MiniExcel + Newtonsoft.Json + System.Data.SqlClient + MiniExcelLibs + Newtonsoft.Json + System.Data + System.Diagnostics + System.Linq.Expressions + System.Text.RegularExpressions + System.Threading + System.Transactions + System.Xml + System.Xml.Linq + System.Xml.XPath + + +void Main() +{ + Test.Program.Main(); +} + +namespace Test +{ + using System; + using System.IO; + using System.Text; + using Newtonsoft.Json; + using MiniExcelLibs; + public class Program + { + public static void Main() + { + // SaveAs & Create by MemoryStream + Byte[] bytes = null; + using (var stream = new MemoryStream()) + { + var value = new[] { new { A = "A1", B = "B1" }, new { A = "A2", B = "B2" } }; + stream.SaveAs(value: value, excelType: ExcelType.CSV); + + bytes = stream.ToArray(); + Console.WriteLine(Encoding.UTF8.GetString(bytes)); + /*result: + A,B + A1,B1 + A2,B2 + */ + } + + using (var stream = new MemoryStream(bytes)) + { + var rows = stream.Query(useHeaderRow: true); + Console.WriteLine(JsonConvert.SerializeObject(rows)); // result : [{"A":"A1","B":"B1"},{"A":"A2","B":"B2"}] + } + } + } +} +// You can define other methods, fields, classes and namespaces here diff --git a/drafts/【.NET Fiddle】Xlsx Query&SaveAs Demo.linq b/drafts/【.NET Fiddle】Xlsx Query&SaveAs Demo.linq new file mode 100644 index 0000000..f882689 --- /dev/null +++ b/drafts/【.NET Fiddle】Xlsx Query&SaveAs Demo.linq @@ -0,0 +1,90 @@ + + Dapper + MiniExcel + Newtonsoft.Json + System.Data.SqlClient + System.Data.SQLite.Core + MiniExcelLibs + Newtonsoft.Json + System.Data + System.Diagnostics + System.Linq.Expressions + System.Text.RegularExpressions + System.Threading + System.Transactions + System.Xml + System.Xml.Linq + System.Xml.XPath + + +void Main() +{ + Test.Program.Main(); +} + +// You can define other methods, fields, classes and namespaces here +namespace Test +{ + using System; + using System.IO; + using MiniExcelLibs; + using System.Linq; + using Dapper; + using System.Data.SQLite; + public class Program + { + public static void Main() + { + var path = "demo.xlsx"; + var values = new[] { new { A = "Github", B = DateTime.Parse("2021-01-01") }, new { A = "Microsoft", B = DateTime.Parse("2021-02-01") } }; + + // create + using (var stream = File.Create(path)) + stream.SaveAs(values); + + // query dynamic + using (var stream = File.OpenRead(path)) + { + var rows = stream.Query(useHeaderRow: true).ToList(); + Console.WriteLine(rows[0].A); //Github + Console.WriteLine(rows[0].B); //2021-01-01 12:00:00 AM + Console.WriteLine(rows[1].A); //Microsoft + Console.WriteLine(rows[1].B); //2021-02-01 12:00:00 AM + } + + // query type mapping + using (var stream = File.OpenRead(path)) + { + var rows = stream.Query().ToList(); + Console.WriteLine(rows[0].A); //Github + Console.WriteLine(rows[0].B); //2021-01-01 12:00:00 AM + Console.WriteLine(rows[1].A); //Microsoft + Console.WriteLine(rows[1].B); //2021-02-01 12:00:00 AM + } + + File.Delete(path); + + // Create by DapperRows + using (var connection = new System.Data.SQLite.SQLiteConnection("Data Source=:memory:")) + { + var rows = connection.Query(@"select 'MiniExcel' as Column1,1 as Column2 union all select 'Github',2"); + MiniExcel.SaveAs(path, rows); + } + + using (var stream = File.OpenRead(path)) + { + var rows = stream.Query(useHeaderRow:true).ToList(); + Console.WriteLine(rows[0].Column1); //MiniExcel + Console.WriteLine(rows[0].Column2); //1 + Console.WriteLine(rows[1].Column1); //Github + Console.WriteLine(rows[1].Column2); //2 + } + } + + public class Demo + { + public string A { get; set; } + public string B { get; set; } + } + } +} \ No newline at end of file