C# Office开发word导出不完善问题

项目中word存在横向纵向不同方式排版,在代码复制到新word的时候出现导出不完善问题解决方法如下:

 /// <summary>
        /// 生成doc方法【带目录+加密+利用分隔符将目录单属一页】
        /// </summary>
        public void copyWord()
        {
            //1.创建新doc
            Word.Application newapp = new Word.Application();
            Word.Document newdoc;
            object nothing = System.Reflection.Missing.Value;//用于作为函数的默认参数
            newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一个word文档
            object Password = "123456";

            //2.解除限制编辑
            /*
            if (newapp.ActiveDocument.ProtectionType == Word.WdProtectionType.wdAllowOnlyComments)
            {
                newapp.ActiveDocument.Unprotect(ref Password);
            }
            */
            //3.word可视化
            newapp.Visible = true;
            //4.复制旧doc
            this.Select();
            //5.黏贴到新doc
            Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
            int SectionSize = this.Sections.Count;
            for (int i = 1; i <= SectionSize;i++ )
            {
                this.Sections[i].Range.Copy();
                newdoc.Sections[i].Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
            }
            //6.初始化要操作的doc路径以及相应属性
            object docFileName = newdoc.Path;
            object missing = System.Reflection.Missing.Value;

            //7.打开doc文件
            newapp.Documents.Open(ref docFileName,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing);
            newapp.Visible = true;//显示文件
            object bkObj = "DictionaryContent";
            if (newapp.ActiveDocument.Bookmarks.Exists("DictionaryContent"))//判断是否存在指定位置的书签
            {
                newapp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();

                //8.定义目录样式
                newapp.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel2;
                newapp.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevel3;
                newapp.Selection.Paragraphs.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText;
                //object x = 0;
                Range myRange = newapp.ActiveDocument.Bookmarks["DictionaryContent"].Range;//关键的一步:定位目录位置
                //Range myRange =this.Application.Documents[1].Sections[2].Range;
                Object oUpperHeadingLevel = "1";
                Object oLowerHeadingLevel = "5";
                Object oTOCTableID = "content_1";//目录名称
                Object oTrue = true;
                Object oFalse = false;
                Object oMissing = System.Reflection.Missing.Value;
                //9.目录开始部分插入分页符
                object oPageBreak = Word.WdBreakType.wdSectionBreakNextPage;
                newapp.ActiveDocument.Bookmarks["DictionaryContent"].Range.Words.Last.InsertBreak(ref oPageBreak);//插入分页符 目录首位置
                //10.目录填充到doc
                newdoc.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);
               
               //11.目录结尾部分插入分页符
                newapp.ActiveDocument.Bookmarks["DictionaryContent2"].Range.Words.Last.InsertBreak(ref oPageBreak);//插入分页符 目录末尾位置
            }


            //12.如果没有限制编辑为文档进行编辑限制
            if (newapp.ActiveDocument.ProtectionType == Word.WdProtectionType.wdNoProtection)
            {
                newapp.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyComments, ref missing, ref Password);
            }
            //13.关闭并保存旧doc
            this.Close(ref saveChanges, ref nothing, ref nothing);//关闭旧doc并保存
            this.Application.Quit(ref saveChanges, ref nothing, ref nothing);
        }


 

 

如上28-30代码块为该问题解决的关键,原因是横向之后word分为了多节点,只需将每个节点内容都进行复制即可解决
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值