我希望通过将标题与文本分离来向Word文档添加文本。在添加标题时,我希望应用适当的样式。
以下是我所做的:
private void Add_Text_To_Word()
{
string file_location = @"D:Bureaumy_word.docx";
string my_title = "TITLE TITLE TITLE";
string my_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(file_location);
app.Visible = true;
object missing = System.Reflection.Missing.Value;
object oEndOfDoc = "\endofdoc"; // endofdoc is a predefined bookmark
int page = 0;
while (page < 3) // For 3 pages (1 title + 1 text + 1 page break)
{
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
Word.Range rngEndOfDoc = doc.Bookmarks[oEndOfDoc].Range;
rngEndOfDoc.InsertAfter("n");
rngEndOfDoc.Collapse(ref oCollapseEnd);
rngEndOfDoc.Text = my_title;
rngEndOfDoc.set_Style(Word.WdBuiltinStyle.wdStyleHeading1);
doc.Paragraphs.Add(rngEndOfDoc);
Word.Range rngEndOfDoc2 = doc.Bookmarks[oEndOfDoc].Range;
rngEndOfDoc.InsertAfter("n");
rngEndOfDoc.Collapse(ref oCollapseEnd);
rngEndOfDoc2.Text = Environment.NewLine + my_text;
rngEndOfDoc2.set_Style(Word.WdBuiltinStyle.wdStyleBodyText);
doc.Paragraphs.Add(rngEndOfDoc2);
doc.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak);
page++;
} // -------------------------
}
结果如下:
结果
但这就是我想做的:
通缉
谢谢你的帮助!
有一个解决方案:
// Title
Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = title;
oPara2.set_Style(Word.WdBuiltinStyle.wdStyleHeading1);
oPara2.Range.InsertParagraphAfter();
// Text
Word.Paragraph oPara3;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = text;
oPara3.set_Style(Word.WdBuiltinStyle.wdStyleBodyText);
oPara3.Range.InsertParagraphAfter();
// Page break
oDoc.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak);
我必须在文本之后设置样式。
这是不工作:
// Title
Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara2.set_Style(Word.WdBuiltinStyle.wdStyleHeading1);
oPara2.Range.Text = title;
oPara2.Range.InsertParagraphAfter();