使用c#修改和保存word文档 [英] Modify and save word document using c#
问题描述
大家好,
情况是这样的:
我收集了一些信息数据库。我有一个Word文档模板,它已经很好地设计了标题,表格等。
现在我想将从db收集的信息添加到表中单词文档并将其保存在不同的地方,而不是覆盖我所拥有的模板。因为我想一次又一次地使用相同的模板。
我根本不知道如何做到这一点。你能帮忙吗?
例子真的有助于我更好地理解。
谢谢,
skunkhead :)
ps:如果不能这样做,其他建议非常感谢!
这些链接可能对你有所帮助 -
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 =
{
< span class =code-keyword> 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,< span class =code-string> 名称))),
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的邮件合并模板文档。
< span class =code-comment> // 重要: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);
这篇关于使用c#修改和保存word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!