zoukankan      html  css  js  c++  java
  • 合并两个WORD文档,并且修改Word中标签的内容

    因工作需要,需要对两个word 文档进行合并
     Word.Application objApp=new Word.ApplicationClass();
                
    object confirmConversion = false;
                
    object link = false;
                
    object attachment = false;
                
    object objMissing = Missing.Value;
                
    object isVisible = true;
                
    object objFalse = false;  
                
    try
                
    {
                   
                    Word.Document objDoc 
    = null;
                    objDoc 
    = objApp.Documents.Open(
                     
    ref objOrgDoc,    //FileName
                     ref objMissing,   //ConfirmVersions
                     ref objMissing,   //ReadOnly
                     ref objMissing,   //AddToRecentFiles
                     ref objMissing,   //PasswordDocument
                     ref objMissing,   //PasswordTemplate
                     ref objMissing,   //Revert
                     ref objMissing,   //WritePasswordDocument
                     ref objMissing,   //WritePasswordTemplate
                     ref objMissing,   //Format
                     ref objMissing,   //Enconding
                     ref objMissing,   //Visible
                     ref objMissing,   //OpenAndRepair
                     ref objMissing,   //DocumentDirection
                     ref objMissing,   //NoEncodingDialog
                     ref objMissing    //XMLTransform
                     );
                   
                    
    if (objDoc.Bookmarks.Exists((string)bm_ReceiveOrderNo))
                        ReplaceBookMark(objDoc, bm_ReceiveOrderNo, receiveOrderNo, 
    32);
                    
    if (objDoc.Bookmarks.Exists((string)bm_CertNo))
                        ReplaceBookMark(objDoc, bm_CertNo, certNo, 
    32);
                    
    if (objDoc.Bookmarks.Exists((string)bm_VerifiedUnit))
                        ReplaceBookMark(objDoc, bm_VerifiedUnit, verifiedUnit, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_Name))
                        ReplaceBookMark(objDoc, bm_Name, name, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_Specification))
                        ReplaceBookMark(objDoc, bm_Specification, specification, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_Rank))
                        ReplaceBookMark(objDoc, bm_Rank, rank, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_ProductionCode))
                        ReplaceBookMark(objDoc, bm_ProductionCode, productionCode, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_ProductionUnit))
                        ReplaceBookMark(objDoc, bm_ProductionUnit, producteUnit, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_ReasonBase))
                        ReplaceBookMark(objDoc, bm_ReasonBase, reasonBase, 
    55);
                    
    if (objDoc.Bookmarks.Exists((string)bm_Conclusion))
                        ReplaceBookMark(objDoc, bm_Conclusion, conclusion, 
    55);
                  
                    
    if (objDoc.Bookmarks.Exists((string)bm_VerifyDate))
                        objDoc.Bookmarks.get_Item(
    ref bm_VerifyDate).Range.Text = verifyDate;
                    
    if (objDoc.Bookmarks.Exists((string)bm_ExpireDate))
                        objDoc.Bookmarks.get_Item(
    ref bm_ExpireDate).Range.Text = expireDate;

                    Range r 
    = objDoc.Range(ref objMissing,ref objMissing);
                    
    object direction = Word.WdCollapseDirection.wdCollapseEnd;
                    r.Collapse(
    ref direction);
                    
    object objBreak = WdBreakType.wdSectionBreakNextPage;
                    r.InsertBreak(
    ref objBreak);
                    r.InsertFile(
    this.txtFilePath.Text, ref objMissing, ref confirmConversion, ref link, ref attachment);
                   
                    
    object objOutDoc = path;
                    objDoc.SaveAs(
    ref objOutDoc,
                              
    ref objMissing,     //FileFormat
                              ref objMissing,     //LockComments
                              ref objMissing,     //PassWord     
                              ref objMissing,     //AddToRecentFiles
                              ref objMissing,     //WritePassword
                              ref objMissing,     //ReadOnlyRecommended
                              ref objMissing,     //EmbedTrueTypeFonts
                              ref objMissing,     //SaveNativePictureFormat
                              ref objMissing,     //SaveFormsData
                              ref objMissing,     //SaveAsAOCELetter,
                              ref objMissing,     //Encoding
                              ref objMissing,     //InsertLineBreaks
                              ref objMissing,     //AllowSubstitutions
                              ref objMissing,     //LineEnding
                              ref objMissing      //AddBiDiMarks
                              );
                    
    return path;
                }

               
    finally
                
    {
                    objApp.Quit(
    ref objFalse,
                     
    ref objMissing,
                     
    ref objMissing
                     );
                    objApp 
    = null;
                }
     private void ReplaceBookMark(Document doc, object bookmarkName, string text, int CharLength)
            
    {
                Range rng 
    = doc.Bookmarks.get_Item(ref bookmarkName).Range;
                rng.Text 
    = text;
                
    int count = System.Text.Encoding.GetEncoding("gb2312").GetBytes(text).Length;
               
                
    if (CharLength > count)
                
    {
                    
    int startAddedSpaceCount = (CharLength - count) / 2;
                    
    int endAddedSpaceCount = (CharLength - count) / 2 + (CharLength - count) % 2;
                    rng.InsertBefore(GetSpaceString(startAddedSpaceCount));
                    rng.InsertAfter(GetSpaceString(endAddedSpaceCount));
                    rng.Font.Underline 
    = WdUnderline.wdUnderlineSingle;
                 
                }
     
            }


            
    private string GetSpaceString(int StringLength)
            
    {
                StringBuilder sb 
    = new StringBuilder();
                
    for(int i=0;i<StringLength;i++)
                
    {
                    sb.Append(
    " ");
                }

                
    return sb.ToString();
            }
  • 相关阅读:
    个人期末总结
    团队冲刺第二阶段10
    团队冲刺第二阶段9
    团队冲刺第二阶段8
    团队冲刺第二阶段7
    团队冲刺第二阶段6
    数据分析之例题
    数据分析之数据操作
    数据分析之Matplotlib可视化
    数据分析之Pandas
  • 原文地址:https://www.cnblogs.com/nosnowwolf/p/1170951.html
Copyright © 2011-2022 走看看