C# 4.0 使用openxml更改word docx中椭圆形的颜色
我有一个包含椭圆的模板文档。我可以用下面的代码访问椭圆形,在调试过程中,我看到FillColor的值发生了变化,但是当打开docx这个词时,椭圆形是原始颜色 我将此代码用作测试。颜色更改将根据应用程序值进行动态更改。正在尝试获取FillColor字符串的正确格式 更多信息-添加了在椭圆形中更改某些文本(使用与更改正文标题相同的代码),但也没有保存C# 4.0 使用openxml更改word docx中椭圆形的颜色,c#-4.0,openxml-sdk,C# 4.0,Openxml Sdk,我有一个包含椭圆的模板文档。我可以用下面的代码访问椭圆形,在调试过程中,我看到FillColor的值发生了变化,但是当打开docx这个词时,椭圆形是原始颜色 我将此代码用作测试。颜色更改将根据应用程序值进行动态更改。正在尝试获取FillColor字符串的正确格式 更多信息-添加了在椭圆形中更改某些文本(使用与更改正文标题相同的代码),但也没有保存 public ActionResult .... { .... other code newpath = copy of original word
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);
}
公共行动结果。。。。
{
..其他代码newpath=您建议的原始单词模板的副本,其中有一个要替换的标题和椭圆
WordUpdatedOxProfile(pptvm、newpath、replacethem、colwidths、4、0);
//下载文件,然后删除
下载和删除(新建路径);
返回视图();
}
私有void WordUpdateDocxProfile(PerfProfileTotalsViewModel m、字符串newpath、List replacethem、List colwidths、int textcols、int numcols)
{
使用(WordprocessingDocument doc=WordprocessingDocument.Open(newpath,true))
{
var body=doc.main documentpart.Document.body;
var-paras=body.Elements();
词语替换项(第段,替换项);
IEnumerable shapes=doc.MainDocumentPart.Document.Body.subjects();
foreach(形状中的变量oo)
{
oo.FillColor=“#c5e0b3[1305]”;
IEnumerable ps=oo.subjects();
List replacerv=新列表();
添加(新的MatrixReplaceViewModel{replacewith=“RV”,toreReplace=“RS”});
Add(新矩阵replaceViewModel{replacewith=“2.7”,toreReplace=“Val”});
Add(新矩阵replaceViewModel{replacewith=“Valuesss”,toreReplace=“Score”});
替换项(ps、替换RV);
}
}
}
private void WordReplaceItems(IEnumerable段落,列出ReplaceItems)
{
foreach(第4-4段中的第4段)
{
foreach(变量在para.Elements()中运行)
{
foreach(run.Elements()中的变量文本)
{
foreach(replacethem中的变量replace)
{
if(text.text.Contains(replace.toreplace))
{
text.text=text.text.Replace(Replace.toreplace,Replace.replacewith);
break;//如果在运行中找到并替换了此文本元素,请离开
}
}
}
}
}
}
私有void downloaddelete(字符串路径)
{
//下载文件,然后删除
System.IO.FileInfo file=新的System.IO.FileInfo(路径);
如果(file.Exists)
{
Response.Clear();
Response.AddHeader(“内容处置”、“附件;文件名=“+file.Name”);
AddHeader(“Content-Length”,file.Length.ToString());
Response.ContentType=“应用程序/八位字节流”;
Response.WriteFile(file.FullName);
Response.End();
}
System.IO.File.Delete(路径);
}
在使用WordprocessingDocument doc=。。。在WordUpdatedOxProfile中,向下钻取到oval.FillColor=“#c5e0b3[1305]”时的值。在开始下载和删除(newpath)之前,我已经停止了代码,并在OpenXMLSDK生产力工具中打开了word文档。标题文本已更改,但椭圆形填充颜色未更改
这是来自OpenXMLSDK生产力工具
Oval oval1=新的Oval(){Id=“Oval 1”,样式=
“位置:绝对;左侧:0;文本对齐:左侧;页边距左侧:0;页边距顶部:-.05pt;宽度:90pt;高度:80.4pt;z索引:25161312;可见性:可见;mso环绕样式:方形;mso宽度百分比:0;mso高度百分比:0;mso环绕距离左侧:9pt;mso环绕距离顶部:0;mso环绕距离右侧:9pt;mso环绕距离底部:0;mso位置水平:绝对;mso位置”n-水平-相对:文本;mso位置垂直:绝对;mso位置垂直相对:文本;mso宽度百分比:0;mso高度百分比:0;mso宽度相对:边距;mso高度相对:边距;v-文本-锚定:顶部“,
Alternate=“Title:circle”,OptionalString=“\u x0000\u s1026”,
FillColor=“#4472c4[3204]”,StrokeColor=“白色[3212]”,
StrokeWeight=“1pt”}
注意:我使用的是存储在服务器上的docx模板(在下面的using语句中复制为newpath)。为了访问要更改的形状,在本例中是我的椭圆形,我必须设置形状的标题。通过右键单击docx中的形状来执行此操作。。。 格式化形状/布局和属性/Alt文本/标题并设置标题值(我的是圆形)
DocumentFormat.OpenXml.Vml.Oval
FillColor
属性不是设置我的椭圆颜色的属性。OpenXML生产力工具帮助我找到了颜色的实际设置位置。在我实际的docx中,我需要的ShapeProperties
是xml层次结构的17层。通过采纳Cindy的建议,创建一个简单的docx,其中只包含椭圆和另一个段落,我能够确定需要设置的值
ShapeProperties
嵌套在一个Anchor
中,Anchor
的DocProperties
有一个Title
,在我的例子中,我在寻找“圆圈”,即我在docx中设置的标题
using (WordprocessingDocument doc = WordprocessingDocument.Open(newpath, true))
{
var body = doc.MainDocumentPart.Document.Body;
var anchor = body.Descendants<DocumentFormat.OpenXml.Drawing.Wordprocessing.Anchor>();
foreach (var anc in anchor)
{
var docProperties = anc.Descendants<DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties>().Where(tp => tp.Title != null);
foreach (DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties docProp in docProperties)
{
if (docProp.Title.Value == "circle")
{
var shapes = anc.Descendants<DocumentFormat.OpenXml.Office2010.Word.DrawingShape.WordprocessingShape>();
foreach (var sh in shapes)
{
foreach (var sp in sh.Elements<DocumentFormat.OpenXml.Office2010.Word.DrawingShape.ShapeProperties>())
{
foreach (var fill in sp.Elements<DocumentFormat.OpenXml.Drawing.SolidFill>())
{
// deleting SchemeColor if already set up in the solidfill
if (fill.SchemeColor != null) { fill.SchemeColor.Remove(); }
// m.QLA.CircleColorHex is simply a string representing the color in 6 char Hex - this does not use the # before, so a value is like "FFFFFF"
DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgb = new DocumentFormat.OpenXml.Drawing.RgbColorModelHex() { Val = m.QLA.CircleColorHex };
fill.Append(rgb);
}
}
}
}
}
}
}
使用(WordprocessingDocument=WordprocessingDocument.Open(newpath,true))
{
var body=doc.main documentpart.Document.body;
var-anchor=body.subjections();
foreach(锚定中的var anc)
{
var docProperties=anc.Des