【发布时间】:2018-04-10 21:14:44
【问题描述】:
我有一个包含椭圆的模板文档。我可以使用以下代码访问椭圆形,在调试时我看到 FillColor 的值发生变化,但是当打开 docx 一词时,椭圆形是原始颜色。
我将此代码用作测试。颜色变化将根据应用值动态变化。尝试获取 FillColor 字符串的正确格式。
更多信息 - 添加了更改椭圆中的一些文本(使用更改正文中的标题的相同代码)并且也没有保存。
public ActionResult ....
{
.... other code newpath = copy of original word Template as you suggested with one Header to replace and Oval
WordUpdateDocxProfile(pptvm, newpath, replacethem, colwidths, 4, 0);
//download file and then delete
DownloadandDelete(newpath);
Return View();
}
private void WordUpdateDocxProfile(PerfProfileTotalsViewModel m, string newpath, List<MatrixReplaceViewModel> replacethem, List<int> colwidths, int textcols, int numcols)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(newpath, true))
{
var body = doc.MainDocumentPart.Document.Body;
var paras = body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
WordReplaceItems(paras, replacethem);
IEnumerable<DocumentFormat.OpenXml.Vml.Oval> shapes = doc.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Vml.Oval>();
foreach( var oo in shapes)
{
oo.FillColor= "#c5e0b3 [1305]";
IEnumerable<DocumentFormat.OpenXml.Wordprocessing.Paragraph> ps = oo.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
List<MatrixReplaceViewModel> replacerv = new List<MatrixReplaceViewModel>();
replacerv.Add(new MatrixReplaceViewModel { replacewith = "RV", toreplace = "RS" });
replacerv.Add(new MatrixReplaceViewModel { replacewith = "2.7", toreplace = "Val" });
replacerv.Add(new MatrixReplaceViewModel { replacewith = "Valuesss", toreplace = "Score" });
WordReplaceItems(ps, replacerv);
}
}
}
private void WordReplaceItems(IEnumerable<DocumentFormat.OpenXml.Wordprocessing.Paragraph> paras, List<MatrixReplaceViewModel> replacethem)
{
foreach (var para in paras)
{
foreach (var run in para.Elements<Run>())
{
foreach (var text in run.Elements<Text>())
{
foreach (var replace in replacethem)
{
if (text.Text.Contains(replace.toreplace))
{
text.Text = text.Text.Replace(replace.toreplace, replace.replacewith);
break; //leave if we found and replaced in this text element within the run
}
}
}
}
}
}
private void DownloadandDelete(string path)
{
//download file and then delete
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
System.IO.File.Delete(path);
}
在 WordUpdateDocxProfile 中使用 WordprocessingDocument doc = ... 结束时,向下钻取到椭圆时的值。FillColor = "#c5e0b3 [1305]"。我在去 DownloadandDelete(newpath) 之前停止了代码,并在 Open XML SDK Productivity Tool 中打开了 word 文档。标题文本已更改,但 Oval FillColor 未更改。
这是来自 Open XML SDK 生产力工具
椭圆 椭圆1 = new Oval(){ Id = "椭圆 1", Style = “位置:绝对;左:0;文本对齐:左;边距左:0;边距顶部:-.05pt;宽度:90pt;高度:80.4pt;z-index:251661312;可见性:可见;mso-环绕样式:方形;mso 宽度百分比:0;mso 高度百分比:0;mso 环绕距离左:9pt;mso 环绕距离顶部:0;mso 环绕距离右: 9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-宽度百分比:0;mso-高度百分比:0;mso-width-relative:边距;mso-height-relative:边距;v-text-anchor:top", Alternate =“标题:圆圈”,OptionalString =“_x0000_s1026”, FillColor = "#4472c4 [3204]", StrokeColor = "白色 [3212]", StrokeWeight = "1pt" };
【问题讨论】:
-
尝试创建两个只有椭圆形的文档,一个使用一种颜色,第二个使用另一种颜色。在 Open XML SDK Productivity Tool 中打开第一个,然后选择与第二个进行比较。您应该获得显示如何将一个更改为另一个的代码 - 将其与您正在做的事情进行比较,看看您是否能找到差异。如果您希望这里的任何人能够更详细地帮助您,您需要提供重现步骤和更完整的代码(请参阅minimal reproducible example)。
-
@CindyMeister 辛迪,谢谢。我为简短的解释道歉。直到您建议我才下载该工具,我很高兴我这样做了。我按照你说的做了,创建了两个简单的文档,只有一个要替换的标题和椭圆(没有表格)。使用工具的输出来获取 FillColor 的新十六进制代码。我在上面添加了更多。仍然没有得到结果。我是否需要 .Append 到 Oval.FillColor 属性而不是尝试直接设置它?我是否需要为属性单独保存 WordProcessingDocument 而不是更新的文本?
-
该工具还应该向您显示从另一个文档创建一个文档所需的代码。这对你有什么有用的吗? (请注意,我不知道答案 - 我必须自己完成这些步骤,而且目前我没有太多时间进行这种研究......)
-
@CindyMeister 是的,这个工具就是答案,我非常感谢你。我刚刚开始使用 openxml,该工具帮助我了解了 xml 中有多少嵌套。它向我展示了 2 docx 中的 Oval.FillColor 可能不同,并且颜色仍然相同。我将把答案放在下面。再次感谢。我会在搜索时寻找你的名字。
标签: c#-4.0 openxml-sdk