Comments (8)
@sergey-tihon Works perfectly! Thank you!
from clippit.
@IvanDrag0 Can you please be move specific about the functionality you are talking about or share full code snippet?
from clippit.
@sergey-tihon, here's a small Powershell script that just loads the assemblies and applies the data found in an XML file to the document (based on the Open-XML-PowerTools example):
Add-Type -Path "$PSScriptRoot\DocumentFormat.OpenXml.dll"
Add-Type -Path "$PSScriptRoot\Clippit.dll"
[xml]$Data = Get-Content -Path "$PSScriptRoot\Data.xml"
[Clippit.WmlDocument]$WmlDocument = [Clippit.WmlDocument]::FromDocument("$PSScriptRoot\TemplateDocument.docx")
[ref]$templateError = $null
[Clippit.WmlDocument]$wmlAssembledDoc = [Clippit.Word.DocumentAssembler]::AssembleDocument($WmlDocument, $Data, $templateError)
$wmlAssembledDoc.SaveAs("$PSScriptRoot\AssembledDocument.docx")
I'm using the following XML data:
<?xml version="1.0" encoding="utf-8"?>
<Customer>
<Orders>
<Order Number="1">
<Names>
<Name>John Smith</Name>
<Name>Johnny Smith</Name>
<Name>Jon Smith</Name>
</Names>
<ProductDescription>Unicycle</ProductDescription>
<Quantity>3</Quantity>
</Order>
<Order Number="2">
<Names>
<Name>Jane Smith</Name>
<Name>Jenny Smith</Name>
<Name>Jannifer Smith</Name>
</Names>
<ProductDescription>Tricycle</ProductDescription>
<Quantity>8</Quantity>
</Order>
</Orders>
</Customer>
The attached Word document shows what I'm trying to do and the results.
It looks like adding any of the command tags/functions inside the <Table>
function will result in an invalid Word document (which Word can't open) because the library adds the literal tags into the document.xml file inside the Word DOCX package:
from clippit.
I do not familiar with this part of the library, but
Here is the code that generate markup for table cell -
Clippit/OpenXmlPowerTools/Word/DocumentAssembler.cs
Lines 1380 to 1386 in e0da582
It looks like it should be possible to concatenate multiple xpath results into one string and then generate one paragraph from this string -
Clippit/OpenXmlPowerTools/Word/DocumentAssembler.cs
Lines 1491 to 1511 in e0da582
from clippit.
Thank you! I'll give it a try!
from clippit.
My C# is a bit rusty, but I've modified the EvaluateXPathToString function to the following:
private static string EvaluateXPathToString(XElement element, string xPath, bool optional )
{
object xPathSelectResult;
try
{
//support some cells in the table may not have an xpath expression.
if (string.IsNullOrWhiteSpace(xPath)) return string.Empty;
string[] xPathSplit = xPath.Split('/');
if (element.Descendants(xPathSplit.Last()).Count() > 1)
{
var result = element.Descendants(xPathSplit[xPathSplit.Length - 2]).Select(hl => new
{
Str = String.Join(", ", hl.Elements(xPathSplit.Last()).Select(x => x.Value))
});
var e = new XElement(xPathSplit.Last(), result.First().Str);
element.Descendants(xPathSplit.Last()).Remove();
element.Element(xPathSplit[xPathSplit.Length - 2]).Add(e);
}
xPathSelectResult = element.XPathEvaluate(xPath);
}
catch (XPathException e)
{
throw new XPathException("XPathException: " + e.Message, e);
}
if (xPathSelectResult is IEnumerable enumerable and not string)
{
var selectedData = enumerable.Cast<XObject>();
if (!selectedData.Any())
{
if (optional) return string.Empty;
throw new XPathException($"XPath expression ({xPath}) returned no results");
}
switch (selectedData.First())
{
case XElement xElement:
return xElement.Value;
case XAttribute attribute:
return attribute.Value;
}
}
return xPathSelectResult.ToString();
}
This changes the XML in memory to the following, before applying it to the document:
<?xml version="1.0" encoding="utf-8"?>
<Customer>
<Orders>
<Order Number="1">
<Names>
<Name>John Smith, Johnny Smith, Jon Smith</Name>
</Names>
<ProductDescription>Unicycle</ProductDescription>
<Quantity>3</Quantity>
</Order>
<Order Number="2">
<Names>
<Name>Jane Smith, Jenny Smith, Jannifer Smith</Name>
</Names>
<ProductDescription>Tricycle</ProductDescription>
<Quantity>8</Quantity>
</Order>
</Orders>
</Customer>
The two things that should be added are:
- Add another "separator" input parameter to the function to allow the user to use custom joining separators.
- Add logic to allow the user to add a new line separator (such as "<w:br/>").
from clippit.
Just release 1.8.0-beta1 that support multi-value xpath inside table cell as described in #39.
@IvanDrag0 can you please take a look/try and confirm that it cover your use case?
from clippit.
Released in 1.8.0
from clippit.
Related Issues (20)
- SpreadsheetWriter format data as Table HOT 1
- Generate Excel reports based on some/user's template
- rowspan causing error Object reference not set to an instance of an object. at Clippit.HtmlToWml.HtmlToWmlConverterCore.AddPseudoCells(XElement html) HOT 4
- PresentationBuilder.PublishSlides generates slides with different data HOT 3
- Slides generated using PresentationBuilder.PublishSlides have original docProps
- Feature: remove all media elements from the file
- Smarter fix for broken URLs inside packages
- Optimize PresentationBuilder memory consumption
- First tab in paragraph is ignorred HOT 2
- ManageMediaCopy: improvement ideas
- StreamHelpers.ComputeHash issue
- PublishSlides: missing ppt/metadata for google presentations
- Add text in word Watermark in DocumentAssembler HOT 7
- Excel gives error HOT 3
- Adding textBox and table wrapped around text when converting to HTML
- Feature: Support for custom content controls in DocumentAssembler HOT 1
- DocumentAssembler: Is it possible to to have conditional Table Row? (unrelated) Hidden methods? HOT 1
- SixLabors.ImageSharp License check HOT 1
- DocumentAssembler: Text formatting? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clippit.