C# Microsoft Word文档控件不接受回车

C# Microsoft Word文档控件不接受回车,c#,xml,formatting,ms-word,openxml,C#,Xml,Formatting,Ms Word,Openxml,因此,我有一个MicrosoftWord2007文档,其中包含几个纯文本格式(我也尝试过富文本格式)控件,这些控件通过XML接受输入 对于回车,当我想要回车时,我让字符串通过包含“\r\n”的XML传递,但是word文档忽略了这一点,只是一直在同一行上包装内容。我还尝试在我的C#mapper中用System.Environment.NewLine替换\r\n,但这只是插入了\r\n,仍然不起作用 还请注意,在控件本身上,我在控件属性中将其设置为“允许回车(多个段落)” 这是listMapper的

因此,我有一个MicrosoftWord2007文档,其中包含几个纯文本格式(我也尝试过富文本格式)控件,这些控件通过XML接受输入

对于回车,当我想要回车时,我让字符串通过包含“\r\n”的XML传递,但是word文档忽略了这一点,只是一直在同一行上包装内容。我还尝试在我的C#mapper中用System.Environment.NewLine替换\r\n,但这只是插入了\r\n,仍然不起作用

还请注意,在控件本身上,我在控件属性中将其设置为“允许回车(多个段落)”

这是listMapper的XML

<Field id="32"  name="32" fieldType="SimpleText">
    <DataSelector path="/Data/DB/DebtProduct">
        <InputField fieldType=""  
                    path="/Data/DB/Client/strClientFirm" 
                    link="" type=""/>
        <InputField fieldType=""  
                    path="strClientRefDebt" 
                    link="" type=""/>
    </DataSelector>
    <DataMapper formatString="{0} Account Number: {1}" 
                name="SimpleListMapper" type="">
        <MapperData></MapperData>
    </DataMapper>
</Field>

请注意,这是ListMapperC#,在这里我实际映射了列表(请注意,我在哪里尝试并附加了system.environment.newline)

名称空间DocEngine.Core.DataMappers
{
公共类CSimpleListMapper:CBaseDataMapper
{
公共覆盖无效填充(DocEngine.Core.Interfaces.Document.IControl控件,CDATA选择器dataSelector)
{
if(control!=null&&dataSelector!=null)
{
ISimpleTextControl textControl=(ISimpleTextControl)控件;
IContent content=textControl.CreateContent();
CInputFieldCollection fileds=dataSelector.Read(上下文);
StringBuilder=新的StringBuilder();
if(fileds!=null)
{
foreach(在文件中列出lst)
{
如果(CanMap(lst)=false)继续;
如果(builder.Length>0&&lst[0]。Length>0)
builder.Append(Environment.NewLine);
if(string.IsNullOrEmpty(FormatString))
builder.Append(lst[0]);
其他的
Append(string.Format(FormatString,lst.ToArray());
}
content.Value=builder.ToString();
textControl.Content=内容;
applyRules(对照,空);
}
}
}
}
}
有人知道我该如何让微软Word 2007(docx)停止忽略我的换行符吗?

我想这是可行的

WordprocessingDocument _docx = WordprocessingDocument.Create("c:\\Test.docx", WordprocessingDocumentType.Document);
MainDocumentPart _part = _docx.MainDocumentPart; 
string _str = "abc\ndef\ngeh";  
string _strArr[] = _str.Split('\n');  

foreach (string _line in _strArr)  
{  
    Body _body = new Body();  
    _body.Append(NewText(_text));  
    _part.Append(_body);  
}  
_part.Document.Save();  
_docx.Close();  


我也遇到了同样的问题,但那是在一个桌子的牢房里

我将一个带回车符的字符串(多行)放入一个文本对象中,该文本对象被附加到一个附加到表格单元格的段落中

=>word忽略了回车符

解决办法很简单:
逐行创建一个段落,并将所有这些段落添加到表格单元格中

使用如下函数:

private static Run InsertFormatRun(Run run, string[] formatText)
{
    foreach (string text in formatText)
    {
        run.AppendChild(new Text(text));
        RunProperties runProps = run.AppendChild(new RunProperties());
        Break linebreak = new Break();
        runProps.AppendChild(linebreak);
    }
    return run;          
}

以上的答案对我都没有帮助

然而,我发现
InsertAfter
方法将原始XML字符串中的
\n
交换为
\v
,当它被传递到内容控件时,它就会正确呈现

contentControl.MultiLine = true    
contentControl.Range.InsertAfter(your string)
private static Run InsertFormatRun(Run run, string[] formatText)
{
    foreach (string text in formatText)
    {
        run.AppendChild(new Text(text));
        RunProperties runProps = run.AppendChild(new RunProperties());
        Break linebreak = new Break();
        runProps.AppendChild(linebreak);
    }
    return run;          
}
contentControl.MultiLine = true    
contentControl.Range.InsertAfter(your string)