Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/MiniWord/MiniWord.Implment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MiniSoftware
namespace MiniSoftware
{
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
Expand All @@ -18,16 +18,23 @@
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Drawing.Charts;
using System.Threading.Tasks;
using System.Threading;

public static partial class MiniWord
{
private static void SaveAsByTemplateImpl(Stream stream, byte[] template, Dictionary<string, object> data)
{
SaveAsByTemplateImplAsync(stream,template,data).GetAwaiter().GetResult();
}

private static async Task SaveAsByTemplateImplAsync(Stream stream, byte[] template, Dictionary<string, object> data, CancellationToken token = default(CancellationToken))
{
var value = data; //TODO: support dynamic and poco value
byte[] bytes = null;
using (var ms = new MemoryStream())
{
ms.Write(template, 0, template.Length);
await ms.WriteAsync(template, 0, template.Length, token).ConfigureAwait(false);
ms.Position = 0;
using (var docx = WordprocessingDocument.Open(ms, true))
{
Expand All @@ -44,9 +51,10 @@ private static void SaveAsByTemplateImpl(Stream stream, byte[] template, Diction
bytes = ms.ToArray();
}

stream.Write(bytes, 0, bytes.Length);
await stream.WriteAsync(bytes, 0, bytes.Length, token).ConfigureAwait(false);
}


private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocument docx,
Dictionary<string, object> tags)
{
Expand Down Expand Up @@ -212,13 +220,14 @@ private static object GetObjVal(object objSource, string[] propNames)
var nextPropNames = propNames.Skip(1).ToArray();
if (objSource is IDictionary)
{
var dict = (IDictionary)objSource;
var dict = objSource as IDictionary;
if (dict.Contains(propNames[0]))
{
var val = dict[propNames[0]];
if (propNames.Length > 1)
return GetObjVal(dict[propNames[0]], nextPropNames);
else return val;
else
return val;
}

return null;
Expand Down Expand Up @@ -1125,12 +1134,18 @@ private static void AddPictureAnchor(OpenXmlElement appendElement, string relati

private static byte[] GetBytes(string path)
{
using (var st = Helpers.OpenSharedRead(path))
return GetByteAsync(path).GetAwaiter().GetResult();
}

private static async Task<byte[]> GetByteAsync(string path, CancellationToken token = default(CancellationToken))
{
using (var st = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true))
using (var ms = new MemoryStream())
{
st.CopyTo(ms);
//use default size 81920
await st.CopyToAsync(ms, 81920, token).ConfigureAwait(false);
return ms.ToArray();
}
}
}
}
}
31 changes: 31 additions & 0 deletions src/MiniWord/MiniWord.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace MiniSoftware
{
using DocumentFormat.OpenXml.Office2013.Excel;
using MiniSoftware.Extensions;
using MiniSoftware.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.IO;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

public static partial class MiniWord
{
Expand All @@ -26,5 +35,27 @@ public static void SaveAsByTemplate(this Stream stream, byte[] templateBytes, ob
{
SaveAsByTemplateImpl(stream, templateBytes, value.ToDictionary());
}

public static async Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value,CancellationToken token = default(CancellationToken))
{
await SaveAsByTemplateImplAsync(stream, templateBytes, value.ToDictionary(),token).ConfigureAwait(false);
}

public static async Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value,CancellationToken token = default(CancellationToken))
{
await SaveAsByTemplateImplAsync(stream, await GetByteAsync(templatePath), value.ToDictionary(),token).ConfigureAwait(false);
}

public static async Task SaveAsByTemplateAsync(string path, string templatePath, object value,CancellationToken token = default(CancellationToken))
{
using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true))
await SaveAsByTemplateImplAsync(stream, await GetByteAsync(templatePath), value.ToDictionary(),token);
}

public static async Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value,CancellationToken token = default(CancellationToken))
{
using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true))
await SaveAsByTemplateImplAsync(stream, templateBytes, value.ToDictionary(),token);
}
}
}
2 changes: 1 addition & 1 deletion src/MiniWord/MiniWord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
<None Include="icon.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="[3.1.1,4.0.0)" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
</ItemGroup>
</Project>
Loading
Loading