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("自动备案成功");
    
            }
  • 相关阅读:
    vue 采坑 Invalid default value for prop "slideItems": Props with type Object/Array must use a factory function to return the default value.
    vue-cli3 按需引入echarts
    vue-cli3 按需引入外部elment-ui UI插件
    vue-cli3 引入less全局变量
    css 文本溢出截断省略方案
    canvas画圆角头像
    css 加载效果
    css实例气泡效果
    css居中-水平居中,垂直居中,上下左右居中
    meta标签
  • 原文地址:https://www.cnblogs.com/longshanshan/p/10735613.html
Copyright © 2011-2022 走看看