Click here to Skip to main content
12,297,854 members (50,286 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# DBA
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 Smile | :)

ps: if this cant be done, other suggestion are much appreciated!
Posted 28-Feb-11 16:51pm
Edited 28-Feb-11 17:12pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hope this[^] might help you.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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:
// 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);
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

Look into T4 templates[^]Hope this helps.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 3 Oct 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100