Skip to content

Commit 5cfa15b

Browse files
authored
Added horizontal template fill workaround to the docs (#912)
1 parent 9287266 commit 5cfa15b

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

README-V2.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,35 @@ class Dto
16851685
}
16861686
```
16871687

1688+
#### Q. How do I fill data horizontally (left-to-right) with templates?
1689+
1690+
A. MiniExcel template collections currently expand vertically (top-to-bottom). Horizontal (left-to-right) fill isn't supported yet (see https://github.com/mini-software/MiniExcel/issues/619).
1691+
1692+
If you just need the final layout, transpose your data into a matrix and export it with `printHeader: false`:
1693+
1694+
```csharp
1695+
var employees = new[]
1696+
{
1697+
new { Name = "Name1", Department = "Department1", City = "City1", Country = "Country1" },
1698+
new { Name = "Name2", Department = "Department2", City = "City2", Country = "Country2" },
1699+
new { Name = "Name3", Department = "Department3", City = "City3", Country = "Country3" },
1700+
};
1701+
1702+
var table = new DataTable();
1703+
table.Columns.Add("A");
1704+
for (var i = 0; i < employees.Length; i++)
1705+
table.Columns.Add($"B{i + 1}");
1706+
1707+
table.Rows.Add(new object[] { "Name" }.Concat(employees.Select(e => (object)e.Name)).ToArray());
1708+
table.Rows.Add(new object[] { "Department" }.Concat(employees.Select(e => (object)e.Department)).ToArray());
1709+
table.Rows.Add(new object[] { "City" }.Concat(employees.Select(e => (object)e.City)).ToArray());
1710+
table.Rows.Add(new object[] { "Country" }.Concat(employees.Select(e => (object)e.Country)).ToArray());
1711+
1712+
MiniExcel.SaveAs(path, table, printHeader: false);
1713+
```
1714+
1715+
If you need template styling, one workaround is to use scalar placeholders (e.g. `{{Name_1}}`, `{{Name_2}}` ...) and fill a dictionary (requires a fixed maximum number of columns).
1716+
16881717
#### Q. How do I query multiple sheets of an Excel file?
16891718

16901719
A. You can retrieve the sheet names with the `GetSheetNames` method and then Query them using the `sheetName` parameter:
@@ -1824,4 +1853,4 @@ for automating the creation of synchronous functions based on asynchronous ones.
18241853

18251854
### Contributors
18261855

1827-
![](https://contrib.rocks/image?repo=mini-software/MiniExcel)
1856+
![](https://contrib.rocks/image?repo=mini-software/MiniExcel)

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,6 @@ var value = new Dictionary<string, object>()
745745
MiniExcel.SaveAsByTemplate(path, templatePath, value);
746746
```
747747

748-
749-
750748
#### 3. Complex Data Fill
751749

752750
> Note: Support multi-sheets and using same varible
@@ -1821,6 +1819,35 @@ foreach (var sheetInfo in sheets)
18211819
}
18221820
```
18231821

1822+
#### Q. How to fill data horizontally (left-to-right) with templates?
1823+
1824+
A. MiniExcel template collection rendering expands vertically (top-to-bottom). Horizontal (left-to-right) fill isn't supported yet (see https://github.com/mini-software/MiniExcel/issues/619).
1825+
1826+
If you just need the final layout, transpose your data into a matrix and export it with `printHeader: false`:
1827+
1828+
```csharp
1829+
var employees = new[]
1830+
{
1831+
new { Name = "Name1", Department = "Department1", City = "City1", Country = "Country1" },
1832+
new { Name = "Name2", Department = "Department2", City = "City2", Country = "Country2" },
1833+
new { Name = "Name3", Department = "Department3", City = "City3", Country = "Country3" },
1834+
};
1835+
1836+
var table = new DataTable();
1837+
table.Columns.Add("A");
1838+
for (var i = 0; i < employees.Length; i++)
1839+
table.Columns.Add($"B{i + 1}");
1840+
1841+
table.Rows.Add(new object[] { "Name" }.Concat(employees.Select(e => (object)e.Name)).ToArray());
1842+
table.Rows.Add(new object[] { "Department" }.Concat(employees.Select(e => (object)e.Department)).ToArray());
1843+
table.Rows.Add(new object[] { "City" }.Concat(employees.Select(e => (object)e.City)).ToArray());
1844+
table.Rows.Add(new object[] { "Country" }.Concat(employees.Select(e => (object)e.Country)).ToArray());
1845+
1846+
MiniExcel.SaveAs(path, table, printHeader: false);
1847+
```
1848+
1849+
If you must use a template for styling, one option is to use scalar placeholders (e.g. `{{Name_1}}`, `{{Name_2}}` ...) and fill a dictionary (requires a fixed maximum number of columns).
1850+
18241851

18251852
#### Q. Whether to use Count will load all data into the memory?
18261853

README.zh-CN.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,6 @@ var value = new Dictionary<string, object>()
737737
MiniExcel.SaveAsByTemplate(path, templatePath, value);
738738
```
739739

740-
741-
742740
#### 3. 复杂数据填充
743741

744742
> Note: 支持多 sheet 填充,并共用同一组参数
@@ -1700,6 +1698,35 @@ foreach (var sheet in sheets)
17001698

17011699
![image](https://user-images.githubusercontent.com/12729184/116199841-2a1f5300-a76a-11eb-90a3-6710561cf6db.png)
17021700

1701+
#### Q. 模板能否横向(从左到右)填充集合数据?
1702+
1703+
A. MiniExcel 模板的集合渲染目前只支持纵向(从上到下)扩展,不支持横向(从左到右)扩展(见 https://github.com/mini-software/MiniExcel/issues/619)。
1704+
1705+
如果只需要最终布局,可以先把数据转置成一个矩阵,再用 `printHeader: false` 导出:
1706+
1707+
```csharp
1708+
var employees = new[]
1709+
{
1710+
new { Name = "Name1", Department = "Department1", City = "City1", Country = "Country1" },
1711+
new { Name = "Name2", Department = "Department2", City = "City2", Country = "Country2" },
1712+
new { Name = "Name3", Department = "Department3", City = "City3", Country = "Country3" },
1713+
};
1714+
1715+
var table = new DataTable();
1716+
table.Columns.Add("A");
1717+
for (var i = 0; i < employees.Length; i++)
1718+
table.Columns.Add($"B{i + 1}");
1719+
1720+
table.Rows.Add(new object[] { "Name" }.Concat(employees.Select(e => (object)e.Name)).ToArray());
1721+
table.Rows.Add(new object[] { "Department" }.Concat(employees.Select(e => (object)e.Department)).ToArray());
1722+
table.Rows.Add(new object[] { "City" }.Concat(employees.Select(e => (object)e.City)).ToArray());
1723+
table.Rows.Add(new object[] { "Country" }.Concat(employees.Select(e => (object)e.Country)).ToArray());
1724+
1725+
MiniExcel.SaveAs(path, table, printHeader: false);
1726+
```
1727+
1728+
如果必须使用模板以保留样式,一种方式是在模板里使用标量占位符(例如 `{{Name_1}}``{{Name_2}}` …)并用 Dictionary 填充;这种方式需要预先确定最大列数。
1729+
17031730

17041731

17051732
#### Q. 是否使用 Count 会载入全部数据到内存
@@ -1882,4 +1909,3 @@ Link https://github.com/orgs/mini-software/discussions/754
18821909
### Contributors
18831910

18841911
![](https://contrib.rocks/image?repo=mini-software/MiniExcel)
1885-

0 commit comments

Comments
 (0)