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


    有想直接从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;
            }
        }
    }
    

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

  • 相关阅读:
    PHP去除所有的空格
    PHP学习之分页类
    PHP学习之验证码类
    PHP学习之迭代生成器
    PHP学习之PHP trait解析
    PHP学习之PHP代码的优化
    PHP学习之PHP的语法糖
    PHP学习之PHP编码习惯
    PHP介绍
    Centos7安装PHP、安装MySQL、安装apache
  • 原文地址:https://www.cnblogs.com/yxd000/p/11255565.html
Copyright © 2020-2023  润新知