Add try it online demo

This commit is contained in:
wei 2021-03-25 15:11:16 +08:00
parent ece657352e
commit 3540d55e04
3 changed files with 160 additions and 9 deletions

View File

@ -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

View File

@ -0,0 +1,59 @@
<Query Kind="Program">
<NuGetReference>Dapper</NuGetReference>
<NuGetReference>MiniExcel</NuGetReference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>System.Data.SqlClient</NuGetReference>
<Namespace>MiniExcelLibs</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<RemoveNamespace>System.Data</RemoveNamespace>
<RemoveNamespace>System.Diagnostics</RemoveNamespace>
<RemoveNamespace>System.Linq.Expressions</RemoveNamespace>
<RemoveNamespace>System.Text.RegularExpressions</RemoveNamespace>
<RemoveNamespace>System.Threading</RemoveNamespace>
<RemoveNamespace>System.Transactions</RemoveNamespace>
<RemoveNamespace>System.Xml</RemoveNamespace>
<RemoveNamespace>System.Xml.Linq</RemoveNamespace>
<RemoveNamespace>System.Xml.XPath</RemoveNamespace>
</Query>
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

View File

@ -0,0 +1,90 @@
<Query Kind="Program">
<NuGetReference>Dapper</NuGetReference>
<NuGetReference>MiniExcel</NuGetReference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>System.Data.SqlClient</NuGetReference>
<NuGetReference>System.Data.SQLite.Core</NuGetReference>
<Namespace>MiniExcelLibs</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<RemoveNamespace>System.Data</RemoveNamespace>
<RemoveNamespace>System.Diagnostics</RemoveNamespace>
<RemoveNamespace>System.Linq.Expressions</RemoveNamespace>
<RemoveNamespace>System.Text.RegularExpressions</RemoveNamespace>
<RemoveNamespace>System.Threading</RemoveNamespace>
<RemoveNamespace>System.Transactions</RemoveNamespace>
<RemoveNamespace>System.Xml</RemoveNamespace>
<RemoveNamespace>System.Xml.Linq</RemoveNamespace>
<RemoveNamespace>System.Xml.XPath</RemoveNamespace>
</Query>
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<Demo>().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; }
}
}
}