大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#操作Word生成目录

C#操作Word生成目录

关键词:Word生成目录WordC#  阅读(1218) 赞(11)

[摘要]本文是对C#操作Word生成目录的讲解,对学习C#编程技术有所帮助,与大家分享。
C#代码
  1. OperateWordow=newOperateWord();
  2. Microsoft.Office.Interop.Word.ApplicationClassss=ow.WordApplication;
  3. AddContent(refss);
  4. voidAddContent(refMicrosoft.Office.Interop.Word.ApplicationClassapp)
  5. {
  6. ObjectoMissing=System.Reflection.Missing.Value;
  7. ObjectoTrue=true;
  8. ObjectoFalse=false;
  9. ObjectoUpperHeadingLevel="1";
  10. ObjectoLowerHeadingLevel="3";
  11. ObjectoTOCTableID="TableOfContents";
  12. app.Selection.Start=0;
  13. app.Selection.End=0;//将光标移动到文档开始位置
  14. objectbeginLevel=2;//目录开始深度
  15. objectendLevel=2;//目录结束深度
  16. objectrightAlignPageNumber=true;//指定页码右对其
  17. /*
  18. *Range
  19. *UserHeadingStyles使用heading风格
  20. *UpperHeadingLevel增加heading级别
  21. *LowerHeadingLevel减小heading级别
  22. *UserFields使用fields
  23. *Tableidtableid
  24. *RightAlignPageNumbers右对齐页数
  25. *IncludePageNumbers包含页数
  26. *Addedstyles添加风格
  27. *UserHyperlinks使用超链接
  28. *HidePageNumbersInweb隐藏页数
  29. *UseOutLineLevels使用提纲级别
  30. *TableOfContents内容表
  31. */
  32. app.Application.ActiveDocument.TablesOfContents.Add(app.Selection.Range,refoTrue,refoUpperHeadingLevel,
  33. refoLowerHeadingLevel,refoMissing,refoTOCTableID,refoTrue,
  34. refoTrue,refoMissing,refoTrue,refoTrue,refoTrue);//添加目录
  35. //写入目录
  36. }

参考1:

C#代码
  1. privatevoidbutton1_Click(objectsender,EventArgse)
  2. {
  3. ObjectoMissing=System.Reflection.Missing.Value;
  4. ObjectoTrue=true;
  5. ObjectoFalse=false;
  6. Microsoft.Office.Interop.Word.ApplicationoWord=newMicrosoft.Office.Interop.Word.Application();
  7. Microsoft.Office.Interop.Word.Documentdoc=newMicrosoft.Office.Interop.Word.Document();
  8. oWord.Visible=true;
  9. objectfileName=this.textBox1.Text;
  10. doc=oWord.Documents.Open(reffileName,
  11. refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
  12. refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
  13. refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);
  14. //---------------------------------------------------------------------------------------------------------------------
  15. oWord.Selection.Paragraphs.OutlineLevel=WdOutlineLevel.wdOutlineLevel2;
  16. oWord.Selection.Paragraphs.OutlineLevel=WdOutlineLevel.wdOutlineLevel3;
  17. oWord.Selection.Paragraphs.OutlineLevel=WdOutlineLevel.wdOutlineLevelBodyText;
  18. objectx=0;
  19. RangemyRange=doc.Range(refx,refx);
  20. ObjectoUpperHeadingLevel="1";
  21. ObjectoLowerHeadingLevel="3";
  22. ObjectoTOCTableID="TableOfContents";
  23. doc.TablesOfContents.Add(myRange,refoTrue,refoUpperHeadingLevel,
  24. refoLowerHeadingLevel,refoMissing,refoTOCTableID,refoTrue,
  25. refoTrue,refoMissing,refoTrue,refoTrue,refoTrue);
  26. //---------------------------------------------------------------------------------------------------------------------
  27. //ObjectoSaveAsFile=fileName;
  28. //doc.SaveAs(refoSaveAsFile,refoMissing,refoMissing,refoMissing,
  29. //refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
  30. //refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
  31. //refoMissing,refoMissing);
  32. }
  33. privatevoidbutton2_Click(objectsender,EventArgse)
  34. {
  35. OpenFileDialogfd=newOpenFileDialog();
  36. if(fd.ShowDialog()==DialogResult.OK)
  37. {
  38. this.textBox1.Text=fd.FileName;
  39. }
  40. }

参考2:

C#代码
  1. voidAddContent(refWord.Applictionapp)
  2. {
  3. app.Selection.Start=0;
  4. app.Selection.End=0;//将光标移动到文档开始位置
  5. objectbeginLevel=2;//目录开始深度
  6. objectendLevel=2;//目录结束深度
  7. objectrightAlignPageNumber=true;//指定页码右对其
  8. app.ActiveDocument.TablesOfContents.Add(app.Selection.Range,refmiss,rightAlignPageNumber,refmiss,
  9. refmiss,refmiss,refmiss,refmiss);//写入目录


相关评论