word 操作类实现拆分word

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using Microsoft.Office.Interop.Word;  
  5. using Microsoft.VisualBasic;  
  6. using System.IO;  
  7.   
  8. namespace Ks5uCms.Web2.Library.core  
  9. {  
  10.     public class CCWordApp  
  11.     {  
  12.         private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic; //   a   reference   to   Word   application   
  13.         private Microsoft.Office.Interop.Word.Document oDoc; //   a   reference   to   the   document   
  14.         private object missing = System.Reflection.Missing.Value;  
  15.         public CCWordApp()  
  16.         {  
  17.             //   
  18.             //   TODO:   在此处添加构造函数逻辑   
  19.             //   
  20.             oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();  
  21.         }  
  22.         //   Open   a   file   (the   file   must   exists)   and   activate   it   
  23.         public void Open(string strFileName)  
  24.         {  
  25.             object fileName = strFileName;  
  26.             object readOnly = false;  
  27.             object isVisible = true;  
  28.   
  29.   
  30.             oDoc = oWordApplic.Documents.Open(ref   fileName, ref   missing, ref   readOnly,  
  31.             ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing,  
  32.             ref   missing, ref   missing, ref   isVisible, ref   missing, ref   missing, ref   missing, ref   missing);  
  33.   
  34.             oDoc.Activate();  
  35.         }  
  36.         /// <summary>  
  37.         /// Open   a   new   with   template   
  38.         /// </summary>  
  39.         /// <param name="strFileName"></param>  
  40.         public void OpenWithTemplate(string strFileName)  
  41.         {  
  42.             object fileName = strFileName;  
  43.   
  44.             oDoc = oWordApplic.Documents.Add(ref   fileName, ref   missing, ref   missing, ref   missing);  
  45.   
  46.             oDoc.Activate();  
  47.         }  
  48.         /// <summary>  
  49.         ///  Open   a   new   document   
  50.         /// </summary>  
  51.     
  52.         public void Open()  
  53.         {  
  54.   
  55.             oDoc = oWordApplic.Documents.Add(ref   missing, ref   missing, ref   missing, ref   missing);  
  56.   
  57.             oDoc.Activate();  
  58.         }  
  59.   
  60.         /// <summary>  
  61.         /// 释放  
  62.         /// </summary>  
  63.         public void Quit()  
  64.         {  
  65.             oDoc.Close(ref   missing, ref   missing, ref   missing);  
  66.             oWordApplic.Quit(ref   missing, ref   missing, ref   missing);  
  67.         }  
  68.   
  69.         public void Save()  
  70.         {  
  71.             oDoc.Save();  
  72.         }  
  73.   
  74.         /// <summary>  
  75.         /// 保存文件  
  76.         /// </summary>  
  77.         /// <param name="strFileName"></param>  
  78.         public void SaveAs(string strFileName)  
  79.         {  
  80.   
  81.             object fileName = strFileName;  
  82.   
  83.             oDoc.SaveAs(ref   fileName, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing,  
  84.             ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing);  
  85.         }  
  86.         /// <summary>  
  87.         /// 添加内容  
  88.         /// </summary>  
  89.         /// <param name="strText"></param>  
  90.         public void InsertText(string strText)  
  91.         {  
  92.             oWordApplic.Selection.TypeText(strText);  
  93.                
  94.         }  
  95.   
  96.   
  97.         /// <summary>  
  98.         /// 换行  
  99.         /// </summary>  
  100.         public void InsertLineBreak()  
  101.         {  
  102.             oWordApplic.Selection.TypeParagraph();  
  103.         }  
  104.         /// <summary>  
  105.         /// 换多行  
  106.         /// </summary>  
  107.         /// <param name="nline"></param>  
  108.         public void InsertLineBreak(int nline)  
  109.         {  
  110.             for (int i = 0; i < nline; i++)  
  111.                 oWordApplic.Selection.TypeParagraph();  
  112.         }  
  113.   
  114.         /// <summary>  
  115.         /// Change   the   paragraph   alignement   
  116.         /// 改变段对准  
  117.         /// </summary>  
  118.         /// <param name="strType"></param>  
  119.         public void SetAlignment(string strType)  
  120.         {  
  121.             switch (strType)  
  122.             {  
  123.                 case "Center ":  
  124.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  125.                     break;  
  126.                 case "Left ":  
  127.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;  
  128.                     break;  
  129.                 case "Right ":  
  130.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;  
  131.                     break;  
  132.                 case "Justify ":  
  133.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;  
  134.                     break;  
  135.             }  
  136.   
  137.         }  
  138.   
  139.   
  140.         //   if   you   use   thif   function   to   change   the   font   you   should   call   it   again   with     
  141.         //   no   parameter   in   order   to   set   the   font   without   a   particular   format   
  142.         /// <summary>  
  143.         /// 字形特别格式  
  144.         /// </summary>  
  145.         /// <param name="strType"></param>  
  146.   
  147.         public void SetFont(string strType)  
  148.         {  
  149.             switch (strType)  
  150.             {  
  151.                 case "Bold ":  
  152.                     oWordApplic.Selection.Font.Bold = 1;  
  153.                     break;  
  154.                 case "Italic ":  
  155.                     oWordApplic.Selection.Font.Italic = 1;  
  156.                     break;  
  157.                 case "Underlined ":  
  158.                     oWordApplic.Selection.Font.Subscript = 0;  
  159.                     break;  
  160.             }  
  161.   
  162.         }  
  163.   
  164.         /// <summary>  
  165.         /// disable   all   the   style     
  166.         /// 设置字形  
  167.         /// </summary>  
  168.         public void SetFont()  
  169.         {  
  170.             oWordApplic.Selection.Font.Bold = 0;  
  171.             oWordApplic.Selection.Font.Italic = 0;  
  172.             oWordApplic.Selection.Font.Subscript = 0;  
  173.   
  174.         }  
  175.         /// <summary>  
  176.         /// 设置字体  
  177.         /// </summary>  
  178.         /// <param name="strType"></param>  
  179.         public void SetFontName(string strType)  
  180.         {  
  181.             //VB:Selection.Font.Name   =   "黑体 "   
  182.             oWordApplic.Selection.Font.Name = strType;  
  183.   
  184.         }  
  185.         /// <summary>  
  186.         /// 设置字体大小  
  187.         /// </summary>  
  188.         /// <param name="nSize"></param>  
  189.         public void SetFontSize(int nSize)  
  190.         {  
  191.             //VB:Selection.Font.Size   =   22   
  192.             oWordApplic.Selection.Font.Size = nSize;  
  193.   
  194.         }  
  195.   
  196.         public void PasteAndFormat()  
  197.         {  
  198.             //Selection.PasteAndFormat   (wdPasteDefault)   
  199.   
  200.             oWordApplic.Selection.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdPasteDefault);  
  201.         }  
  202.         /// <summary>  
  203.         /// 插入页  
  204.         /// </summary>  
  205.         public void InsertPagebreak()  
  206.         {  
  207.             //   VB   :   Selection.InsertBreak   Type:=wdPageBreak   
  208.             object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;  
  209.             oWordApplic.Selection.InsertBreak(ref   pBreak);  
  210.         }  
  211.         public void InsertBackspace()  
  212.         {  
  213.             //   VB   :   Selection.TypeBackspace   
  214.   
  215.             oWordApplic.Selection.TypeBackspace();  
  216.         }  
  217.   
  218.         /// <summary>  
  219.         /// 插入书签  
  220.         /// </summary>  
  221.         /// <param name="BookMark"></param>  
  222.         public void InsertBookMark(string BookMark)  
  223.         {  
  224.             /*   VB   :   With   ActiveDocument.Bookmarks  
  225.             .Add   Range:=Selection.Range,   Name:= "dq "  
  226.             .DefaultSorting   =   wdSortByName  
  227.             .ShowHidden   =   False  
  228.               End   With*/  
  229.             object Range = oWordApplic.Selection.Range;  
  230.             oWordApplic.ActiveDocument.Bookmarks.Add(BookMark, ref   Range);  
  231.         }  
  232.         /// <summary>  
  233.         /// 插入图片  
  234.         /// </summary>  
  235.         /// <param name="FileName"></param>  
  236.         public void InsertPicture(string FileName)  
  237.         {  
  238.             /*   VB   :   Selection.InlineShapes.AddPicture   FileName:=zp,   LinkToFile:=False,   SaveWithDocument:=True*/  
  239.   
  240.             object LinkToFile = false;  
  241.             object SaveWithDocument = true;  
  242.             oWordApplic.Selection.InlineShapes.AddPicture(FileName, ref   LinkToFile, ref   SaveWithDocument, ref   missing);  
  243.         }  
  244.   
  245.         //   Go   to   a   predefined   bookmark,   if   the   bookmark   doesn 't   exists   the   application   will   raise   an   error   
  246.   
  247.         public void GotoBookMark(string strBookMarkName)  
  248.         {  
  249.             //   VB   :     Selection.GoTo   What:=wdGoToBookmark,   Name:= "nome "   
  250.   
  251.   
  252.             object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;  
  253.             object NameBookMark = strBookMarkName;  
  254.             oWordApplic.Selection.GoTo(ref   Bookmark, ref   missing, ref   missing, ref   NameBookMark);  
  255.         }  
  256.   
  257.         public void GoToTheEnd()  
  258.         {  
  259.             //   VB   :     Selection.EndKey   Unit:=wdStory   
  260.   
  261.             object unit;  
  262.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  263.             oWordApplic.Selection.EndKey(ref   unit, ref   missing);  
  264.   
  265.         }  
  266.         public void GoToTheBeginning()  
  267.         {  
  268.             //   VB   :   Selection.HomeKey   Unit:=wdStory   
  269.   
  270.             object unit;  
  271.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  272.             oWordApplic.Selection.HomeKey(ref   unit, ref   missing);  
  273.   
  274.         }  
  275.   
  276.         public void GoToTheTable(int ntable)  
  277.         {  
  278.             // Selection.GoTo   What:=wdGoToTable,   Which:=wdGoToFirst,   Count:=1,   Name:= " "   
  279.             //         Selection.Find.ClearFormatting   
  280.             //         With   Selection.Find   
  281.             //                 .Text   =   " "   
  282.             //                 .Replacement.Text   =   " "   
  283.             //                 .Forward   =   True   
  284.             //                 .Wrap   =   wdFindContinue   
  285.             //                 .Format   =   False   
  286.             //                 .MatchCase   =   False   
  287.             //                 .MatchWholeWord   =   False   
  288.             //                 .MatchWildcards   =   False   
  289.             //                 .MatchSoundsLike   =   False   
  290.             //                 .MatchAllWordForms   =   False   
  291.             //         End   With   
  292.   
  293.   
  294.             object what;  
  295.             what = Microsoft.Office.Interop.Word.WdUnits.wdTable;  
  296.             object which;  
  297.             which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;  
  298.             object count;  
  299.             count = 1;  
  300.             oWordApplic.Selection.GoTo(ref   what, ref   which, ref   count, ref   missing);  
  301.             oWordApplic.Selection.Find.ClearFormatting();  
  302.   
  303.             oWordApplic.Selection.Text = " ";  
  304.   
  305.   
  306.         }  
  307.   
  308.         public void GoToRightCharacter(int Count)  
  309.         {  
  310.             //   Selection.MoveRight   Unit:=wdCharacter   
  311.   
  312.   
  313.             object count = Count;  
  314.             object direction;  
  315.             direction = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;  
  316.             oWordApplic.Selection.MoveRight(ref   direction, ref   count, ref   missing);  
  317.         }  
  318.         public void GoToRightCharacterAndSelected(int Count, string Extend)  
  319.         {  
  320.             //   Selection.MoveDown   Unit:=wdLine,   Count:=32,   Extend:=wdExtend   
  321.   
  322.             object count = Count;  
  323.             object extend = Extend;  
  324.             object direction;  
  325.             direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  326.             oWordApplic.Selection.MoveRight(ref   direction, ref   count, ref   extend);  
  327.         }  
  328.   
  329.         public void CopyNewTable()  
  330.         {  
  331.             /*VB:   Selection.WholeStory  
  332.                         Selection.Copy*/  
  333.   
  334.             //object   Extend=Word.WdMovementType.wdExtend;   
  335.             //object   direction   =   Word.WdUnits.wdLine;   
  336.             //oWordApplic.Selection.MoveDown(ref   direction,ref   count,ref   Extend);   
  337.             oWordApplic.Selection.WholeStory();  
  338.             oWordApplic.Selection.Copy();  
  339.             GoToTheBeginning();  
  340.         }  
  341.   
  342.         public void GoToRightCell()  
  343.         {  
  344.             //   Selection.MoveRight   Unit:=wdCell   
  345.   
  346.   
  347.             object direction;  
  348.             direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;  
  349.             oWordApplic.Selection.MoveRight(ref   direction, ref   missing, ref   missing);  
  350.         }  
  351.   
  352.         public void GoToLeftCell()  
  353.         {  
  354.             //   Selection.MoveRight   Unit:=wdCell   
  355.   
  356.   
  357.             object direction;  
  358.             direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;  
  359.             oWordApplic.Selection.MoveLeft(ref   direction, ref   missing, ref   missing);  
  360.         }  
  361.   
  362.         public void GoToDownCell()  
  363.         {  
  364.             //   Selection.MoveRight   Unit:=wdCell   
  365.   
  366.   
  367.             object direction;  
  368.             direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  369.             oWordApplic.Selection.MoveDown(ref   direction, ref   missing, ref   missing);  
  370.         }  
  371.   
  372.         public void GoToUpCell()  
  373.         {  
  374.             //   Selection.MoveRight   Unit:=wdCell   
  375.   
  376.   
  377.             object direction;  
  378.             direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  379.             oWordApplic.Selection.MoveUp(ref   direction, ref   missing, ref   missing);  
  380.         }  
  381.         public void GoToUpCell(int Count)  
  382.         {  
  383.             //   Selection.MoveRight   Unit:=wdCell   
  384.             object count = Count;  
  385.   
  386.             object direction;  
  387.             direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  388.             oWordApplic.Selection.MoveUp(ref   direction, ref   count, ref   missing);  
  389.         }  
  390.   
  391.         //   this   function   doesn 't   work   
  392.         public void InsertPageNumber(string strType, bool bHeader)  
  393.         {  
  394.   
  395.             object alignment;  
  396.             object bFirstPage = false;  
  397.             object bF = true;  
  398.             //if   (bHeader   ==   true)   
  399.             //WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber   =   bF;   
  400.             switch (strType)  
  401.             {  
  402.                 case "Center ":  
  403.                     alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;  
  404.                     //WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref   alignment,ref   bFirstPage);   
  405.                     //Word.Selection   objSelection   =   WordApplic.pSelection;   
  406.   
  407.                     // oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberCenter;   
  408.                     break;  
  409.                 case "Right ":  
  410.                     alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;  
  411.                     // oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment   =   Word.WdPageNumberAlignment.wdAlignPageNumberRight;   
  412.                     break;  
  413.                 case "Left ":  
  414.                     alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberLeft;  
  415.                     oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref   alignment, ref   bFirstPage);  
  416.                     break;  
  417.             }  
  418.   
  419.         }  
  420.         ///<summary>  
  421.         /// 在word 中查找一个字符串直接替换所需要的文本  
  422.         /// </summary>  
  423.         /// <param name="strOldText">原文本</param>  
  424.         /// <param name="strNewText">新文本</param>  
  425.         /// <returns></returns>  
  426.         public bool Replace(string strOldText, string strNewText)  
  427.         {  
  428.             this.oWordApplic.Selection.Find.Text = strOldText;  
  429.             object FindText, ReplaceWith, Replace;//   
  430.             object MissingValue = Type.Missing;  
  431.             FindText = strOldText;//要查找的文本  
  432.             ReplaceWith = strNewText;//替换文本  
  433.             Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;/**//*wdReplaceAll - 替换找到的所有项。 
  434.                                                      * wdReplaceNone - 不替换找到的任何项。 
  435.                                                      * wdReplaceOne - 替换找到的第一项。 
  436.                                                      * */  
  437.             this.oWordApplic.Selection.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置  
  438.             if (this.oWordApplic.Selection.Find.Execute(  
  439.                 ref FindText, ref MissingValue,  
  440.                 ref MissingValue, ref MissingValue,  
  441.                 ref MissingValue, ref MissingValue,  
  442.                 ref MissingValue, ref MissingValue, ref MissingValue,  
  443.                 ref ReplaceWith, ref Replace,  
  444.                 ref MissingValue, ref MissingValue,  
  445.                 ref MissingValue, ref MissingValue))  
  446.             {  
  447.                 return true;  
  448.             }  
  449.             return false;  
  450.   
  451.         }  
  452.   
  453.          public bool SearchReplace(string strOldText, string strNewText)  
  454.         {  
  455.             object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;  
  456.              
  457.             //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。  
  458.             oWordApplic.Selection.Find.ClearFormatting();  
  459.             oWordApplic.Selection.Find.Text = strOldText;  
  460.   
  461.             oWordApplic.Selection.Find.Replacement.ClearFormatting();  
  462.             oWordApplic.Selection.Find.Replacement.Text = strNewText;  
  463.   
  464.             if (oWordApplic.Selection.Find.Execute(  
  465.                 ref missing, ref missing, ref missing, ref missing, ref missing,  
  466.                 ref missing, ref missing, ref missing, ref missing, ref missing,  
  467.                 ref replaceAll, ref missing, ref missing, ref missing, ref missing))  
  468.             {  
  469.                 return true;  
  470.             }  
  471.             return false;  
  472.         }  
  473.   
  474.         /// <summary>  
  475.         /// 获取书签的位置  
  476.         /// </summary>  
  477.         /// <param name="word">文档对象</param>  
  478.         /// <returns></returns>  
  479.         private int[,] GetPosition(Document word)  
  480.         {  
  481.             int bmcount = word.Bookmarks.Count;  
  482.             int[,] result = new int[bmcount, 2];  
  483.             for (int i = 1; i <= bmcount; i++)  
  484.             {  
  485.                 object index = i;  
  486.                 Bookmark bm = word.Bookmarks.get_Item(ref index);  
  487.                 if (bm.Name == "【结束】" + Convert.ToString(i + 1))  
  488.                 {  
  489.                     result[i, 0] = bm.Start;  
  490.                     result[i, 1] = bm.End;  
  491.                       
  492.                 }  
  493.             }  
  494.             return result;  
  495.         }  
  496.   
  497.   
  498.   
  499.          
  500.   
  501.         public void InsertParagraphBeforeByParagraph( Microsoft.Office.Interop.Word.Paragraph paragraph,int nLine )  
  502.         {  
  503.             for ( int i = 0; i < nLine; i++ )  
  504.                 paragraph.Range.InsertParagraphBefore();  
  505.         }  
  506.   
  507.         public void InsertParagraphAfterByParagraph( Microsoft.Office.Interop.Word.Paragraph paragraph )  
  508.         {  
  509.             paragraph.Range.InsertParagraphAfter();  
  510.         }  
  511.   
  512.         public void InsertParagraphAfterByParagraph( Microsoft.Office.Interop.Word.Paragraph paragraph, int nLine )  
  513.         {  
  514.             for ( int i = 0; i < nLine; i++ )  
  515.                 paragraph.Range.InsertParagraphAfter();  
  516.         }  
  517.   
  518.       
  519.         /**//// <summary>  
  520.         /// 段落的对齐方式  
  521.         /// 例如:word.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphCenter)  
  522.         /// </summary>  
  523.         /// <param name="alignment"></param>  
  524.         public void SetAlignment( Microsoft.Office.Interop.Word.WdParagraphAlignment alignment )  
  525.         {  
  526.             oWordApplic.Selection.ParagraphFormat.Alignment = alignment;  
  527.         }  
  528.   
  529.         /**//// <summary>  
  530.         /// 首行缩进  
  531.         /// </summary>  
  532.         /// <param name="fltCount">float类型的数值</param>  
  533.         public void SetFirstLineIndent( float fltCount )  
  534.         {  
  535.             oWordApplic.Selection.ParagraphFormat.FirstLineIndent = fltCount;  
  536.         }  
  537.   
  538.         /**//// <summary>  
  539.         /// 左缩进  
  540.         /// </summary>  
  541.         /// <param name="fltCount">float类型的数值</param>  
  542.         public void SetLeftIndent( float fltCount )  
  543.         {  
  544.             oWordApplic.Selection.ParagraphFormat.LeftIndent = fltCount;  
  545.         }  
  546.   
  547.         /**//// <summary>  
  548.         /// 右缩进  
  549.         /// </summary>  
  550.         /// <param name="fltCount">float类型的数值</param>  
  551.         public void SetRightIndent(float fltCount)  
  552.         {  
  553.             oWordApplic.Selection.ParagraphFormat.RightIndent = fltCount;  
  554.         }  
  555.   
  556.         
  557.   
  558.         /**//// <summary>  
  559.         /// 设置是否有粗体,0->否 ,1->是  
  560.         /// </summary>  
  561.         /// <param name="intBoldBi"></param>  
  562.         public void setBoldBi( int intBoldBi )  
  563.         {  
  564.             oWordApplic.Selection.Font.BoldBi = intBoldBi;  
  565.             //oWordApplic.Selection.Font.Bold = intBoldBi;  
  566.         }  
  567.   
  568.         public void SetBoldSize( int intBold )  
  569.         {  
  570.             oWordApplic.Selection.Font.Bold = intBold;  
  571.         }  
  572.   
  573.         public void SetUnderLine( Microsoft.Office.Interop.Word.WdUnderline underLine )  
  574.         {  
  575.             oWordApplic.Selection.Font.Underline = underLine;   
  576.         }  
  577.   
  578.         public void SetUnderLineColor( Microsoft.Office.Interop.Word.WdColor Color )  
  579.         {  
  580.             oWordApplic.Selection.Font.UnderlineColor = Color;  
  581.         }  
  582.   
  583.   
  584.          
  585.         /**//// <summary>  
  586.         /// 向后定位到指定段落  
  587.         /// </summary>  
  588.         /// <param name="para"></param>  
  589.         /// <param name="count"></param>  
  590.         public void GoToNextParagraph(ref Microsoft.Office.Interop.Word.Paragraph para,ref object count)  
  591.         {  
  592.             para.Next(ref count) ;  
  593.         }  
  594.   
  595.         /**//// <summary>  
  596.         /// 向前定位到指定段落  
  597.         /// </summary>  
  598.         /// <param name="para"></param>  
  599.         /// <param name="count"></param>  
  600.         public void GoToPreviousParagraph( ref Microsoft.Office.Interop.Word.Paragraph para, ref object count )  
  601.         {  
  602.             para.Previous( ref count ) ;  
  603.         }  
  604.   
  605.         
  606.   
  607.         public Boolean ExecuteReplace( Microsoft.Office.Interop.Word.Find find, Object replaceOption )  
  608.         {  
  609.             // Simple wrapper around Find.Execute:  
  610.             Object findText = Type.Missing;  
  611.             Object matchCase = Type.Missing;  
  612.             Object matchWholeWord = Type.Missing;  
  613.             Object matchWildcards = Type.Missing;  
  614.             Object matchSoundsLike = Type.Missing;  
  615.             Object matchAllWordForms = Type.Missing;  
  616.             Object forward = Type.Missing;  
  617.             Object wrap = Type.Missing;  
  618.             Object format = Type.Missing;  
  619.             Object replaceWith = Type.Missing;  
  620.             Object replace = replaceOption;  
  621.             Object matchKashida = Type.Missing;  
  622.             Object matchDiacritics = Type.Missing;  
  623.             Object matchAlefHamza = Type.Missing;  
  624.             Object matchControl = Type.Missing;  
  625.         
  626.             return find.Execute( ref findText, ref matchCase, ref matchWholeWord,   
  627.                 ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,   
  628.                 ref forward, ref wrap, ref format,    ref replaceWith, ref replace,   
  629.                 ref matchKashida, ref matchDiacritics, ref matchAlefHamza,   
  630.                 ref matchControl );  
  631.         }  
  632.       
  633.         public Boolean ExecuteFind( Microsoft.Office.Interop.Word.Find find )  
  634.         {  
  635.             return ExecuteFind( find, find.Text, Type.Missing, Type.Missing );  
  636.         }  
  637.         public Boolean ExecuteFind( Microsoft.Office.Interop.Word.Find find, string strFindText )  
  638.         {  
  639.             return ExecuteFind( find, strFindText, Type.Missing, Type.Missing );  
  640.         }  
  641.   
  642.         public Boolean ExecuteFind(  
  643.             Microsoft.Office.Interop.Word.Find find, string strFindText, Object wrapFind, Object forwardFind )  
  644.         {  
  645.             // Simple wrapper around Find.Execute:  
  646.               
  647.             Object findText ;  
  648.             Object matchCase = Type.Missing;  
  649.             Object matchWholeWord = Type.Missing;  
  650.             Object matchWildcards = Type.Missing;  
  651.             Object matchSoundsLike = Type.Missing;  
  652.             Object matchAllWordForms = Type.Missing;  
  653.             Object forward = forwardFind;  
  654.             Object wrap = wrapFind;  
  655.             Object format = Type.Missing;  
  656.             Object replaceWith = Type.Missing;  
  657.             Object replace = Type.Missing;  
  658.             Object matchKashida = Type.Missing;  
  659.             Object matchDiacritics = Type.Missing;  
  660.             Object matchAlefHamza = Type.Missing;  
  661.             Object matchControl = Type.Missing;  
  662.         
  663.             if ( ( strFindText == "" )||( strFindText == string.Empty ) )  
  664.                 findText = find.Text;  
  665.             else  
  666.                 findText = strFindText;  
  667.             find.ClearFormatting();  
  668.             return find.Execute( ref findText, ref matchCase, ref matchWholeWord,   
  669.                 ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,   
  670.                 ref forward, ref wrap, ref format, ref replaceWith, ref replace,   
  671.                 ref matchKashida, ref matchDiacritics, ref matchAlefHamza,   
  672.                 ref matchControl );  
  673.         }  
  674.           
  675.       
  676.         public Boolean FindInSelection( Microsoft.Office.Interop.Word.Selection Selection, string strFindText )   
  677.         {  
  678.                           
  679.             return this.ExecuteFind( Selection.Find,strFindText,System.Type.Missing,System.Type.Missing );  
  680.         }  
  681.   
  682.         public Boolean FindInSelection( Microsoft.Office.Interop.Word.Selection Selection, string strFindText, Object wrapFind, Object forwardFind )   
  683.         {  
  684.                           
  685.             return this.ExecuteFind( Selection.Find, strFindText, wrapFind, forwardFind );  
  686.         }  
  687.   
  688.         public Boolean FindInRange( Microsoft.Office.Interop.Word.Range range,string strFindText )   
  689.         {  
  690.             Boolean blnReturn  =this.ExecuteFind( range.Find, strFindText, Type.Missing, Type.Missing );  
  691.             range.Select();  
  692.             return blnReturn;  
  693.         }  
  694.     
  695.   
  696.         /// <summary>  
  697.         /// 根据文本批量拆分word  
  698.         /// </summary>  
  699.         /// <param name="strFindText">拆分文字</param>  
  700.         public System.Collections.ArrayList FindInAllDocument( string strFindText )   
  701.         {  
  702.             System.Collections.ArrayList list = new System.Collections.ArrayList();  
  703.             Microsoft.Office.Interop.Word.Find fnd = oWordApplic.Selection.Find;  
  704.            fnd.Text = strFindText;  
  705.            fnd.Replacement.Text = "";  
  706.            fnd.Application.ScreenUpdating = false;  
  707.            fnd.Forward = true;  
  708.            fnd.Wrap = WdFindWrap.wdFindStop;  
  709.            fnd.Format = false;  
  710.            fnd.MatchCase = false;  
  711.            fnd.MatchWholeWord = false;  
  712.            fnd.MatchByte = false;  
  713.            fnd.MatchAllWordForms = false;  
  714.            fnd.MatchSoundsLike = false;  
  715.            fnd.MatchWildcards = true;  
  716.            Object nStart, nEnd;  
  717.             Boolean fContinue ;   
  718.             fContinue = true;  
  719.             Object wdStory= WdUnits.wdStory;  
  720.             oWordApplic.Selection.StartOf(ref wdStory,ref missing);  
  721.             while (fContinue)  
  722.             {  
  723.                  fnd.ClearFormatting();  
  724.                  nStart = oWordApplic.Selection.Start;  
  725.                  if (ExecuteFind(fnd))  
  726.                  {  
  727.                      Object dwLine=Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  728.                      nEnd = oWordApplic.Selection.End;  
  729.                  }  
  730.                  else  
  731.                  {  
  732.                      nEnd = oDoc.Content.End;  
  733.                      fContinue = false;  
  734.                  }  
  735.                  object wdCollapseEnd = WdCollapseDirection.wdCollapseEnd;  
  736.                  oWordApplic.Selection.Collapse(ref wdCollapseEnd);  
  737.              
  738.                 oDoc.Range(ref nStart,ref nEnd).Copy();  
  739.                  Document newdoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);  
  740.                  newdoc.Content.Paste();  
  741.   
  742.                  string path= System.Web.HttpContext.Current.Server.MapPath("word");  
  743.                  //要保存的文件夹  
  744.                 string follter = DateTime.Now.ToShortDateString();  
  745.                 if (!System.IO.Directory.Exists(path + "/" + follter))  
  746.                 {  
  747.                     System.IO.Directory.CreateDirectory(path + "/" + follter);  
  748.                 }  
  749.                 object filename = follter+"/"+Ks5uCMS.Common.Rand.Str(5) + "-" + Ks5uCMS.Common.Rand.Str(5) + "-" + DateTime.Now.Ticks + ".doc";  
  750.                  object newFileName = path + "/" + filename;  
  751.                  newdoc.SaveAs(ref newFileName, ref missing, ref missing, ref missing, ref missing,  
  752.                      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  
  753.                      ref missing, ref missing, ref missing, ref missing, ref missing);  
  754.                  newdoc.Close(ref missing, ref missing, ref missing);  
  755.                  list.Add(filename);  
  756.                  
  757.             }  
  758.             return list;  
  759.         }  
  760.   
  761.         /// <summary>  
  762.         /// 从字符开始到结束的word  
  763.         /// </summary>  
  764.         /// <param name="startStr"></param>  
  765.         /// <param name="endStr"></param>  
  766.         /// <returns></returns>  
  767.         public string FindTitleAllDocument(string startStr,string endStr)  
  768.         {  
  769.             this.Replace("【解析】","");  
  770.             Microsoft.Office.Interop.Word.Find fnd = oWordApplic.Selection.Find;  
  771.             fnd.Text = startStr;  
  772.             fnd.Replacement.Text = "";  
  773.             fnd.Application.ScreenUpdating = false;  
  774.             fnd.Forward = true;  
  775.             fnd.Wrap = WdFindWrap.wdFindStop;  
  776.             fnd.Format = false;  
  777.             fnd.MatchCase = false;  
  778.             fnd.MatchWholeWord = false;  
  779.             fnd.MatchByte = false;  
  780.             fnd.MatchAllWordForms = false;  
  781.             fnd.MatchSoundsLike = false;  
  782.             fnd.MatchWildcards = true;  
  783.             Object nStart, nEnd;  
  784.             Object wdStory = WdUnits.wdStory;  
  785.               
  786.             oWordApplic.Selection.StartOf(ref wdStory, ref missing);  
  787.             fnd.ClearFormatting();  
  788.             if (ExecuteFind(fnd))  
  789.                 nStart = oWordApplic.Selection.End;  
  790.             else  
  791.                 nStart = oWordApplic.Selection.Start;  
  792.   
  793.   
  794.             fnd.Text = endStr;  
  795.             oWordApplic.Selection.StartOf(ref wdStory, ref missing);  
  796.             fnd.ClearFormatting();  
  797.             if (ExecuteFind(fnd))  
  798.                 nEnd = oWordApplic.Selection.Start;  
  799.              else  
  800.                 nEnd = oDoc.Content.End;  
  801.             object wdCollapseEnd = WdCollapseDirection.wdCollapseEnd;  
  802.             oWordApplic.Selection.Collapse(ref wdCollapseEnd);  
  803.   
  804.             oDoc.Range(ref nStart, ref nEnd).Copy();  
  805.             Document newdoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);  
  806.             newdoc.Content.Paste();  
  807.   
  808.                 string path = System.Web.HttpContext.Current.Server.MapPath("word");  
  809.                 //要保存的文件夹  
  810.                 string follter = DateTime.Now.ToShortDateString();  
  811.                 if (!System.IO.Directory.Exists(path + "/" + follter))  
  812.                 {  
  813.                     System.IO.Directory.CreateDirectory(path + "/" + follter);  
  814.                 }  
  815.                 object filename = follter + "/" + Ks5uCMS.Common.Rand.Str(5) + "-" + Ks5uCMS.Common.Rand.Str(5) + "-" + DateTime.Now.Ticks + ".doc";  
  816.                 object newFileName = path + "/" + filename;  
  817.                 newdoc.SaveAs(ref newFileName, ref missing, ref missing, ref missing, ref missing,  
  818.                     ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  
  819.                     ref missing, ref missing, ref missing, ref missing, ref missing);  
  820.                 newdoc.Close(ref missing, ref missing, ref missing);  
  821.   
  822.                 return filename.ToString();  
  823.   
  824.   
  825.         }  
  826.   
  827.         /// <summary>  
  828.         /// 保存成HTML  
  829.         /// </summary>  
  830.         /// <param name="strFileName"></param>  
  831.         public void SaveAsHtml(string strFileName)  
  832.         {  
  833.             Type wordType = oWordApplic.GetType();  
  834.             object missing = System.Reflection.Missing.Value;  
  835.             object fileName = strFileName;  
  836.             object Format = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;  
  837.             oDoc.SaveAs(ref   fileName, ref   Format, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing,  
  838.                     ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing);  
  839.               
  840.   
  841.         }   
  842.   
  843.     }  
  844. }  

 

使用代码:

一开始位置到结束位置截取内容

 

[csharp]  view plain  copy
  1. Ks5uCms.Web2.Library.core.uploadWorldpassHTML uploadword = new Ks5uCms.Web2.Library.core.uploadWorldpassHTML();  
  2.                        Ks5uCms.Web2.Library.core.CCWordApp wp = new Ks5uCms.Web2.Library.core.CCWordApp();  
  3.                        wp.Open(path);  
  4.                        string path1 = wp.FindTitleAllDocument("【题文】""【答案】");  
  5.                        string path2 = wp.FindTitleAllDocument("【答案】""【标题】");  
  6.                        wp.Quit();  


二、根据结束标记批量拆分word

[csharp]  view plain  copy
  1. Ks5uCms.Web2.Library.core.CCWordApp wp = new Ks5uCms.Web2.Library.core.CCWordApp();  
  2.                wp.Open(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + filename));  
  3.                System.Collections.ArrayList arraylist = wp.FindInAllDocument("^13【结束】^13");  
  4.                wp.Quit();  


 


打包下载地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值