您的位置:程序门 -> .net技术 -> c#



如何在c#中遍历word文档?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何在c#中遍历word文档?[已结贴,结贴人:zhanglinlei]
发表于:2007-03-14 22:24:27 楼主
我需要使用c#实现对word文档里的全部文字遍历,并且对每个文字字型或颜色之类的不同修改。
现在我已经能实现打开word文档了,但就是不知道怎么分别取出单个字,并且修改。
发表于:2007-03-14 22:51:461楼 得分:0
我提的建议是把word转换成html或者xml,然后进行解析,一步一步去掉html中的tag,并且对css进行判断,只是建议,我自己没做过,我想应该是个方法吧
发表于:2007-03-14 23:06:302楼 得分:0
谢谢你的回答。不过,我的项目是使用c/s构架的,不是b/s.
发表于:2007-03-14 23:10:513楼 得分:0
对word的操作,要比较了解宏,比较难,帮顶
发表于:2007-03-14 23:17:244楼 得分:0
关注中.....
发表于:2007-03-15 08:24:415楼 得分:100
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;
                        }

                }
发表于:2007-03-15 08:24:576楼 得分:0
接上贴
        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   bookmark   =   (int)word.wdgotoitem.wdgotobookmark;
                        object   namebookmark   =   strbookmarkname;
                        owordapplic.selection.goto(ref   bookmark,   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;
                        }

                }

        }
}
发表于:2007-03-15 08:26:087楼 得分:0
调用方法:
  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();
发表于:2007-03-15 08:29:238楼 得分:0
///   开发环境:本程序在vs2005+.net2.0+office2003   sp2环境下运行通过
///   测试环境:本程序在vs2003+windows2003+office2007下测试通过
发表于:2007-03-15 08:43:269楼 得分:0
ding..
发表于:2007-03-15 08:46:1410楼 得分:0
太感谢了!
发表于:2007-03-15 08:49:3411楼 得分:0
mark
发表于:2007-07-25 11:34:4312楼 得分:0
xx
发表于:2007-08-20 12:09:5713楼 得分:0
mark


快速检索

最新资讯
热门点击