C#DOC批量转TXT工具

来源:互联网 发布:java 判断法定节假日 编辑:程序博客网 时间:2024/05/05 03:10

先上图:

本程序是偶利用一个下午的时间呕心沥血才搞定的,呵呵——!可以把某一路径下的doc文件全部转化为TXT文件,但是程序没有检索子目录,如有需要请参考源码修改!

本程序在win2008,vs2008下编译,如有需要编译好的程序者请留下email我如果有空的话会给你mail一个的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace doc2txt
{
    public partial class Form1 : Form
    {
        string[] fs;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string _path;
            fldDlg.ShowDialog();
            _path = fldDlg.SelectedPath;
            if (!Directory.Exists(_path)) return;
            textBox1.Text = _path;
            fs = Directory.GetFiles(_path, "*.doc");
            foreach (string f in fs)
            {
                textBox2.AppendText(f + "/r/n");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Word.Application oWord = new Word.Application();
            object vOpt = System.Reflection.Missing.Value;
            try
            {
                textBox2.Text = "";
                foreach (string of in fs)
                {
                    object f = (object)of;
                    Word.Document vDoc = oWord.Documents.Open(ref f, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt);

                    string txtf = of.Substring(0, of.Length - 3) + "txt";
                    if (File.Exists(txtf)) File.Delete(txtf);
                    object otxt = (object)txtf;
                    object saveFormat = Word.WdSaveFormat.wdFormatText;
                    vDoc.SaveAs(ref otxt, ref saveFormat, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt);
                    vDoc.Close(ref vOpt, ref vOpt, ref vOpt);
                    textBox2.AppendText(of + " --> " + Path.GetFileName(txtf) + "/r/n");
                }
                MessageBox.Show("转化完成!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                oWord.Quit(ref vOpt, ref vOpt, ref vOpt);
                oWord = null;
            }
        }
    }
}

原创粉丝点击