2012-11-07 23 views
10

私はMSワードdocを.docxとして保存しました。 docxのXMLファイルを編集することでテキストに新しい行を挿入したい。私はすでに試しました

	、それは私に新しい行ではなく、常に私にスペースを与えます。私は1本のライン上のHelloになりたかったようXML - 改行を追加する

Hel loない:私は.docxファイルを開くと

(XMLコード) <w:t>hel&#xA;lo</w:t>

は、その後、それが変更された:それは何をするか

セカンドラインで。

+0

は、Wordで編集をやってみました、との違いを調べてきましたか? –

+0

はい、それはのようなことをしています...しかし、私はDBからデータを読み込んでおり、ロードするすべての名前は新しい行にそれぞれ1つずつ持ちたいので、新しい行の文字にコードを使用する必要があります。何を意味するのですか –

+0

本当に.docxファイルを編集していますか?どうやって? (それらはXMLのようなものではありませんが、XMLを圧縮します。) –

答えて

26

<w:br/>タグを使用してください。

Word文書を作成してXMLとして保存し(Save Asを使用)、Shift Enterで強制改行を追加して変更をチェックアウトしました。本質的な違いはちょうどw:brタグのようで、明らかにHTML brタグを反映しています。

+0

あなたは私に多くの時間を救った!答えのためのThx! –

+0

明白に思えるかもしれませんが、実際に実行する必要があるのは、 ''タグをすべて ' 'と置き換えることです。 – Sebas

2
それはC#コードの次のビットは、上記のコードは子ノードとキャリッジリターン必要が作成され

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

マルチラインXML構造を作成し、誰を助け、そして接頭辞の世話をする場合に

同じように。

0

上記レニーの答え@に基づいて、これは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; 
    } 
} 
関連する問題