Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.60/5 (3 votes)
See more:
Hi guys,

The situation is like this:

I have some information gathered from database. And I have an template of Word document which already well designed with the headers, table etc.

Now I want to add the information gathered from the db to the table in the word document and save it in different place not overriding the template which I have. Because I want to use the same template again and again.

I have no idea at all how to do this.Can you guys help on this?
Examples really will help for me to understand better.

Thanks,
skunkhead :)

ps: if this cant be done, other suggestion are much appreciated!
Posted
Updated 28-Feb-11 17:12pm
v2

Hope this[^] might help you.
 
Share this answer
 
Well,

you can [spam link removed] file programatically, but what you really should be doing is [spam link removed] Word file with DataTable from your database.

Here is a sample C# code how to accomplish this with [spam link removed] component:
C#
// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Define DataTable with two columns: 'Name' and 'Surname', and fill it with some data.
// You don't have to do this if you already have a DataTable instance.
var dataTable = new DataTable("People")
{
  Columns =
  {
    new DataColumn("Name", typeof(string)),
    new DataColumn("Surname", typeof(string))
  },
  Rows =
  {
    new object[] { "John", "Doe" },
    new object[] { "Fred", "Nurk" },
    new object[] { "Hans", "Meier" },
    new object[] { "Ivan", "Horvat" }
  }
};

// Create and save a template document. 
// You don't have to do this if you already have a template document.
// This code is only provided as a reference how template document should look like.
var document = new DocumentModel();
document.Sections.Add(
  new Section(document,
    new Table(document,
      new TableRow(document,
        new TableCell(document,
          new Paragraph(document, "Name")),
        new TableCell(document,
          new Paragraph(document, "Surname"))),
      new TableRow(document,
        new TableCell(document,
          new Paragraph(document,
            new Field(document, FieldType.MergeField, "RangeStart:People"),
            new Field(document, FieldType.MergeField, "Name"))),
        new TableCell(document,
          new Paragraph(document,
            new Field(document, FieldType.MergeField, "Surname"),
            new Field(document, FieldType.MergeField, "RangeEnd:People")))))));
document.Save("TemplateDocument.docx", SaveOptions.DocxDefault);

// Load a template document.
document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault);

// Mail merge template document with DataTable.
// Important: DataTable.TableName and RangeStart/RangeEnd merge field names must match.
document.MailMerge.ExecuteRange(dataTable);

// Save the mail merged document.
document.Save("Document.docx", SaveOptions.DocxDefault);
 
Share this answer
 
v2
Please try to use can use Open XML SDK:
http://www.microsoft.com/en-us/download/details.aspx?id=30425[^].

This way, you can support new XML-based Office formats (such as .DOCX, .XLSX), ECMA-376 standard, even if Office is not installed:
http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats[^],
http://en.wikipedia.org/wiki/Office_Open_XML[^].

This is another option: http://npoi.codeplex.com/[^].

—SA
 
Share this answer
 
Look into T4 templates[^]Hope this helps.
 
Share this answer
 
Try this C# Word Library , its an API that can edit and save your word document using c# language. I hope it will solve your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900