Wednesday, 14 August 2013

Create doc file from template and adding data from database using open xml

Create doc file from template and adding data from database using open xml

I have a word template and I want to create doc file from that, also I
want to replace add data in place of bookmarks present in the template. I
have been able to create a doc file, but I am not able to understand, how
to add data in place of bookmarks? My code till now:
private void CreateSampleWordDocument()
{
string sourceFile = Path.Combine(Environment.CurrentDirectory,
"GeneralWelcomeLetter.dotx");
string destinationFile =
Path.Combine(Environment.CurrentDirectory, "Sample.docx");
try
{
File.Copy(sourceFile, destinationFile, true);
WordprocessingDocument document =
WordprocessingDocument.Open(destinationFile, true);
document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
MainDocumentPart mainPart = document.MainDocumentPart;
DocumentSettingsPart documentSettingPart1 =
mainPart.DocumentSettingsPart;
AttachedTemplate attachedTemplate1 = new AttachedTemplate() {
Id = "relationId1" };
documentSettingPart1.Settings.Append(attachedTemplate1);
}
catch
{
}
}
Now to add data from database in place of bookmarks?

No comments:

Post a Comment