How to insert text in to a word document from Rich Text Box

拈花ヽ惹草 提交于 2019-12-07 13:04:50

问题


I have a rich text box in my c# application and it contains different lines of text(one to many lines). I want to get those lines and insert it to a Word document with the font and the font size I want. This is how it should look like.

OBSERVATIONS:

  • Line 1 from the rich text box
  • Line 2 from the rich text box
  • Line 3 from the rich text box

I want this to happen by using interop library.

I need some sample code for this since I'm a newbie


回答1:


You could use the OpenXML SDK Package:

http://www.microsoft.com/en-us/download/details.aspx?id=5124

And then do something like the following:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

const string fileName = @"C:\YourFile.docx";
string dataToInsert = txtYourRichTextBox.Text;

using (var document = WordprocessingDocument.Open(fileName, true))
{
   var doc = document.MainDocumentPart.Document;
   document.Body.Append(dataToInsert);
   document.Save();
}


来源:https://stackoverflow.com/questions/10362152/how-to-insert-text-in-to-a-word-document-from-rich-text-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!