如何在C#中实现Word文档三个段落同行显示
Got it, the core issue here is that each of your three text sections lives in a separate Paragraph element—Word automatically inserts a line break between distinct paragraphs. To get them to display as text1,text2,text3 on the same line, you need to merge their content into one paragraph, with commas separating each segment.
Option 1: Merge Existing Paragraphs
If you've already added the three paragraphs and need to fix them after the fact, use this approach to combine their content and remove the extra paragraphs:
using(WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true)) { gjenerimi = randomstring(14); var body = wDoc.MainDocumentPart.Document.Body; // Get all existing paragraphs in the body var paragraphs = body.Elements<Paragraph>().ToList(); // Make sure we have at least 3 paragraphs to work with if (paragraphs.Count >= 3) { // Use the first paragraph as our merged container var mergedParagraph = paragraphs[0]; // Process the second paragraph: add a comma, then clone its content if (paragraphs[1].Elements<Run>().Any()) { mergedParagraph.AppendChild(new Run(new Text(","))); foreach (var run in paragraphs[1].Elements<Run>()) { mergedParagraph.AppendChild(run.CloneNode(true)); } } // Process the third paragraph the same way if (paragraphs[2].Elements<Run>().Any()) { mergedParagraph.AppendChild(new Run(new Text(","))); foreach (var run in paragraphs[2].Elements<Run>()) { mergedParagraph.AppendChild(run.CloneNode(true)); } } // Remove the now-empty second and third paragraphs paragraphs[1].Remove(); paragraphs[2].Remove(); } // Save the changes to the document wDoc.MainDocumentPart.Document.Save(); }
Key Notes for This Option:
- Cloning
Runelements preserves any existing formatting (like font size, color) from your original paragraphs. - We check if each paragraph has content before adding commas to avoid extra commas at the start/end.
Option 2: Add Content in a Single Paragraph (Prevent the Issue Upfront)
If you haven't added the paragraphs yet, or want to avoid the problem entirely, create a single paragraph with all three text segments separated by commas right from the start:
using(WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true)) { gjenerimi = randomstring(14); var body = wDoc.MainDocumentPart.Document.Body; // Create one paragraph to hold all content var combinedParagraph = new Paragraph(); // Add each text segment with comma separators combinedParagraph.AppendChild(new Run(new Text("text1"))); combinedParagraph.AppendChild(new Run(new Text(","))); combinedParagraph.AppendChild(new Run(new Text("text2"))); combinedParagraph.AppendChild(new Run(new Text(","))); combinedParagraph.AppendChild(new Run(new Text("text3"))); // Add the merged paragraph to the document body body.AppendChild(combinedParagraph); wDoc.MainDocumentPart.Document.Save(); }
This is cleaner because it avoids having to clean up extra paragraphs later—you're building the exact structure you want from the beginning.
内容的提问来源于stack exchange,提问作者Arbenita Musliu

