Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 4585bcb
Author: Wei <shps951002@gmail.com>
Date:   Thu Sep 22 10:27:30 2022 +0800

    doc tranlate picture tag

commit f90ba4a
Author: WeiLin <shps951002@gmail.com>
Date:   Wed Sep 21 18:46:25 2022 +0800

    [New] support object & dynamic parameter (mini-software#19 via @isdaniel )

commit e7c8492
Merge: 0ddfe00 c5a7d2f
Author: Wei Lin <shps951002@gmail.com>
Date:   Wed Sep 21 10:07:55 2022 +0800

    Merge pull request mini-software#24 from isdaniel/feature/support-object-dynamic

    support object & dynamic parameter

commit c5a7d2f
Author: danielshih <dog830228@gmail.com>
Date:   Tue Sep 20 19:37:14 2022 +0800

    add sample code.

commit 0580c65
Author: danielshih <dog830228@gmail.com>
Date:   Tue Sep 20 18:26:47 2022 +0800

    支援 object & dynamic 參數
  • Loading branch information
ping9719 committed Sep 22, 2022
1 parent 0ddfe00 commit c4851ec
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 7 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);

### Image

标签值为 `MiniWordPicture` 类别
tag value is `MiniWordPicture` type

##### Example

Expand Down Expand Up @@ -182,6 +182,21 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);



## Other

### POCO or dynamic parameter

v0.5.0 support POCO or dynamic parameter

```csharp
var value = new { title = "Hello MiniWord" };
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
```





## Examples


Expand Down
15 changes: 15 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);



## 其他

### POCO or dynamic 参数

v0.5.0 支持 POCO 或 dynamic parameter

```csharp
var value = new { title = "Hello MiniWord" };
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
```





## 例子


Expand Down
13 changes: 13 additions & 0 deletions README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);



## 其他

### POCO or dynamic 參數

v0.5.0 支持 POCO 或 dynamic parameter

```csharp
var value = new { title = "Hello MiniWord" };
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
```



## 例子


Expand Down
9 changes: 9 additions & 0 deletions release-note/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@

---



### 0.5.0

- [New] support object & dynamic parameter (#19 via @isdaniel )



### 0.4.0

- [New] support HeaderParts, FooterParts template
- [Bug] fixed multiple table generate problem #18

Expand Down
4 changes: 4 additions & 0 deletions release-note/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@



### 0.5.0

- [New] 支持 object & dynamic parameter (#19 via @isdaniel )

### 0.4.0
- [New] 支持HeaderParts, FooterParts template
- [Bug] 修正multiple table generate problem #18
Expand Down
2 changes: 2 additions & 0 deletions release-note/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@



### 0.5.0

- [New] 支持 object & dynamic parameter (#19 via @isdaniel )

### 0.4.0

Expand Down
3 changes: 3 additions & 0 deletions src/MiniWord/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSoftware
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.IO;

internal static partial class Helpers
Expand Down
1 change: 1 addition & 0 deletions src/MiniWord/MiniExcel.Implment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
Expand Down
37 changes: 32 additions & 5 deletions src/MiniWord/MiniWord.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
namespace MiniSoftware
{
using System.Collections.Generic;
using System.IO;
using DocumentFormat.OpenXml.Office2013.Excel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.IO;
using System.Linq.Expressions;

public static partial class MiniWord
public static partial class MiniWord
{
public static void SaveAsByTemplate(string path, string templatePath, Dictionary<string, object> value)
{
using (var stream = File.Create(path))
SaveAsByTemplate(stream, templatePath, value);
}

public static void SaveAsByTemplate(string path, string templatePath, object value)
{
using (var stream = File.Create(path))
SaveAsByTemplate(stream, templatePath, value.ToDictionary());
}

public static void SaveAsByTemplate(string path, byte[] templateBytes, Dictionary<string, object> value)
{
using (var stream = File.Create(path))
SaveAsByTemplate(stream, templateBytes, value);
}

public static void SaveAsByTemplate(string path, byte[] templateBytes, object value)
{
using (var stream = File.Create(path))
SaveAsByTemplate(stream, templateBytes, value.ToDictionary());
}

public static void SaveAsByTemplate(this Stream stream, string templatePath, Dictionary<string, object> value)
{
SaveAsByTemplateImpl(stream, GetBytes(templatePath), value);
}

public static void SaveAsByTemplate(this Stream stream, byte[] templateBytes, Dictionary<string, object> value)
public static void SaveAsByTemplate(this Stream stream, string templatePath, object value)
{
SaveAsByTemplateImpl(stream, GetBytes(templatePath), value.ToDictionary());
}

public static void SaveAsByTemplate(this Stream stream, byte[] templateBytes, Dictionary<string, object> value)
{
SaveAsByTemplateImpl(stream, templateBytes, value);
}
}

public static void SaveAsByTemplate(this Stream stream, byte[] templateBytes, object value)
{

SaveAsByTemplateImpl(stream, templateBytes, value.ToDictionary());
}
}
}
2 changes: 1 addition & 1 deletion src/MiniWord/MiniWord.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
<Version>0.4.0</Version>
<Version>0.5.0</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniWord</AssemblyName>
Expand Down
31 changes: 31 additions & 0 deletions src/MiniWord/ObjectExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace MiniSoftware
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;

internal static class ObjectExtension
{
internal static Dictionary<string, object> ToDictionary(this object value)
{
if (value is ExpandoObject)
{
return new Dictionary<string, object>(value as ExpandoObject);
}

Dictionary<string, object> reuslt = new Dictionary<string, object>();

if (value != null)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value);
foreach (PropertyDescriptor prop in props)
{
object val = prop.GetValue(value);
reuslt.Add(prop.Name, val);
}
}

return reuslt;
}
}
}
30 changes: 30 additions & 0 deletions tests/AspNetCoreDemo/AspNetCoreDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public IActionResult Index()
Content = @"<html><body>
<a href='api/DownloadWordFromTemplatePath'>DownloadWordFromTemplatePath</a><br>
<a href='api/DownloadWordFromTemplateBytes'>DownloadWordFromTemplateBytes</a><br>
<a href='api/DownloadWordFromTemplatePathByObjectType'>DownloadWordFromTemplatePathByObjectType</a><br>
</body></html>"
};
}
Expand All @@ -62,6 +63,21 @@ public IActionResult Index()
}
};

static object objectValue = new
{
title = "FooCompany",
managers = new List<Dictionary<string, object>> {
new Dictionary<string, object>{{"name","Jack"},{ "department", "HR" } },
new Dictionary<string, object> {{ "name", "Loan"},{ "department", "IT" } }
},
employees = new List<Dictionary<string, object>> {
new Dictionary<string, object>{{ "name", "Wade" },{ "department", "HR" } },
new Dictionary<string, object> {{ "name", "Felix" },{ "department", "HR" } },
new Dictionary<string, object>{{ "name", "Eric" },{ "department", "IT" } },
new Dictionary<string, object> {{ "name", "Keaton" },{ "department", "IT" } }
}
};

public IActionResult DownloadWordFromTemplatePath()
{
string templatePath = "TestTemplateComplex.docx";
Expand All @@ -77,6 +93,19 @@ public IActionResult DownloadWordFromTemplatePath()
};
}

public IActionResult DownloadWordFromTemplatePathByObjectType()
{
string templatePath = "TestTemplateComplex.docx";

MemoryStream memoryStream = new MemoryStream();
MiniWord.SaveAsByTemplate(memoryStream, templatePath, objectValue);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
FileDownloadName = "demo.docx"
};
}

private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>();

static ApiController()
Expand All @@ -100,4 +129,5 @@ public IActionResult DownloadWordFromTemplateBytes()
FileDownloadName = "demo.docx"
};
}

}
Loading

0 comments on commit c4851ec

Please sign in to comment.