2012-11-07 64 views
10

我將MS word文檔另存爲.docx。我想通過編輯docx的XML文件在我的文本中插入新行。我已經試過
,
,
,	,它總是隻給我空間而不是換新線。XML - 添加新行

它做什麼:

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

當我打開.docx文件,那麼它更改爲:

Hel lo並不像我想成爲Hel在一行和lo在secound線。

+0

你試過做編輯的文字,並檢查區別在哪裏? –

+0

是的我有它像 ...但我需要使用代碼換行字符,因爲我將加載數據從數據庫和我將加載的所有名稱希望每一個新行...我希望你明白我的意思是 –

+0

你真的在編輯.docx文件嗎?怎麼樣? (他們不是XML本身,而是壓縮XML。) –

回答

26

使用<w:br/>標記。

我通過創建一個Word文檔,將其保存爲XML(通過另存爲),使用Shift Enter添加強制換行符並檢出更改來找到它。本質區別似乎只是w:br標籤,顯然反映了HTML br標籤。

+0

你爲我節省了很多時間! Thx回答! –

+0

似乎很明顯,但實際上需要做的是將''標籤全部替換爲''...... – Sebas

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; 
    } 
} 

上面的代碼將創建必要的子節點,並回車,並採取前綴的護理以及。

0

基於@以上萊尼的答案,這是使用的OBJ-C與MS Word 2011在Mac上什麼工作在我的情況:

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