C# Office开发word导出不完善问题
2013-02-21 17:30
288 查看
项目中word存在横向纵向不同方式排版,在代码复制到新word的时候出现导出不完善问题解决方法如下:
如上28-30代码块为该问题解决的关键,原因是横向之后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分为了多节点,只需将每个节点内容都进行复制即可解决
相关文章推荐
- C#操作office组件(excel、word、ppt)报错问题!
- C#导出数据到word相关问题汇总
- C# office控件开发 如何使word中书签不被删除,且一直保持[]输入状态,非|状态
- C# office控件 开发环境的导出
- C# 操作office(word、excel)com异常问题
- C#操作Excel,调用ApplicationClass.Quit()关闭Excel时,发生异常:Microsoft Office Word 遇到问题需要关闭
- 使用freemarker导出word,wps可以打开,office打不开问题
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- C#导出数据到word相关问题汇总
- 关于 .net 开发 Office Word 的一些问题小结
- C# office 控件编程 由于word中排版造成word复制【含书签】容易遇到的问题 .docx
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- asp.net(C#)中将DataTable导出Execl、Word、Xml
- C# 导出word 表格代码
- C# 开发 Win 7下应用程序所需的管理员身份运行问题
- c#中做在线预览时没有在利用using时发现没有Microsoft.Office.Interop.Word;这个引用解决方案
- office 插件开发 -- 64位系统注册表问题
- C# WEB开发的几个问题的解决
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 能支撑10万以上客户端的数据同步下载问题
- 提示Microsoft office word 遇到问题需要关闭。还问是否发送错误报告。