zoukankan      html  css  js  c++  java
  • C# word 通过书签拆分文档。Microsoft.Office.Interop.Word

     1  public static ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
     2 
     3         /// <summary>
     4         /// 拆分word
     5         /// </summary>
     6         public static void CovertWord(string filePath, string name)
     7         {
     8             
    10             object missing = System.Reflection.Missing.Value;12             try
    13             {15                 Document doc = ReadDocument(filePath);//读取文件
    16 
    17                 for (int i = 1; i <= doc.Bookmarks.Count; i++)
    18                 {
    19                     object index = i;
    20                     Bookmark bm = doc.Bookmarks.get_Item(ref index);
    21                     if (bm.Name == "技术报告" || bm.Name == "结果报告")
    22                     {
    23                         //复制书签内容
    24                         bm.Range.Copy();
    25                         //创建新文档储存复制内容
    26                         Document docto = CreateDocument();
    27                         docto.Content.Paste();
    28                         //创建储存文件路径 
    29                         object filename =  @"E:1" + name + bm.Name + ".docx";
    30                         docto.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing,
    31                             ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
    32                             ref missing, ref missing, ref missing, ref missing, ref missing);
    33                         docto.Close(ref missing, ref missing, ref missing);36                     }
    37                 }
    38                 return listPath;
    39 
    40             }
    41             catch (Exception ex)
    42             {
    43                 throw new Exception(ex.Message);
    44             }
    45             finally
    46             {
    47                 //清理回收站
    48                 GC.Collect();
    49                  //word.Quit(ref missing, ref missing, ref missing);
    50             }
    51 
    52         }
     1 /// <summary>
     2         /// 读取word文档
     3         /// </summary>
     4         /// <param name="path">word文档路径</param>
     5         /// <returns></returns>
     6         public static Document ReadDocument(string path)
     7         {
     8             object missing = System.Reflection.Missing.Value;
     9             Type wordType = word.GetType();
    10             Documents docs = word.Documents;
    11             Type docsType = docs.GetType();
    12 
    13             object objDocName = path;
    14             Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
    15 
    16             return doc;
    17         }
     1   /// <summary>
     2         /// 创建word文档
     3         /// </summary>
     4         /// <returns></returns>
     5         public static Document CreateDocument()
     6         {
     7             object missing = System.Reflection.Missing.Value;
     8             Document newdoc = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
     9             return newdoc;
    10         }
    public static void Main(string[] args)
            {
               var filePath = @"E:1	est.docx";
                CovertWordView.CovertWord(filePath);
                Console.WriteLine("自动备案成功");
    
            }
  • 相关阅读:
    冒泡排序
    Windows 10家庭版升级专业版
    VRRP + MSTP实验
    MSTP多生成树协议
    解决office 2016提示“你的许可证不是正版,并且你可能是盗版软件的受害者。使用正版Office,避免干扰并保护你的文件安全”
    路由器开启ssh实现远程管理
    CentOS 7安装Telnet服务进行远程管理
    CentOS 7开启ssh服务进行远程管理
    华为特有接口Hybrid
    Vlan Mapping
  • 原文地址:https://www.cnblogs.com/longshanshan/p/10735613.html
Copyright © 2011-2022 走看看