【发布时间】:2018-03-03 09:55:15
【问题描述】:
我很难让内容控件遵循多行格式。它似乎从字面上解释了我给它的一切。我是 OpenXML 的新手,我觉得我一定缺少一些简单的东西。
我正在使用这个函数转换我的多行字符串。
private static void parseTextForOpenXML(Run run, string text)
{
string[] newLineArray = { Environment.NewLine, "<br/>", "<br />", "\r\n" };
string[] textArray = text.Split(newLineArray, StringSplitOptions.None);
bool first = true;
foreach (string line in textArray)
{
if (!first)
{
run.Append(new Break());
}
first = false;
Text txt = new Text { Text = line };
run.Append(txt);
}
}
我用这个把它插入到控件中
public static WordprocessingDocument InsertText(this WordprocessingDocument doc, string contentControlTag, string text)
{
SdtElement element = doc.MainDocumentPart.Document.Body.Descendants<SdtElement>().FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag);
if (element == null)
throw new ArgumentException("ContentControlTag " + contentControlTag + " doesn't exist.");
element.Descendants<Text>().First().Text = text;
element.Descendants<Text>().Skip(1).ToList().ForEach(t => t.Remove());
return doc;
}
我用类似...的东西来称呼它
doc.InsertText("Primary", primaryRun.InnerText);
虽然我也尝试过 InnerXML 和 OuterXML。结果看起来像
Example AttnExample CompanyExample AddressNew York, NY 12345 或
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:t>Example Attn</w:t><w:br /><w:t>Example Company</w:t><w:br /><w:t>Example Address</w:t><w:br /><w:t>New York, NY 12345</w:t></w:r>
该方法适用于简单的文本插入。只是当我需要它来解释对我不起作用的 XML 时。
我觉得我必须非常接近得到我需要的东西,但我的摆弄让我无处可去。有什么想法吗?谢谢。
【问题讨论】:
标签: openxml multiline contentcontrol