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

参考1:

  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. Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
  7. Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
  8. oWord.Visible = true;
  9. object fileName = this.textBox1.Text;
  10. doc = oWord.Documents.Open(ref fileName,
  11. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  12. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  13. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  14. //---------------------------------------------------------------------------------------------------------------------
  15. oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel2;
  16. oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel3;
  17. oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText;
  18. object x = 0;
  19. Range myRange = doc.Range(ref x, ref x);
  20. Object oUpperHeadingLevel = "1";
  21. Object oLowerHeadingLevel = "3";
  22. Object oTOCTableID = "TableOfContents";
  23. doc.TablesOfContents.Add(myRange, ref oTrue, ref oUpperHeadingLevel,
  24. ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue,
  25. ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);
  26. //---------------------------------------------------------------------------------------------------------------------
  27. //Object oSaveAsFile = fileName;
  28. //doc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
  29. //    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  30. //    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  31. //    ref oMissing, ref oMissing);
  32. }
  33. private void button2_Click(object sender, EventArgs e)
  34. {
  35. OpenFileDialog fd = new OpenFileDialog();
  36. if (fd.ShowDialog() == DialogResult.OK)
  37. {
  38. this.textBox1.Text = fd.FileName;
  39. }
  40. }

参考2:

  1. void AddContent(ref   Word.Appliction app)
  2. {
  3. app.Selection.Start=0;
  4. app.Selection.End=0;//将光标移动到文档开始位置
  5. object beginLevel=2;//目录开始深度
  6. object endLevel=2;//目录结束深度
  7. object rightAlignPageNumber=true;// 指定页码右对其
  8. app.ActiveDocument.TablesOfContents.Add(app.Selection.Range,ref miss,rightAlignPageNumber,ref miss,
  9. ref miss,ref miss,ref miss,ref miss);//写入目录

C#操作Word生成目录的更多相关文章

  1. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  2. VBA操作word生成sql语句

    项目开始一般都是用word保存下数据库的文档 但是从表单一个一个的建表实在是很困难乏味,查查资料 1.可以生成一个html或者xml,检索结构生成sql.但是这个方式也蛮麻烦 2.查到vba可以操作w ...

  3. DocX操作word生成报表

    1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...

  4. word生成目录的pdf

    在很多情况下,需要将Word转换为带目录书签的PDF,方便pdf阅读,所以可以使用word自带的pdf转换,在转换时设置相关即可 注意:待转换Word中应该有目录,可以用Word中的标题来自动生成目录 ...

  5. word 生成目录

    生成目录: (1)Ctrl+End,到达文档的最后一页: (2)"插入"菜单--引用--索引和目录(此时出现索引和目录对话框): (3)单击"目录"选项卡 a. ...

  6. [转载]java操作word生成水印

    应用场景 为了保护版权或辨别文件的真伪,有时需要在生成的Word文件中动态添加水印,PageOffice组件的WaterMark类就封装了给在线编辑的Word文件添加水印这一功能,调用接口非常简单. ...

  7. [原创]java操作word生成水印

    应用场景 为了保护版权或辨别文件的真伪,有时需要在生成的Word文件中动态添加水印,PageOffice组件的WaterMark类就封装了给在线编辑的Word文件添加水印这一功能,调用接口非常简单. ...

  8. c# 操作Word总结(车)

    在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中,在显示的时,可以在线打开 ...

  9. c# 操作Word总结【转】

    http://www.cnblogs.com/eye-like/p/4121219.html 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很 ...

随机推荐

  1. 基于Web的数据推送技术(转)

    基于Web的数据推送技术 对于实时性数据显示要求比较高的系统,比如竞价,股票行情,实时聊天等,我们的解决方案有以下几种.1. HTTP请求发送模式,一般可以基于ajax的请求,比如每3秒一次访问下服务 ...

  2. hdu 1527 取石子游戏(Wythoff Game)

    题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...

  3. 进入appstore中指定的应用

    1.进入appstore中指定的应用 NSString *str = [NSString stringWithFormat:                           @"itms ...

  4. php 高效分页

    mysql.php 获取数据库中的记录,完全个人经验总结,仅供参考!<?php/***PHP+MYSQL数据库基本功能*http://blog.csdn.net/yown*/########## ...

  5. windows下跑python flask,环境配置

    首先声明一下,我安装的是python 2.7. 第一步:下载easy_setup.py 下载地址:https://pypi.python.org/pypi/setuptools 这个下载地址真心难找, ...

  6. c语言字符串大小写字母转换

    输入一个以#结束的字符串,本题要求将小写字母全部转换成大写字母,把大写字母全部转换成小写字母,其它字符不变. 输入格式: 输入在一行中给出一个长度不超过40的.以#结束的非空字符串. 输出格式: 在一 ...

  7. Kali vmtools

    参考链接:http://www.leavesongs.com/SHARE/kali-vmtools-install.html   安装环境:kali-1.1.0 ---3.18.0-kali1-amd ...

  8. linux3.0.4编译LDD中的scull全过程

    按照惯例,我是应该先写一些本章的收获的,不过太晚了. 在看完第三章之后开始编译,错误一堆,几乎崩溃,幸亏经过不断的百度,总算解决了问题,我发现 我遇到问题比较多,算是集中七个龙珠了吧,感谢先行的大神们 ...

  9. 第三百三十六天 how can I 坚持

    家里断网了,忘交网费了,连的手机网络,也挺好,吃完饭就可以睡觉了. 不知道怎的,昨天和家人聊天,一提对象的事就很容易着急生气,然后就会后悔..哎,这脾气得改. 确实不知道自己的另一半是啥样,想象不出来 ...

  10. Flex布局如何让子类在超出边界时隐藏掉

    在flex4中,因为必须添加<s:Scroller>标签才能出现滚动条,如果一个容器例如Panel没有添加滚动条,那么添加到Panel中的child的位置如果超出了Panel的边界,那么这 ...