您的位置:首页 > 编程语言 > C#

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

2013-02-21 17:30 288 查看
项目中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分为了多节点,只需将每个节点内容都进行复制即可解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: