Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add table HeaderRowCount attribute #26

Open
wants to merge 22 commits into
base: vNext
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
233edb1
add table HeaderRowCount attribute
chen1tian Jul 16, 2020
6ea34d8
发布包到本地nuget
chen1tian Jul 17, 2020
a8b6601
增加组合图表的测试代码
chen1tian Oct 30, 2020
cd07a05
优化MoveSecondChartSeries后修改旧series的代码
chen1tian Oct 30, 2020
691d2f2
增加打包脚本
chen1tian Oct 30, 2020
c993302
修正第二坐标轴为折线图时,不展示线条的问题
chen1tian Nov 17, 2020
f5b3ede
修复当次级坐标轴为折线图时,线条不显示的问题
chen1tian Nov 17, 2020
f219e4c
remove build script for local nuget server
chen1tian Aug 16, 2021
220bf32
Merge branch 'EricWhiteDev:vNext' into vNext
chen1tian Aug 16, 2021
1594363
修复次轴图表展示不争取的问题。
chen1tian Apr 19, 2023
efa7bfb
更新版本
chen1tian Apr 19, 2023
3216baa
nuget包版本增加
chen1tian Apr 19, 2023
6c4d029
Merge branch 'vNext' of https://github.com/chen1tian/Open-Xml-PowerTo…
chen1tian Apr 19, 2023
31b7524
如果没有找到次轴序列则抛出异常
chen1tian Apr 19, 2023
5b4cf01
增加图片替换功能
chen1tian Apr 24, 2023
9bbb759
重构图片更新器,增加byte[]参数的输入
chen1tian Apr 24, 2023
a7822a4
增加图片替换
chen1tian Apr 24, 2023
1dfc59f
修正替换图片时候,将其他图片全部替换的问题
chen1tian Apr 25, 2023
e7926ce
图片替换后,去掉包围图片的自定义控件
chen1tian May 17, 2023
7fab0b1
修改图片时候,更新宽度和高度
chen1tian May 17, 2023
30b04c6
图片替换时,可以设置宽高
chen1tian May 18, 2023
c42056c
更新版本号
chen1tian May 18, 2023
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
50 changes: 49 additions & 1 deletion OpenXmlPowerTools/ChartUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public class ChartData
public string[] CategoryNames;

public double[][] Values;

/// <summary>
/// 组合图表中,第二个图表的序列索引.
/// 这些序列数据会被复制到第二个图表的序列中.
/// </summary>
public int[] SecondChartSeriesIndex;

public XName SecondChartType;
}

public class ChartUpdater
Expand Down Expand Up @@ -95,6 +103,46 @@ public static void UpdateChart(ChartPart chartPart, ChartData chartData)
}

UpdateSeries(chartPart, chartData);
MoveSecondChartSeries(chartPart, chartData);
}

/// <summary>
/// 移动第二图表的序列数据.
/// </summary>
/// <param name="chartPart">图表部件.</param>
/// <param name="chartData">图表数据.</param>
private static void MoveSecondChartSeries(ChartPart chartPart, ChartData chartData)
{
if (chartData.SecondChartType == null)
{
return;
}

if (chartData.SecondChartSeriesIndex != null && chartData.SecondChartSeriesIndex.Length == 0)
{
return;
}

XDocument cpXDoc = chartPart.GetXDocument();
XElement root = cpXDoc.Root;

// 根据配置得到序列
var series = chartData.SecondChartSeriesIndex.Select(x =>
root.Descendants(C.ser).ElementAt(x)
).ToList();

var firstParent = series.First().Parent;

var chart = root.Descendants(chartData.SecondChartType).FirstOrDefault();

var firstSeries = chart.Descendants(C.ser).FirstOrDefault();
firstSeries.Parent.Elements(C.ser).Skip(1).Remove();

firstSeries.ReplaceWith(series);

// 删除旧series
series.Remove();
chartPart.PutXDocument();
}

private static Dictionary<int, string> FormatCodes = new Dictionary<int, string>()
Expand Down Expand Up @@ -427,7 +475,7 @@ private static void UpdateEmbeddedWorkbook(ChartPart chartPart, ChartData chartD
var firstRow = new XElement(S.row,
new XAttribute("r", "1"),
new XAttribute("spans", string.Format("1:{0}", chartData.SeriesNames.Length + 1)),
new [] { new XElement(S.c,
new[] { new XElement(S.c,
new XAttribute("r", "A1"),
new XAttribute("t", "str"),
new XElement(S.v,
Expand Down
49 changes: 31 additions & 18 deletions OpenXmlPowerTools/DocumentAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ private static string ValidatePerSchema(XElement element)
<xs:element name='Table'>
<xs:complexType>
<xs:attribute name='Select' type='xs:string' use='required' />
<xs:attribute name='HeaderRowCount' type='xs:int' use='optional' />
</xs:complexType>
</xs:element>
</xs:schema>",
Expand Down Expand Up @@ -575,6 +576,8 @@ private class PA
public static XName Match = "Match";
public static XName NotMatch = "NotMatch";
public static XName Depth = "Depth";

public static XName HeaderRowCount = "HeaderRowCount";
}

private class PASchemaSet
Expand All @@ -600,8 +603,8 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
XElement para = element.Descendants(W.p).FirstOrDefault();
XElement run = element.Descendants(W.r).FirstOrDefault();

var xPath = (string) element.Attribute(PA.Select);
var optionalString = (string) element.Attribute(PA.Optional);
var xPath = (string)element.Attribute(PA.Select);
var optionalString = (string)element.Attribute(PA.Optional);
bool optional = (optionalString != null && optionalString.ToLower() == "true");

string newValue;
Expand All @@ -618,7 +621,7 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
{

XElement p = new XElement(W.p, para.Elements(W.pPr));
foreach(string line in newValue.Split('\n'))
foreach (string line in newValue.Split('\n'))
{
p.Add(new XElement(W.r,
para.Elements(W.r).Elements(W.rPr).FirstOrDefault(),
Expand All @@ -630,7 +633,7 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
else
{
List<XElement> list = new List<XElement>();
foreach(string line in newValue.Split('\n'))
foreach (string line in newValue.Split('\n'))
{
list.Add(new XElement(W.r,
run.Elements().Where(e => e.Name != W.t),
Expand Down Expand Up @@ -692,11 +695,21 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
}
if (tableData.Count() == 0)
return CreateContextErrorMessage(element, "Table Select returned no data", templateError);



int? headerRowCountAttr = (int?)element.Attribute(PA.HeaderRowCount) ?? 1;
if (headerRowCountAttr.Value < 1)
{
headerRowCountAttr = 1;
}
var headerRowCount = headerRowCountAttr.Value;

XElement table = element.Element(W.tbl);
XElement protoRow = table.Elements(W.tr).Skip(1).FirstOrDefault();
XElement protoRow = table.Elements(W.tr).Skip(headerRowCount).FirstOrDefault();
var footerRowsBeforeTransform = table
.Elements(W.tr)
.Skip(2)
.Skip(headerRowCount + 1)
.ToList();
var footerRows = footerRowsBeforeTransform
.Select(x => ContentReplacementTransform(x, data, templateError))
Expand All @@ -707,7 +720,7 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
protoRow.Descendants(W.bookmarkEnd).Remove();
XElement newTable = new XElement(W.tbl,
table.Elements().Where(e => e.Name != W.tr),
table.Elements(W.tr).FirstOrDefault(),
table.Elements(W.tr).Take(headerRowCount),
tableData.Select(d =>
new XElement(W.tr,
protoRow.Elements().Where(r => r.Name != W.tc),
Expand Down Expand Up @@ -756,17 +769,17 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
if (match != null && notMatch != null)
return CreateContextErrorMessage(element, "Conditional: Cannot specify both Match and NotMatch", templateError);

string testValue = null;
string testValue = null;

try
{
testValue = EvaluateXPathToString(data, xPath, false);
}
catch (XPathException e)
catch (XPathException e)
{
return CreateContextErrorMessage(element, e.Message, templateError);
}

if ((match != null && testValue == match) || (notMatch != null && testValue != notMatch))
{
var content = element.Elements().Select(e => ContentReplacementTransform(e, data, templateError));
Expand Down Expand Up @@ -815,14 +828,14 @@ private static XElement CreateParaErrorMessage(string errorMessage, TemplateErro
return errorPara;
}

private static string EvaluateXPathToString(XElement element, string xPath, bool optional )
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;

xPathSelectResult = element.XPathEvaluate(xPath);
}
catch (XPathException e)
Expand All @@ -832,7 +845,7 @@ private static string EvaluateXPathToString(XElement element, string xPath, bool

if ((xPathSelectResult is IEnumerable) && !(xPathSelectResult is string))
{
var selectedData = ((IEnumerable) xPathSelectResult).Cast<XObject>();
var selectedData = ((IEnumerable)xPathSelectResult).Cast<XObject>();
if (!selectedData.Any())
{
if (optional) return string.Empty;
Expand All @@ -843,11 +856,11 @@ private static string EvaluateXPathToString(XElement element, string xPath, bool
throw new XPathException(string.Format("XPath expression ({0}) returned more than one node", xPath));
}

XObject selectedDatum = selectedData.First();
if (selectedDatum is XElement) return ((XElement) selectedDatum).Value;
XObject selectedDatum = selectedData.First();

if (selectedDatum is XElement) return ((XElement)selectedDatum).Value;

if (selectedDatum is XAttribute) return ((XAttribute) selectedDatum).Value;
if (selectedDatum is XAttribute) return ((XAttribute)selectedDatum).Value;
}

return xPathSelectResult.ToString();
Expand Down
2 changes: 2 additions & 0 deletions OpenXmlPowerTools/NugetPush.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nuget push C:\Work\github\Open-Xml-PowerTools\OpenXmlPowerTools\bin\Release\*.nupkg -ApiKey 123.123a -Source http://nuget.cefcfco.com
pause
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess this shouldn't be here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you`re right, this is build script for my local nuget server. I will remove it soon.

9 changes: 9 additions & 0 deletions OpenXmlPowerTools/OpenXmlPowerTools.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;net46;netstandard2.0</TargetFrameworks>
<Version>4.5.5</Version>
<Authors>yitian.chen</Authors>
<Company>yitian.chen</Company>
<PackageId>OpenXmlPowerTools-HrRd</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,4 +26,8 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Drawing.Common" Version="4.5.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="IF NOT &quot;$(ConfigurationName)&quot;==&quot;Release&quot; EXIT /B 0&#xD;&#xA;echo nuget push $(ProjectDir)bin\Release\*.nupkg -ApiKey 123.123a -Source http://nuget.cefcfco.com &gt; NugetPush.bat&#xD;&#xA;echo pause &gt;&gt; NugetPush.bat" />
</Target>
</Project>
40 changes: 40 additions & 0 deletions OpenXmlPowerToolsExamples/ChartUpdater01/ChartUpdater01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@ namespace OpenXmlPowerTools
class Program
{
static void Main(string[] args)
{
// Example1();
TowTypeChartExample();
}

private static void TowTypeChartExample()
{
var fi = new FileInfo("TowTypeChart.docx");

using (var wDoc = WordprocessingDocument.Open(fi.FullName, true))
{
var chart1Data = new ChartData
{
SecondChartType = C.lineChart,
SecondChartSeriesIndex = new[] { 1 },
SeriesNames = new[] {
"平仓手数",
"平仓盈亏-逐笔对冲",
},
CategoryDataType = ChartDataType.String,
CategoryNames = new[] {
"1:0天",
"1:1天",
"1:2天",
"1:3天",
},
Values = new double[][] {
new double[] {
100, 310, 220, 450,
},
new double[] {
-11323.65, 3101, -12220, 15421,
},
},
};
ChartUpdater.UpdateChart(wDoc, "towTypeChart", chart1Data);
}
}

private static void Example1()
{
var n = DateTime.Now;
var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
<ItemGroup>
<Reference Include="WindowsBase" />
</ItemGroup>

<ItemGroup>
<None Update="TowTypeChart.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file not shown.
17 changes: 17 additions & 0 deletions OpenXmlPowerToolsExamples/DocumentAssembler02/Data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@
<OrderDate>September 26, 2001</OrderDate>
</Order>
</Orders>
<Items>
<Item>
<Name>Items1</Name>
<MinCount>1</MinCount>
<MaxCount>10</MaxCount>
</Item>
<Item>
<Name>Items2</Name>
<MinCount>2</MinCount>
<MaxCount>15</MaxCount>
</Item>
<Item>
<Name>Items3</Name>
<MinCount>3</MinCount>
<MaxCount>30</MaxCount>
</Item>
</Items>
</Customer>
Binary file not shown.