From 4728be6fe58a57cfc66056f1e79116feda848676 Mon Sep 17 00:00:00 2001 From: wei Date: Thu, 15 Apr 2021 12:12:28 +0800 Subject: [PATCH] doc add FQA --- README.md | 34 +++++++++++++++++++ README.zh-CN.md | 34 +++++++++++++++++++ README.zh-Hant.md | 34 +++++++++++++++++++ .../【Try】XDocument Remove Namespace.linq | 19 +++++++++-- 4 files changed, 119 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c9c216..f94c12f 100644 --- a/README.md +++ b/README.md @@ -635,6 +635,40 @@ public static IEnumerable Page(IEnumerable en, int pageSize, int page) +### FQA + +#### Q: How to Query as DataTable + +Note : There will be no advantage *whatsoever* in using MiniExcel for a scenario involving `DataSet` or `DataTable`. + +```C# +public static DataTable QueryAsDataTable(string path) +{ + var rows = MiniExcel.Query(path, true); + var dt = new DataTable(); + var first = true; + foreach (IDictionary row in rows) + { + if (first) + { + foreach (var key in row.Keys) + { + var type = row[key]?.GetType() ?? typeof(string); + dt.Columns.Add(key, type); + } + + first = false; + } + dt.Rows.Add(row.Values.ToArray()); + } + return dt; +} +``` + +![image-20210415120352604](https://i.loli.net/2021/04/15/TbtP1GLDEgONueK.png) + + + ### Limitations and caveats - Not support xls and encrypted file now diff --git a/README.zh-CN.md b/README.zh-CN.md index d3739ef..3bd31fb 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -642,6 +642,40 @@ public static IEnumerable Page(IEnumerable en, int pageSize, int page) +### FQA + +#### Q: 如何将查询结果转为 DataTable + +提醒 : 不建议使用,因为DataTable会将数据`全载入内存`,失去MiniExcel低内存消耗功能。 + +```C# +public static DataTable QueryAsDataTable(string path) +{ + var rows = MiniExcel.Query(path, true); + var dt = new DataTable(); + var first = true; + foreach (IDictionary row in rows) + { + if (first) + { + foreach (var key in row.Keys) + { + var type = row[key]?.GetType() ?? typeof(string); + dt.Columns.Add(key, type); + } + + first = false; + } + dt.Rows.Add(row.Values.ToArray()); + } + return dt; +} +``` + +![image-20210415120352604](https://i.loli.net/2021/04/15/TbtP1GLDEgONueK.png) + + + ### 局限与警告 - 目前不支援 xls (97-2003) 或是加密文件。 diff --git a/README.zh-Hant.md b/README.zh-Hant.md index 472a970..4a091ad 100644 --- a/README.zh-Hant.md +++ b/README.zh-Hant.md @@ -643,6 +643,40 @@ public static IEnumerable Page(IEnumerable en, int pageSize, int page) +### FQA + +#### Q: 如何將查詢結果轉為 DataTable + +提醒 : 不建議使用,因為DataTable會將數據`全載入記憶體`,失去MiniExcel低記憶體消耗功能。 + +```C# +public static DataTable QueryAsDataTable(string path) +{ + var rows = MiniExcel.Query(path, true); + var dt = new DataTable(); + var first = true; + foreach (IDictionary row in rows) + { + if (first) + { + foreach (var key in row.Keys) + { + var type = row[key]?.GetType() ?? typeof(string); + dt.Columns.Add(key, type); + } + + first = false; + } + dt.Rows.Add(row.Values.ToArray()); + } + return dt; +} +``` + +![image-20210415120352604](https://i.loli.net/2021/04/15/TbtP1GLDEgONueK.png) + + + ### 侷限與警告 - 目前不支援 xls (97-2003) 或是加密檔案。 diff --git a/drafts/【Try】XDocument Remove Namespace.linq b/drafts/【Try】XDocument Remove Namespace.linq index b951960..85efd46 100644 --- a/drafts/【Try】XDocument Remove Namespace.linq +++ b/drafts/【Try】XDocument Remove Namespace.linq @@ -14,12 +14,27 @@ System.Transactions -void Main() -{ +void Main(){ var doc = XDocument.Parse(xml); + XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"); + var dimension = doc.XPathSelectElement("/worksheet/dimension", ns); +} + +void Main2() +{ + var doc = XDocument.Parse(xml); + + + XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); + ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"); + + + + + var dimension = doc.XPathSelectElement("/x:worksheet/x:dimension", ns); Console.WriteLine(dimension); //