10

我将 MS word doc 保存为.docx. 我想通过编辑 docx 的 XML 文件在我的文本中插入新行。我已经尝试过
, 
, 
, 	, amd 它总是只给我空间而不是新行。

它能做什么:

(XML 代码) <w:t>hel&#xA;lo</w:t>

当我打开.docx文件时,它会更改为:

Hel lo不是因为我想Hel在一条线上和lo二线。

4

3 回答 3

30

使用<w:br/>标签。

我通过创建 Word 文档、将其保存为 XML(通过另存为)、使用 Shift Enter 添加强制换行符并检查更改来找到它。本质区别似乎只是w:br标签,显然反映了HTMLbr标签。

于 2012-11-07T13:55:59.000 回答
2

如果它对任何人有帮助,以下 C# 代码将创建多行 XML 结构

//Sets the text for a Word XML <w:t> node
//If the text is multi-line, it replaces the single <w:t> node for multiple nodes
//Resulting in multiple Word XML lines
private static void SetWordXmlNodeText(XmlDocument xmlDocument, XmlNode node, string newText)
{

    //Is the text a single line or multiple lines?>
    if (newText.Contains(System.Environment.NewLine))
    {
        //The new text is a multi-line string, split it to individual lines
        var lines = newText.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);


        //And add XML nodes for each line so that Word XML will accept the new lines
        var xmlBuilder = new StringBuilder();
        for (int count = 0; count < lines.Length; count++)
        {
            //Ensure the "w" prefix is set correctly, otherwise docFrag.InnerXml will fail with exception
            xmlBuilder.Append("<w:t xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\">");
            xmlBuilder.Append(lines[count]);
            xmlBuilder.Append("</w:t>");

            //Not the last line? add line break
            if (count != lines.Length - 1)
            {
                xmlBuilder.Append("<w:br xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\" />");
            }
        }

        //Create the XML fragment with the new multiline structure
        var docFrag = xmlDocument.CreateDocumentFragment();
        docFrag.InnerXml = xmlBuilder.ToString();
        node.ParentNode.AppendChild(docFrag);

        //Remove the single line child node that was originally holding the single line text, only required if there was a node there to start with
        node.ParentNode.RemoveChild(node);
    }
    else
    {
        //Text is not multi-line, let the existing node have the text
        node.InnerText = newText;
    }
}

上面的代码将创建必要的子节点和回车,并处理前缀。

于 2015-01-16T12:23:06.077 回答
0

根据上面@Lenny的回答,这就是我在Mac上使用MS Word 2011的情况下使用Obj-C的方法:

- (NSString *)setWordXMLText:(NSString *)str
{
    NSString *newStr = @"";
    // split the string into individual lines
    NSArray *lines = [str componentsSeparatedByString: @"\n"];

    if (lines.count > 1)
    {
        // add XML nodes for each line so that Word XML will accept the new lines
        for (int count = 0; count < lines.count; count++)
        {
            newStr = [newStr stringByAppendingFormat:@"<w:t>%@</w:t>", lines[count]];

            // Not the last line? add a line break
            if (count != lines.count - 1)
            {
                newStr = [newStr stringByAppendingString:@"<w:br/>"];
            }
        }

        return newStr;
    }
    else
    {
        return str;
    }
}
于 2017-06-17T08:49:32.913 回答