我需要使用C#实现对WORD文档里的全部文字遍历,并且对每个文字字型或颜色之类的不同修改。
现在我已经能实现打开WORD文档了,但就是不知道怎么分别取出单个字,并且修改。

解决方案 »

  1.   

    我提的建议是把word转换成HTML或者XML,然后进行解析,一步一步去掉html中的tag,并且对css进行判断,只是建议,我自己没做过,我想应该是个方法吧
      

  2.   

    谢谢你的回答。不过,我的项目是使用C/S构架的,不是B/S.
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ControlWord
    {
        /// <summary>
        /// 功能:在WINFORM中处理WORD的各种操作
        /// 作者:黄海
        /// 时间:2007-3-13
        /// </summary>
        public class CCWordApp
        {
            private Word.ApplicationClass oWordApplic; // a reference to Word application
            private Word.Document oDoc; // a reference to the document        //用于读取打开Word的字符数
            public int Count = 0;        public CCWordApp()
            {            oWordApplic = new Word.ApplicationClass();
            }        /// <summary>
            /// 功能:打开WORD文档
            /// 作者:黄海
            /// 
            /// </summary>
            /// <param name="strFileName"></param>
            public void Open(string strFileName)
            {
                object fileName = strFileName;
                object readOnly = false;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;            oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing);
                
                Count = oDoc.Characters.Count;
                oDoc.Activate();
            }
            /// <summary>
            /// 功能:WORD退出
            /// 作者:黄海
            /// </summary>
            public void Quit()
            {
                object missing = System.Reflection.Missing.Value;
                oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
            }        public void Save()
            {
                oDoc.Save();
            }        public void SaveAs(string strFileName)
            {
                object missing = System.Reflection.Missing.Value;
                object fileName = strFileName;            oDoc.SaveAs(ref fileName, 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);
            }
            public void SaveAsHtml(string strFileName)
            {
                object missing = System.Reflection.Missing.Value;
                object fileName = strFileName;
                object Format = (int)Word.WdSaveFormat.wdFormatHTML;
                oDoc.SaveAs(ref fileName, ref Format, 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);
                oDoc.Close(ref missing, ref missing, ref missing);        }
            public void CopyAll()
            {
                oWordApplic.Selection.WholeStory();
                oWordApplic.Selection.Copy();        }        public void PasetAll()
            {            oWordApplic.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
            }
            public void Clear()
            {            object Unit = (int)Word.WdUnits.wdCharacter;
                object Count = 1;
                oWordApplic.Selection.WholeStory();
                oWordApplic.Selection.Delete(ref Unit, ref Count);
            }
            public void InsertText(string strText)
            {
                oWordApplic.Selection.TypeText(strText);
            }        public void InsertLineBreak()
            {
                oWordApplic.Selection.TypeParagraph();
            }
            public void InsertLineBreak(int nline)
            {
                for (int i = 0; i < nline; i++)
                    oWordApplic.Selection.TypeParagraph();
            }
            public void SetAlignment(string strType)
            {
                switch (strType)
                {
                    case "Center":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        break;
                    case "Left":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                        break;
                    case "Right":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                        break;
                    case "Justify":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                        break;
                }        }        public void SetFont(string strType)
            {
                switch (strType)
                {
                    case "Bold":
                        oWordApplic.Selection.Font.Bold = 1;
                        break;
                    case "Italic":
                        oWordApplic.Selection.Font.Italic = 1;
                        break;
                    case "Underlined":
                        oWordApplic.Selection.Font.Subscript = 0;
                        break;
                }        }
      

  4.   

    接上贴
        public void SetFont()
            {
                oWordApplic.Selection.Font.Bold = 0;
                oWordApplic.Selection.Font.Italic = 0;
                oWordApplic.Selection.Font.Subscript = 0;        }        public void SetFontName(string strType)
            {
                oWordApplic.Selection.Font.Name = strType;        }        public void SetFontSize(int nSize)
            {
                oWordApplic.Selection.Font.Size = nSize;        }        public void InsertPagebreak()
            {            object pBreak = (int)Word.WdBreakType.wdPageBreak;
                oWordApplic.Selection.InsertBreak(ref pBreak);
            }        public void GotoBookMark(string strBookMarkName)
            {            object missing = System.Reflection.Missing.Value;            object Book = (int)Word.WdGoToItem.wdGoToBook;
                object NameBookMark = strBookMarkName;
                oWordApplic.Selection.GoTo(ref Book, ref missing, ref missing, ref NameBookMark);
            }        public void GoToTheEnd()
            {            object missing = System.Reflection.Missing.Value;
                object unit;
                unit = Word.WdUnits.wdStory;
                oWordApplic.Selection.EndKey(ref unit, ref missing);        }
            public void GoToTheBeginning()
            {            object missing = System.Reflection.Missing.Value;
                object unit;
                unit = Word.WdUnits.wdStory;
                oWordApplic.Selection.HomeKey(ref unit, ref missing);        }        public void GoToTheTable(int ntable)
            {            object missing = System.Reflection.Missing.Value;
                object what;
                what = Word.WdUnits.wdTable;
                object which;
                which = Word.WdGoToDirection.wdGoToFirst;
                object count;
                count = 1;
                oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
                oWordApplic.Selection.Find.ClearFormatting();            oWordApplic.Selection.Text = "";
            }        public void GoToRightCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdCell;
                oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
            }        public void GoToLeftCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdCell;
                oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
            }        public void GoToDownCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdLine;
                oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
            }        public void GoToUpCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdLine;
                oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
            }
            public void InsertPageNumber(string strType, bool bHeader)
            {
                object missing = System.Reflection.Missing.Value;
                object alignment;
                object bFirstPage = false;
                object bF = true;
                switch (strType)
                {
                    case "Center":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                        break;
                    case "Right":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                        break;
                    case "Left":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
                        break;
                }        }    }
    }
      

  5.   

    调用方法:
     CCWordApp test;
                        test = new CCWordApp();
                        test.Open(fi.FullName);
                        //这里为什么是2不是1呢?没整明白
                        if (test.Count < 2)
                        {
                            test.GoToTheEnd();
                            test.InsertText("略");
                            test.Save();
                            Content.Text += "处理了下面的文件:" + fi.Name + "\r\n";
                        }
                        else
                        {
                            Content.Text += "略过了下面的文件:" + fi.Name + "\r\n";
                        }                    test.Quit();
      

  6.   

    /// 开发环境:本程序在VS2005+.NET2.0+OFFICE2003 SP2环境下运行通过
    /// 测试环境:本程序在VS2003+WINDOWS2003+OFFICE2007下测试通过