zoukankan      html  css  js  c++  java
  • C#操作Word生成目录

    C#代码  收藏代码
    1. OperateWord ow = new OperateWord();  
    2. Microsoft.Office.Interop.Word.ApplicationClass ss = ow.WordApplication;  
    3.             AddContent(ref ss);  
    4.               
    5.  void AddContent(ref   Microsoft.Office.Interop.Word.ApplicationClass app)  
    6.         {  
    7.   
    8.             Object oMissing = System.Reflection.Missing.Value;  
    9.             Object oTrue = true;  
    10.             Object oFalse = false;  
    11.             Object oUpperHeadingLevel = "1";  
    12.             Object oLowerHeadingLevel = "3";  
    13.             Object oTOCTableID = "TableOfContents";  
    14.   
    15.             app.Selection.Start = 0;  
    16.   
    17.             app.Selection.End = 0;//将光标移动到文档开始位置  
    18.   
    19.             object beginLevel = 2;//目录开始深度  
    20.   
    21.             object endLevel = 2;//目录结束深度  
    22.   
    23.             object rightAlignPageNumber = true;// 指定页码右对其  
    24.   
    25.             /* 
    26.              * Range 
    27.              * UserHeadingStyles 使用heading风格 
    28.              * UpperHeadingLevel 增加heading级别 
    29.              * LowerHeadingLevel 减小heading级别 
    30.              * UserFields 使用fields 
    31.              * Tableid tableid 
    32.              * RightAlignPageNumbers 右对齐页数 
    33.              * IncludePageNumbers 包含页数 
    34.              * Addedstyles 添加风格 
    35.              * UserHyperlinks 使用超链接 
    36.              * HidePageNumbersInweb 隐藏页数 
    37.              * UseOutLineLevels 使用提纲级别 
    38.              * TableOfContents 内容表 
    39.              */  
    40.             app.Application.ActiveDocument.TablesOfContents.Add(app.Selection.Range, ref oTrue, ref oUpperHeadingLevel,  
    41.                 ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue,  
    42.                 ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);//添加目录  
    43.   
    44.             //写入目录  
    45.         }  

     参考1:

    C#代码  收藏代码
    1. private void button1_Click(object sender, EventArgs e)  
    2.         {   
    3.             Object oMissing = System.Reflection.Missing.Value;  
    4.             Object oTrue = true;  
    5.             Object oFalse = false;  
    6.                
    7.             Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();  
    8.             Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();   
    9.   
    10.             oWord.Visible = true;  
    11.             object fileName = this.textBox1.Text;  
    12.             doc = oWord.Documents.Open(ref fileName,  
    13.             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
    14.             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
    15.             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
    16.   
    17.             //---------------------------------------------------------------------------------------------------------------------  
    18.             oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel2;  
    19.             oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel3;  
    20.             oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText;  
    21.   
    22.             object x = 0;  
    23.             Range myRange = doc.Range(ref x, ref x);    
    24.             Object oUpperHeadingLevel = "1";  
    25.             Object oLowerHeadingLevel = "3";  
    26.             Object oTOCTableID = "TableOfContents";  
    27.             doc.TablesOfContents.Add(myRange, ref oTrue, ref oUpperHeadingLevel,  
    28.                 ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue,  
    29.                 ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);  
    30.             //---------------------------------------------------------------------------------------------------------------------  
    31.             //Object oSaveAsFile = fileName;  
    32.             //doc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,  
    33.             //    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
    34.             //    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
    35.             //    ref oMissing, ref oMissing);  
    36.         }  
    37.   
    38.         private void button2_Click(object sender, EventArgs e)  
    39.         {  
    40.             OpenFileDialog fd = new OpenFileDialog();  
    41.             if (fd.ShowDialog() == DialogResult.OK)  
    42.             {  
    43.                 this.textBox1.Text = fd.FileName;  
    44.             }  
    45.         }  

    参考2:

    C#代码  收藏代码
    1. void AddContent(ref   Word.Appliction app)  
    2.   
    3. {  
    4.   
    5.         app.Selection.Start=0;  
    6.   
    7.         app.Selection.End=0;//将光标移动到文档开始位置  
    8.   
    9.        object beginLevel=2;//目录开始深度  
    10.   
    11.        object endLevel=2;//目录结束深度  
    12.   
    13.        object rightAlignPageNumber=true;// 指定页码右对其  
    14.   
    15.       app.ActiveDocument.TablesOfContents.Add(app.Selection.Range,ref miss,rightAlignPageNumber,ref miss,  
    16.   
    17.                                                ref miss,ref miss,ref miss,ref miss);//写入目录  
  • 相关阅读:
    vue + ajax + php 接口的使用小接
    网页调用qq聊天
    基于touch.js 左滑删除功能
    touch.js——常见应用操作
    常用链接
    如何判断滚动条已到达底部
    前端如何优雅的选择字体
    纯css3打造瀑布流布局
    移动端软键盘监听(弹出,收起),及影响定位布局的问题
    jq获取图片的原始尺寸,自适应布局
  • 原文地址:https://www.cnblogs.com/gc2013/p/3824593.html
Copyright © 2011-2022 走看看