问题
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