当前位置:编程学习 > C#/ASP.NET >>

Word 转化问题

各位高手:
     你们好!小弟碰到了一个辣手的问题,我想把Word2003格式的文档转化成Word2007格式的文档,能不能在不打开word2003的情况下,利用自己的代码或者利用微软的东东转化,最好是利用微软的东东。我找了很久都不知道怎么办,望高人指点。我用的是C#2005 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;
namespace ConvertWord
{
    public partial class FormConvert : Form
    {
        public FormConvert()
        {
            InitializeComponent();
        }
        private Object missing = System.Reflection.Missing.Value;
        private string  srcDirectory = null;
        private string distDirectory;
        private void btnOpenDirectory_Click(object sender, EventArgs e)
        {
            this.flbDgWord.ShowNewFolderButton = false;
            this.flbDgWord.RootFolder = Environment.SpecialFolder.MyComputer;
            this.flbDgWord.SelectedPath = Application.StartupPath;
           
            if (this.flbDgWord.ShowDialog() == DialogResult.OK)
            {
                if (srcDirectory == string.Empty)
                {
                    return;
                }
                srcDirectory = this.flbDgWord.SelectedPath;
                string[]    files = Directory.GetFiles(srcDirectory, "*.doc");

                lstFile.Items.Clear();
                foreach (string file in files)
                {
                    this.lstFile.Items.Add(file);
                }
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (srcDirectory == null)
            {
                //   srcDirectory = Application.StartupPath;
                //this.flbDgWord.SelectedPath = Application.StartupPath;
                return ;
            }
            else
            {
                this.flbDgWord.SelectedPath = this.srcDirectory;
            }            
            this.flbDgWord.ShowNewFolderButton = true;
            this.flbDgWord.RootFolder = Environment.SpecialFolder.MyComputer;
            if (this.flbDgWord.ShowDialog() == DialogResult.OK)
            {
                this.distDirectory = this.flbDgWord.SelectedPath;
                try
                {
                    string[] files = Directory.GetFiles(this.srcDirectory);
                    this.ConvertWord(files,this.distDirectory);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private  void ConvertWord(string[] files,string drtWord)
        {
            lstConverter.Items.Clear();
            Word._Application appword = null;

            try
            {
                System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();              
                foreach (System.Diagnostics.Process process in processes)
                {                    
                    if (process.ProcessName == "WINWORD")
                    {
                        if (MessageBox.Show(this, "为了防止错误发生,需要关闭Word文档,是否现在关闭吗?", "提醒", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            process.Kill();
                        }
                        else
                        {
                            MessageBox.Show(this,  "必须关闭word","提醒");
                            return;
                        }
                    }
                }
                appword = new Word.ApplicationClass();
                Word._Document doc;
                appword.Visible = false;
                Object saveformat = null;
                foreach (Word.FileConverter fileconvert in appword.FileConverters)
                {                   
                    if (fileconvert.Extensions == "docx")
                    {
                        saveformat = fileconvert.SaveFormat;
                        break;
                    }

                }
                if (saveformat == null)
                {
                    MessageBox.Show("Please Install Fileconvert");
                    return;
                }

                foreach (string file in files)
                {
                    Object ofile = file;
                    string[] str = file.Split('\\');
                    Object distfile =this.distDirectory +"\\" +str[str.Length-1]+"x";

                    doc = appword.Documents.Open(ref ofile, 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);
                    doc.SaveAs(ref distfile, ref saveformat, 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);
                    doc.Close(ref missing, ref missing, ref missing);

                }
                appword.Quit(ref missing, ref missing, ref missing); 
            }
            catch (Exception ex)
            {
               // appword.Quit(ref missing, ref missing, ref missing);
                appword.Quit(ref missing, ref missing, ref missing); 
                throw ex;               
            }
            finally
            {
               //appword.Quit(ref missing, ref missing, ref missing); 
            }
            
        }
    }
}

这是我用的方法,我不想用Word对象,行吗?
补充:.NET技术 ,  .NET Framework
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,