代码之家  ›  专栏  ›  技术社区  ›  Scott

Microsoft Word文档控件不接受回车

  •  1
  • Scott  · 技术社区  · 17 年前

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

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

    这是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)

    namespace DocEngine.Core.DataMappers
    {
        public class CSimpleListMapper:CBaseDataMapper
        {
            public override void Fill(DocEngine.Core.Interfaces.Document.IControl control, CDataSelector dataSelector)
            {
                if (control != null && dataSelector != null)
                {
                    ISimpleTextControl textControl = (ISimpleTextControl)control;
                    IContent content = textControl.CreateContent();
                    CInputFieldCollection fileds = dataSelector.Read(Context);
                    StringBuilder builder = new StringBuilder();
    
                    if (fileds != null)
                    {
                        foreach (List<string> lst in fileds)
                        {
                            if (CanMap(lst) == false) continue;
                            if (builder.Length > 0 && lst[0].Length > 0)
                                builder.Append(Environment.NewLine);
                            if (string.IsNullOrEmpty(FormatString))
                                builder.Append(lst[0]);
                            else
                                builder.Append(string.Format(FormatString, lst.ToArray()));
                        }
    
                        content.Value = builder.ToString();
    
                        textControl.Content = content;
                        applyRules(control, null);
                    }
                }
            }
        }
    }
    

    有人知道我如何让微软Word 2007(docx)停止忽略我的换行符吗??

    4 回复  |  直到 13 年前
        1
  •  2
  •   S.L. Barth is on codidact.com Monika Restecka    13 年前

    使用如下函数:

    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;          
    }
    
        2
  •  1
  •   Bojan B Rajeev Ranjan    10 年前

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

    然而我发现 InsertAfter \n 在的原始XML字符串中 \v

    contentControl.MultiLine = true    
    contentControl.Range.InsertAfter(your string)
    
        3
  •  0
  •   Zitun    15 年前

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

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

    逐行创建一个段落,并将所有这些段落添加到表格单元格中。

        4
  •  0
  •   LPL user462990    15 年前

    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();  
    

    .

    static Paragraph NewText(string _text)  
    {  
        Paragraph _head = new Paragraph();  
        Run _run = new Run();  
        Text _line = new Text(_text);  
        _run.Append(_line);  
        _head.Append(_run);  
        return _head;  
    }