且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用c#修改和保存word文档

更新时间:2023-01-30 16:18:42

这些链接可能对你有所帮助 -

http://www.techrepublic。 com / blog / howdoi / how-do-i-modify-word-documents-using-c / 190 [ ^ ]

http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx [ ^ ]


希望这个 [ ^ ]可能会帮助你。


好吧,



你可以[编程删除垃圾邮件]文件,但你应该是什么做的是[删除垃圾邮件链接]来自数据库的带有DataTable的Word文件。



这是一个示例C#代码如何通过[垃圾邮件链接删除]组件实现此目的:

  //  在免费模式下使用该组件。 
ComponentInfo.SetLicense( FREE-LIMITED-KEY);

// 使用两列定义DataTable:'Name'和'Surname',并填写它带有一些数据。
// 如果你已经这样做,你不必这样做有一个DataTable实例。
var dataTable = new DataTable( People
{
Columns =
{
new DataColumn( 名称 typeof string )),
new DataColumn( typeof string ))
},
Rows =
{
new object [] { John Doe},
new object [] { Fred Nurk},
new object [] { Hans Meier },
new object [] { Ivan Horvat}
}
};

// 创建并保存模板文档。
// 如果您已有模板文档,则不必执行此操作。
// 此代码仅作为参考提供模板文档的外观。
var document = new DocumentModel();
document.Sections.Add(
new 部分(文件,
new 表格(文件,
TableRow(文件,
TableCell( document,
new 段落(文档, 名称)),
new TableCell(文档,
new 段落(文档, ))),
new TableRow(文档,
new TableCell(文档,
段落(文件,
字段(文档,FieldType.MergeField, RangeStart:People),
new 字段(document,FieldType.MergeField, 名称))),
new TableCell(文档,
段落(文档,
字段(document,FieldType.MergeField, ),
new 字段(document,FieldType.MergeField, RangeEnd:People )))))));
document.Save( TemplateDocument.docx,SaveOptions.DocxDefault);

// 加载模板文档。
document = DocumentModel .Load( TemplateDocument.docx,LoadOptions.DocxDefault);

// 带有DataTable的邮件合并模板文档。
// 重要:DataTable.TableName和RangeStart / RangeEnd合并字段名称必须匹配。
document。 MailMerge.ExecuteRange(dataTable中);

// 保存邮件合并文档。
文档。保存( Document.docx,SaveOptions.DocxDefault);


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!

These links might help you -
http://www.techrepublic.com/blog/howdoi/how-do-i-modify-word-documents-using-c/190[^]
http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx[^]


Hope this[^] might help you.


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);