using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace WordDocManipulation
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\sample.docx";
string strtxt = "Hello This is done by programmatically";
OpenAndAddTextToWordDocument(path,strtxt);
}
public static void OpenAndAddTextToWordDocument(string filepath, string txt)
{
/* I want to the below text to be added in the new section */
// Open a WordprocessingDocument for editing using the filepath.
WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(filepath, true);
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
// Close the handle explicitly.
wordprocessingDocument.Close();
}
}
}
Я хочу добавить разрыв раздела в конце документа и добавить немного текста.
Мой код выглядит следующим образом:
[code]using System; using System.Collections.Generic; using System.Linq; using System.Text; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing;
namespace WordDocManipulation { class Program { static void Main(string[] args) {
string path = @"C:\sample.docx"; string strtxt = "Hello This is done by programmatically";
OpenAndAddTextToWordDocument(path,strtxt); } public static void OpenAndAddTextToWordDocument(string filepath, string txt) { /* I want to the below text to be added in the new section */
// Open a WordprocessingDocument for editing using the filepath. WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true);
// Assign a reference to the existing document body. Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text. Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text(txt));
// Close the handle explicitly. wordprocessingDocument.Close(); } } } [/code]