2012-11-07 4 views
10

나는 MS 워드 문서를 .docx으로 저장했습니다. docx의 XML 파일을 편집하여 내 텍스트에 새로운 행을 삽입하고 싶습니다. 나는 이미 시도했다 
, 
, 
, 	, amd 그것은 항상 내게 새로운 라인이 아닌 유일한 공간을 준다.XML - 새 줄 추가

그것은 무엇을 :

그때 .docx 파일을 열 때 (XML 코드) <w:t>hel&#xA;lo</w:t>

이 변경에 :

Hel lo 내가 한 줄과 loHel되고 싶어하지 두 번째 줄에.

+0

당신이 단어 편집을하고 시도하고, 그 차이를 조사했다? –

+0

과 같은 작업을 수행합니다 ... 그러나 DB에서 데이터를로드하고로드 할 모든 이름을 새 행에 하나씩 갖고 싶기 때문에 새 행 문자에 대한 코드를 사용해야합니다. 무슨 뜻이야 –

+0

정말로 .docx 파일을 편집하고 있습니까? 방법? (그것들은 XML과 같은 것이 아니라 XML을 압축합니다.) –

답변

26

<w:br/> 태그를 사용하십시오.

Word 문서를 만들어 XML로 저장하고 (다른 이름으로 저장) Shift 키를 누른 상태에서 강제 줄 바꿈을 추가하여 변경 사항을 확인했습니다. 본질적인 차이점은 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 워드 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; 
    } 
}