C#实现对Word文件读写

手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可。

实现方法:首先需要引用VBA组建,我用的是Office2003 Professional,Dll版本号为Microsoft Word11.0,

 

另外当然还需要引用Interop.Word.Dll.

代码如下:

#region 打开Word文档,并且返回对象wDoc,wDoc
  /// <summary>
  /// 打开Word文档,并且返回对象wDoc,wDoc
  /// </summary>
  /// <param name="FileName">完整Word文件路径+名称</param>  
  /// <param name="wDoc">返回的Word.Document wDoc对象</param>
  /// <param name="WApp">返回的Word.Application对象</param>
  public static void CreateWordDocument(string FileName,ref Word.Document wDoc,ref  Word.Application WApp)
  {
   if(FileName == "") return;
   Word.Document thisDocument = null;
   Word.FormFields   formFields = null;
   Word.Application thisApplication = new Word.ApplicationClass();
   thisApplication.Visible = true;
   thisApplication.Caption = "";
   thisApplication.Options.CheckSpellingAsYouType = false;
   thisApplication.Options.CheckGrammarAsYouType = false;

   Object filename = FileName;
   Object ConfirmConversions = false;
   Object ReadOnly = true;
   Object AddToRecentFiles = false;

   Object PasswordDocument = System.Type.Missing;
   Object PasswordTemplate = System.Type.Missing;
   Object Revert = System.Type.Missing;
   Object WritePasswordDocument = System.Type.Missing;
   Object WritePasswordTemplate = System.Type.Missing;
   Object Format = System.Type.Missing;
   Object Encoding = System.Type.Missing;
   Object Visible = System.Type.Missing;
   Object OpenAndRepair = System.Type.Missing;
   Object DocumentDirection =  System.Type.Missing;
   Object NoEncodingDialog = System.Type.Missing;
   Object XMLTransform = System.Type.Missing;

   try
   {
    Word.Document wordDoc =
     thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
     ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
     ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
     ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
     ref NoEncodingDialog, ref XMLTransform );
    
    thisDocument = wordDoc;
    wDoc = wordDoc;
    WApp = thisApplication;
    formFields = wordDoc.FormFields;

  }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   
  }
  #endregion

 

 

调用上面静态方法,打开目标文件并且把DataGrid中数据填充到对应Word标签中去

 

#region Word填充数据(For Example)
  /// <summary>
  /// Word填充数据
  /// </summary>
  private void WordLoadData()
  {
   Word.Document wDoc=null;
   Word.Application wApp=null;
   sysFun.CreateWordDocument("E://监测报告(new).dot",ref wDoc,ref wApp);

   //对标签"C"进行填充
   object bkmC="C";
   if(wApp.ActiveDocument.Bookmarks.Exists("C") == true)
   {
    wApp.ActiveDocument.Bookmarks.get_Item
     (ref bkmC).Select();
   }
   wApp.Selection.TypeText(this.txt1.Text);
   object bkmG = "TWaterTable3";
   object unit;
   object count; //移动数
   object extend;
    

   extend = Word.WdMovementType.wdExtend;
   unit = Word.WdUnits.wdCell;
   //把DataGrid中数据填充到标签TWaterTable3上
   if(wApp.ActiveDocument.Bookmarks.Exists("TWaterTable3") == true)
   {
    wApp.ActiveDocument.Bookmarks.get_Item
     (ref bkmG).Select();

    for(int i=0;i<this.gridEX1.RecordCount;i++)
    {
     if(i==0)
     {
      count=1;
     }
     else
     {
      count=0;
     }
     //需填充5列数据
     wApp.Selection.Move(ref unit,ref count);
     wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[0].Text);
     count=1;
      
     wApp.Selection.Move(ref unit,ref count);
     wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text);
      
     wApp.Selection.Move(ref unit,ref count);
     wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text);
      
     wApp.Selection.Move(ref unit,ref count);
     wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text);
     
     wApp.Selection.Move(ref unit,ref count);
     wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[4].Text);
     //换行
     wApp.Selection.MoveRight(ref unit,ref count,ref extend);

  }
   }
  }
  #endregion

 

然后就OK了,在对标签表控制要注意列循环和换行,不知道还有没有其它好办法,欢迎探讨!

### 回答1: 这个错误一般是因为项目中使用的SnakeYAML库版本不匹配。 方法setMaxAliasesForCollections是在SnakeYAML 1.25版本中添加的。在更早的版本中将找不到这个方法。 要解决这个问题,需要使用正确版本的SnakeYAML库。可以通过更新pom.xml或gradle.build文件来达到这个目的。或者,也可以手动下载所需版本的SnakeYAML库并将其添加到项目中的classpath中。 此外,还应注意库依赖关系的完整性和正确性,并在项目构建时执行依赖项升级。 总之,这个错误可以通过检查SnakeYAML库的版本和更新项目依赖关系来解决。 ### 回答2: 这个错误通常发生在使用SnakeYAML库时未能正确配置选项的情况下。要理解这个错误,我们需要了解一下SnakeYAML库和它的LoaderOptions类。 SnakeYAML是一个Java库,用于解析和生成YAML格式的数据。它包括一个名为LoaderOptions的类,用于设置YAML加载器的选项。其中一个选项是setMaxAliasesForCollections,它指定一个集合中的最大别名数量。如果未正确配置此选项,则会引发java.lang.nosuchmethoderror错误,因为Java无法找到设置该选项的方法。 要解决这个问题,我们可以在代码中添加以下代码: LoaderOptions options = new LoaderOptions(); options.setMaxAliasesForCollections(100); Yaml yaml = new Yaml(options); InputStream inputStream = new FileInputStream("example.yml"); Object data = yaml.load(inputStream); 以上代码创建了一个LoaderOptions对象,并指定了setMaxAliasesForCollections选项的值。然后,我们将此选项传递到Yaml对象的构造函数中。最后,我们使用Yaml对象加载一个YAML文件,并将其解析为一个Java对象。 总之,要解决java.lang.nosuchmethoderror错误,我们需要正确配置SnakeYAML库的选项,特别是LoaderOptions类的setMaxAliasesForCollections选项。只有在正确配置选项后,我们才能成功解析YAML数据。 ### 回答3: java.lang.nosuchmethoderror: org.yaml.snakeyaml.loaderoptions.setmaxaliasesf 错误表示在加载 YAML 文件时,JVM 发现了一个无法解决的方法或方法参数问题。具体地说,这个错误是由于在代码中使用了 org.yaml.snakeyaml.loader.LoaderOptions 类的 setMaxAliasesForCollections 方法,但该方法不存在或未正确设置导致的。这个错误通常会在运行时出现,并且很难通过编译或静态分析工具检测到。 要解决这个问题,您可以采取以下几个步骤: 1. 检查版本兼容性。在运行时发生这个错误通常是因为不同版本的库之间存在兼容性问题。请确保您使用的 org.yaml.snakeyaml 库的版本与您的代码中使用的版本相同,并且这两个版本都与您的应用程序的其他库相兼容。 2. 检查参数。如果您使用了 setMaxAliasesForCollections 方法,请确保参数设置正确,并且与您的代码中的其他逻辑一致。您可以参考官方文档或其他相应的代码示例,以确保您代码中的参数设置正确。 3. 检查其他代码库。如果您的代码还使用了其他库,这些库也可能对 YAML 文件的加载产生影响。请确保您的应用程序的其他库版本与 org.yaml.snakeyaml 库的版本相同,并且这些库对 YAML 文件的加载没有任何影响。 总之,java.lang.nosuchmethoderror: org.yaml.snakeyaml.loaderoptions.setmaxaliasesf 错误通常是由版本兼容性或方法参数问题引起的。通过检查版本和参数设置,并确保与其他库相兼容,可以解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值