C# 给word文档添加目录

yqsg1217 yqsg1217 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public void mulv() {
Object oMissing = System.Reflection.Missing.Value;
Object oTrue = true;
Object oFalse = false;

Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

object fileName = Server.MapPath("../Words/test.doc");
doc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel2;
oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel3;
oWord.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText;

object x = 0;
Range myRange = doc.Range(ref x, ref x);
Object oUpperHeadingLevel = "1";
Object oLowerHeadingLevel = "3";
Object oTOCTableID = "TableOfContents";
doc.TablesOfContents.Add(myRange, ref oTrue, ref oUpperHeadingLevel,
ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue,
ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);

oWord.ActiveDocument.TablesOfContents[1].TabLeader = WdTabLeader.wdTabLeaderDots;
oWord.ActiveDocument.TablesOfContents.Format = 0;

doc.Close(ref oMissing, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
这是我的代码,生成之后第二遍之后,目录会重复添加,不能覆盖,并且子目录不能与根目录对齐!谢谢各位大虾的帮助!
...全文
评论 5
收藏 已收藏
分享
返回顶部
yqsg1217
C# 111,129 成员 642,538 内容