コード例 #1
0
 /// <summary>
 /// Creates a new word document from the specified template and then performas a mail merge.
 /// </summary>
 /// <param name="templateFilePath">The mail merge template document to use</param>
 /// <param name="outputFilePath">The name of the new file.</param>
 /// <param name="data">The data to use for the mail merge.</param>
 /// <param name="mappings">The mappings to use to match Word MAILMERGE fields with Data Columns in the specified data.</param>
 /// <param name="mergeType">Specifies whether to merge to a single document or multiple documents</param>
 public static void MailMerge(string templateFilePath, string outputFilePath, DataTable data, MailMergeType mergeType, IEnumerable <MailMergeMapping> mappings)
 {
     if (mergeType == MailMergeType.MultiDocument)
     {
         foreach (DataRow row in data.Rows)
         {
             MailMerge(templateFilePath, GetUniqueFileName(outputFilePath), row, mappings);
         }
     }
     else
     {
         MailMerge(templateFilePath, outputFilePath, data, mappings);
     }
 }
コード例 #2
0
        /// <summary>
        /// Creates a new word document from the specified template and then performas a mail merge.
        /// </summary>
        /// <param name="templateFilePath">The mail merge template document to use</param>
        /// <param name="outputFilePath">The name of the new file.</param>
        /// <param name="data">The data to use for the mail merge.</param>
        /// <param name="mergeType">Specifies whether to merge to a single document or multiple documents</param>
        public static void MailMerge(string templateFilePath, string outputFilePath, DataTable data, MailMergeType mergeType)
        {
            var mappings = new List <MailMergeMapping>();

            data.Columns.Cast <DataColumn>().ForEach(column =>
            {
                mappings.Add(new MailMergeMapping
                {
                    DataColumnFieldName = column.ColumnName,
                    MailMergeFieldName  = column.ColumnName
                });
            });

            MailMerge(templateFilePath, outputFilePath, data, mergeType, mappings);
        }