对word文档编程,以及从模板文档批量生成新的word文档。

还是在帮hr作文档批量生成系统的时候,需要对word文档编程,并且要根据模板批量生成新的文档。参考microsoft office 2003帮助文件之后知道了,office本身提供了编程类库,在office11文件夹低下的msword.olb。在.net开发环境中新建一个project,引入此类库,便能很方便的对word文档编程。留下实例代码,以便后用和他用:

using System;

namespace TestGrid
{
 /// <summary>
 /// Summary description for Employee.
 /// </summary>
 public class Employee
 {

  /// <summary>
  /// Initializes an new instance of <see cref="Employee"/> class.
  /// </summary>
  public Employee()
  {
  }
  
  /// <summary>
  /// Initializes an new instance of <see cref="Employee"/> class.
  /// </summary>
  /// <param name="lastName">The last name.</param>
  /// <param name="position">The position.</param>
  /// <param name="salary">The salary.</param>
  public Employee(string lastName,string position,string salary)
  {
   this.m_lastName = lastName;
   this.m_position = position;
   this.m_salary = salary;
  }

  /// <summary>
  /// Private field of the <see cref="LastName"/> property.
  /// </summary>
  private string m_lastName;

  /// <summary>
  /// Private field of the <see cref="Position"/> property.
  /// </summary>
  private string m_position;

  /// <summary>
  /// Private field of the <see cref="Salary"/> property.
  /// </summary>
  private string m_salary;


  /// <summary>
  /// Gets or sets the last name.
  /// </summary>
  /// <value>The last name.</value>
  /// <exception cref="ArgumentNullException">
  /// Throws while the value is <see langword="null"/>.
  /// </exception>
  /// <exception cref="ArgumentNullException">
  /// Throws while the value is an empty string.
  /// </exception>
  public string LastName
  {
   get
   {
    return this.m_lastName;
   }
   set
   {
    if(value == null)
     throw new ArgumentNullException("lastName");
    if(value.Length == 0)
     throw new ArgumentNullException("lastName");
    this.m_lastName = value;
   }
  }

  /// <summary>
  /// Gets or sets the position.
  /// </summary>
  /// <value>The position.</value>
  /// <exception cref="ArgumentNullException">
  /// Throws while the value is <see langword="null"/>.
  /// </exception>
  /// <exception cref="ArgumentNullException">
  /// Throws while the value is an empty string.
  /// </exception>
  public string Position
  {
   get
   {
    return this.m_position;
   }
   set
   {
    if(value == null)
     throw new ArgumentNullException("position");
    if(value.Length == 0)
     throw new ArgumentNullException("position");
    
    this.m_position = value;
   }
  }

  /// <summary>
  /// Gets or sets the salary.
  /// </summary>
  /// <value>The salary.</value>
  /// <exception cref="ArgumentNullException">
  /// Throws while the value is <see langword="null"/>.
  /// </exception>
  /// <exception cref="ArgumentNullException">
  /// Throws while the value is an empty string.
  /// </exception>
  public string Salary
  {
   get
   {
    return this.m_salary;
   }
   set
   {
    if(value == null)
     throw new ArgumentNullException("salary");
    if(value.Length == 0)
     throw new ArgumentNullException("salary");

    this.m_salary = value;
   }
  }
 }
}

using System;
using Microsoft.Office.Core;
using Microsoft.Office.Interop;

namespace TestGrid
{
 /// <summary>
 /// Summary description for WordOperation.
 /// </summary>
 public class WordOperation
 {
  public WordOperation()
  {
  }

  /// <summary>
  /// Generates word documents with the information of employees.
  /// </summary>
  /// <param name="employees">A set of employee.</param>
  public static void GenerateWordDocument(Employee[] employees)
  {
   Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
   wordApplication.Visible = false;

   Object missingValue = Type.Missing;
   Object strFileName = @"D:/offer.doc";

   Microsoft.Office.Interop.Word.Document tempDocument = null;
   Object forward = true;
   Object strFindName = @"#@name@#"; //The string to be replaced.
   Object strFindSalary = @"#@salary@#"; //The string to be replaced.
   Object strFindPosition = @"#@position@#"; //The string to be replaced.
   
   Object strReplaceName;
   Object strReplacePosition;
   Object strReplaceSalary;

   foreach(Employee employee in employees)
   {
    strReplaceName = employee.LastName;
    strReplacePosition = employee.Position;
    strReplaceSalary = employee.Salary;

    //Open and save the template file as another name.
    Object strSaveAsName ="d://" + (string)strReplaceName + ".doc";
    Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Open(
     ref strFileName,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue);
    wordDocument.SaveAs(
     ref strSaveAsName,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue);
    wordDocument.Close(ref missingValue,ref missingValue,ref missingValue);

    //Open the new file.
    tempDocument = wordApplication.Documents.Open(
     ref strSaveAsName,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue);

    //Replaces name.
    tempDocument.Content.Find.Replacement.ClearFormatting();
    tempDocument.Content.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    tempDocument.Content.Find.Execute(
     ref strFindName,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref forward,
     ref missingValue,
     ref missingValue,
     ref strReplaceName,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue);

    //Remplaces position
    tempDocument.Content.Find.Replacement.ClearFormatting();   
    tempDocument.Content.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    tempDocument.Content.Find.Execute(
     ref strFindPosition,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref forward,
     ref missingValue,
     ref missingValue,
     ref strReplacePosition,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue);
    
    //Replaces salary
    tempDocument.Content.Find.Replacement.ClearFormatting();
    tempDocument.Content.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    tempDocument.Content.Find.Execute(
     ref strFindSalary,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref forward,
     ref missingValue,
     ref missingValue,
     ref strReplaceSalary,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue,
     ref missingValue);

    //Saves and closes the document
    tempDocument.Save();
    tempDocument.Close(ref missingValue,ref missingValue,ref missingValue);
   }
  }
 }
}

如果是web application,可能会出现访问不允许(access denied)的问题,需要对iis和web.config进行配置。

在IIS里选择工程对应的虚拟目录,右键选择属性,在DIRECTORY SECURITY 下选择EDIT,反选anonymous access选上Bacis authentication (我们公司所有的认证都是给予LDAP)的;并且在web.config 文件中添加<identity impersonate="true"/>。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值