mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
doc add FQA
This commit is contained in:
parent
1be832d8bd
commit
4728be6fe5
34
README.md
34
README.md
@ -635,6 +635,40 @@ public static IEnumerable<T> Page<T>(IEnumerable<T> 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<string, object> 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
|
||||
|
@ -642,6 +642,40 @@ public static IEnumerable<T> Page<T>(IEnumerable<T> 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<string, object> 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) 或是加密文件。
|
||||
|
@ -643,6 +643,40 @@ public static IEnumerable<T> Page<T>(IEnumerable<T> 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<string, object> 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) 或是加密檔案。
|
||||
|
@ -14,12 +14,27 @@
|
||||
<RemoveNamespace>System.Transactions</RemoveNamespace>
|
||||
</Query>
|
||||
|
||||
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); //<dimension ref="A1:B100" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user