2014-09-04 53 views
0

我想編輯Word文檔,以開放的XML一個的docx,但我沒有一個令人滿意的結果:編輯與開放的XML

這裏是原始文件:

enter image description here

在這裏,我剛剛得到一句話來啓動文件。並在:後我必須添加幾行。

我想要的結果是這樣的(因爲某些原因,我不得不抹去屏幕上的一些DATAS,但這並不影響這個問題):

enter image description here

但我只得到了一個結果是這樣的:

enter image description here

因此,在第三圖像,文本不居中,也沒有任何標籤。 另外,不是粗體(如果不是下劃線,那不是問題)。

但真正的問題是,它是左對齊,沒有選項卡,而它應該與選項卡中心對齊。

這裏是我的代碼:

        Text text = sdtFils.Descendants<Text>().FirstOrDefault(); 
            string[] lst = variableCourrier.Tables[0].Rows[0][v_ligne["NOM_CHAMP"].ToString()].ToString().Split("$".ToCharArray()); 
            Paragraph existPar = sdtFils.Descendants<Paragraph>().FirstOrDefault(); 
            if (existPar == null) 
            { 
             existPar = sdtFils.Ancestors<Paragraph>().FirstOrDefault(); 
            } 
            Run existRun = sdtFils.Descendants<Run>().FirstOrDefault(); 
            foreach (string str in lst) 
            { 
             Paragraph p = new Paragraph(); 

             ParagraphProperties pPr = new ParagraphProperties(new Tabs(new TabStop() { Val = TabStopValues.Center, Position = 7372 }), 
              new Justification() { Val = JustificationValues.Left }, 
              new ParagraphStyleId() { Val = "Paragraphedeliste" }, 
              new Indentation() { Start = (int.Parse(existPar.ParagraphProperties.Indentation.Left.Value) + int.Parse(existPar.ParagraphProperties.Indentation.FirstLine.Value)).ToString() }); 

             string[] lstDetails = str.Split(";".ToCharArray()); 
             int j = 0; 
             foreach (string strDetail in lstDetails) 
             { 
              if (!string.IsNullOrEmpty(strDetail)) 
              { 
               TabChar tab = new TabChar(); 
               Text t = new Text(); 
               Run r = new Run(); 
               RunProperties runPr = new RunProperties(new FontSize() { Val = existRun.RunProperties.FontSize.Val }); 

               r.AppendChild(runPr); 
               t.Text = j == 0 ? "" + strDetail : " " + strDetail; 
               r.AppendChild(t); 


               if (j != lstDetails.Length - 1) 
               { 
                Break br = new Break(); 
                r.AppendChild(br); 
               } 
               p.AppendChild(r); 
               j++; 
              } 
             } 

             text.Parent.Parent.Parent.Parent.InsertAfterSelf(p); 
            } 

有了這個代碼,我將一個字符串分解的各部分,這樣我可以把它們放進自己的線路。

我認爲不正確的部分是當我定義Paragraph元素,但我沒有看到如何糾正它,以獲得將看起來像第二個圖像的reuslt。

有人可以幫助我嗎?

如果不夠清楚,請告訴我,我會編輯該問題。

謝謝。

回答

0

您可以使用的Open XML SDK 2.5生產力工具(記住一個.docx文件是壓縮到一個文件中的XML文檔的集合),打開你的*的.docx,然後單擊反映代碼看到需要的代碼正確設置文檔的樣式。你

enter image description here

也可以使用Open XML Package Editor for Visual Studio加載項,提供了一種簡單的方法來分析和編輯開放式打包約定文件,包括Word,Excel,PowerPoint和Visio文檔。

一切都是open source並在Github上提供。