Hi all,
Im totally new to open xml, just started playing some code after study some article on msdn.
I had try to direct save user input textbox data into a msWord document in bin folder with open XML with C#.
I would like to know is that possible to save the data from a dummy list i created in C#? Do I need to write something to read the data from list line by line then insert into document line by line? Or i just need to modify something from code below?
Please guide. Any useful link or video for beginner to lear step by step will be very helpful for me.
string docName = "OpenXML.doc"; List<Person> people = new List<Person>(); people.Add(new Person(50, "Fred")); people.Add(new Person(30, "John")); people.Add(new Person(26, "Andrew")); people.Add(new Person(24, "Xavier")); people.Add(new Person(5, "Mark")); people.Add(new Person(6, "Cameron")); // Create a Wordprocessing document. using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document)) { // Add a new main document part. package.AddMainDocumentPart(); // Create the Document DOM. package.MainDocumentPart.Document = new Document( new Body( new Paragraph( new Run( new DocumentFormat.OpenXml.Wordprocessing.Text(people))))); // Save changes to the main document part. package.MainDocumentPart.Document.Save(); }
Thank you very much!!
PC
I'm not clear if this is working code since you don't show the Person class. At best, you would have all the text in one paragraph with that code. If you want to see each item in separate paragraph, you should use something like this:
package.MainDocumentPart.Document = new Document( new Body(people.Select(p => new Paragraph(new Run(p.ToString()))).ToArray()));
The p.ToString() assumes that you override that method in Person to produce the desired text for each item.
If you want something more complex, like bullet lists or numbered lists, then you will need to create the appropriate XML for those formats. I suggest that you create a document manually in Word, then examine the XML to see how it should be done.