当前位置: 移动技术网 > IT编程>开发语言>.net > C#(1)实现一键从Word文档转换TXT文本的功能

C#(1)实现一键从Word文档转换TXT文本的功能

2019年07月27日  | 移动技术网IT编程  | 我要评论

北京保健按摩小妹,怎么做芝士蛋糕,孕妇胎教音乐大全

有想直接从word转txt文本的可以看看,懒得复制粘贴的也可以使用下,方便而快捷!!

首先打开vs2012创建一个简单的form窗体:

里面主要的就是一个存放word文档的button和一个执行的button

点击运行后:把实验的word文档导入文件中:

点击开始转换的button,进行执行文件:

点击确定实现整个过程,打开txt文档:

 核心代码部分:

namespace wordtotext
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }
        public static void wordtohtmltext(string wordfilepath)
        {
            try
            {
                word.application wapp = new word.application();
                //指定原文件和目标文件 
                object docpath = wordfilepath;
                string htmlpath = wordfilepath.substring(0, wordfilepath.length - 3) + "txt";
                object target = htmlpath;
                //缺省参数 
                object unknown = type.missing;
                //只读方式打开 
                object readonly = true;
                //打开doc文件 
                word.document document = wapp.documents.open(ref docpath, ref unknown,
                ref readonly, ref unknown, ref unknown,
                ref unknown, ref unknown, ref unknown,
                ref unknown, ref unknown, ref unknown,
                ref unknown);
                object format = word.wdsaveformat.wdformattext;
                document.saveas(ref target, ref format,
                ref unknown, ref unknown, ref unknown,
                ref unknown, ref unknown, ref unknown,
                ref unknown, ref unknown, ref unknown); 
                document.close(ref unknown, ref unknown, ref unknown);
                wapp.quit(ref unknown, ref unknown, ref unknown);
            }
            catch (exception e)
            {
                messagebox.show(e.message);
            }
        }
        private void button2_click(object sender, eventargs e)
        {
            if (textbox1.text != "")
            {
                wordtohtmltext(textbox1.text.trim());
                messagebox.show("转换成功,在word文件的同一目录下可找到txt数据!");
            }
        }

        private void button1_click(object sender, eventargs e)
        {
            if (openfiledialog1.showdialog() == dialogresult.ok)
                textbox1.text = openfiledialog1.filename;
        }
    }
}

  有兴趣的可以自己动手试试!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网