C#实现批量Word转换Html的示例代码

 更新时间:2022年12月22日 08:48:43   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#批量Word转换Html的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public static void BatchConvert(string docDir)//如果批量转换
    {
        //创建数组保存文件夹下的文件名 
        string[] docFiles = Directory.GetFiles(docDir, "*.doc");
        for (int i = 0; i < docFiles.Length; i++)
        {
            WordToHtmlFile(docFiles[i]);
        }
        MessageBox.Show("转换完毕!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    //将word转换为html
    public static void WordToHtmlFile(string WordFilePath)
    {
        try
        {
            Microsoft.Office.Interop.Word.Application wApp = new Microsoft.Office.Interop.Word.Application();
            //指定原文件和目标文件 
            object docPath = WordFilePath;
 			string htmlPath;
            if (WordFilePath.Contains(".docx"))
            {
                htmlPath = WordFilePath.Substring(0, WordFilePath.Length - 4) + "html";
            }
            else
            {
                htmlPath = WordFilePath.Substring(0, WordFilePath.Length - 3) + "html";
            }
            object Target = htmlPath;
            //缺省参数 
            object Unknown = Type.Missing;
            //只读方式打开 
            object readOnly = true;
            //打开doc文件 
            Microsoft.Office.Interop.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 = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
            // 转换格式 
            document.SaveAs(ref Target, ref format,
            ref Unknown, ref Unknown, ref Unknown,
            ref Unknown, ref Unknown, ref Unknown,
            ref Unknown, ref Unknown, ref Unknown);
            // 关闭文档和Word程序 
            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 button1_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            tsslInfo.Text = "";
            txtWordPath.Text = folderBrowserDialog1.SelectedPath;
            string[] docFiles = Directory.GetFiles(txtWordPath.Text.Trim(),"*.doc");
            for (int i = 0; i < docFiles.Length; i++)
            {
                string docPath=docFiles[i].ToString();
                string docName = docPath.Substring(docPath.LastIndexOf("\\") + 1, docPath.Length - docPath.LastIndexOf("\\") -1);
                listView1.Items.Add(docName);
            }
            tsslInfo.Text = "文件数量:"+listView1.Items.Count.ToString();
        }
    }

    private void button2_Click(object sender, EventArgs e)//单个转换
    {
        if (listView1.Items.Count > 0)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("确定将此文档转换为HTML文件吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    string docPath = txtWordPath.Text.Trim() + "\\" + listView1.SelectedItems[0].Text;
                    WordToHtmlFile(docPath);
                    MessageBox.Show("转换完毕!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选择要转换的Word文档!");
            }
        }
    }

    private void button4_Click(object sender, EventArgs e)//批量转换
    {
        if (listView1.Items.Count > 0)
        {
            if (MessageBox.Show("确定将批量将文档转换为HTML文件吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                BatchConvert(txtWordPath.Text.Trim());
            }
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.txtWordPath = new System.Windows.Forms.TextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.listView1 = new System.Windows.Forms.ListView();
        this.button2 = new System.Windows.Forms.Button();
        this.button3 = new System.Windows.Forms.Button();
        this.statusStrip1 = new System.Windows.Forms.StatusStrip();
        this.tsslInfo = new System.Windows.Forms.ToolStripStatusLabel();
        this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
        this.button4 = new System.Windows.Forms.Button();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.statusStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(8, 21);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 0;
        this.label1.Text = "选择目录:";
        // 
        // txtWordPath
        // 
        this.txtWordPath.Location = new System.Drawing.Point(70, 17);
        this.txtWordPath.Name = "txtWordPath";
        this.txtWordPath.Size = new System.Drawing.Size(294, 21);
        this.txtWordPath.TabIndex = 1;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(370, 15);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(59, 23);
        this.button1.TabIndex = 2;
        this.button1.Text = "浏览...";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.txtWordPath);
        this.groupBox1.Controls.Add(this.button1);
        this.groupBox1.Controls.Add(this.label1);
        this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.groupBox1.Location = new System.Drawing.Point(13, 12);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(435, 44);
        this.groupBox1.TabIndex = 3;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "选择目录";
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.listView1);
        this.groupBox2.Location = new System.Drawing.Point(13, 62);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(435, 187);
        this.groupBox2.TabIndex = 4;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "Word文档";
        // 
        // listView1
        // 
        this.listView1.FullRowSelect = true;
        this.listView1.Location = new System.Drawing.Point(6, 17);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(423, 161);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.View = System.Windows.Forms.View.List;
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(225, 259);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(59, 23);
        this.button2.TabIndex = 6;
        this.button2.Text = "转换";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(389, 259);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(59, 23);
        this.button3.TabIndex = 7;
        this.button3.Text = "取消";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button3_Click);
        // 
        // statusStrip1
        // 
        this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.tsslInfo});
        this.statusStrip1.Location = new System.Drawing.Point(0, 285);
        this.statusStrip1.Name = "statusStrip1";
        this.statusStrip1.Size = new System.Drawing.Size(460, 22);
        this.statusStrip1.TabIndex = 8;
        this.statusStrip1.Text = "statusStrip1";
        // 
        // tsslInfo
        // 
        this.tsslInfo.Name = "tsslInfo";
        this.tsslInfo.Size = new System.Drawing.Size(131, 17);
        this.tsslInfo.Text = "toolStripStatusLabel1";
        // 
        // button4
        // 
        this.button4.Location = new System.Drawing.Point(298, 259);
        this.button4.Name = "button4";
        this.button4.Size = new System.Drawing.Size(75, 23);
        this.button4.TabIndex = 9;
        this.button4.Text = "批量转换";
        this.button4.UseVisualStyleBackColor = true;
        this.button4.Click += new System.EventHandler(this.button4_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(460, 307);
        this.Controls.Add(this.button4);
        this.Controls.Add(this.statusStrip1);
        this.Controls.Add(this.button3);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Word转换为Html";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.groupBox2.ResumeLayout(false);
        this.statusStrip1.ResumeLayout(false);
        this.statusStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox txtWordPath;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.StatusStrip statusStrip1;
    private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.ToolStripStatusLabel tsslInfo;
}

以上就是C#实现批量Word转换Html的示例代码的详细内容,更多关于C# Word转Html的资料请关注脚本之家其它相关文章!

相关文章

  • 基于C#模拟实现回合制游戏

    基于C#模拟实现回合制游戏

    这篇文章主要介绍了通过C#模拟实现回合制游戏,文中的示例代码讲解详细,对我们的学习和工作有一定的帮助,感兴趣的可以跟随小编一起学习一下
    2021-12-12
  • 通过LinQ查询字符出现次数的实例方法

    通过LinQ查询字符出现次数的实例方法

    这篇文章主要介绍了通过LinQ查询字符出现次数的实例方法,大家参考使用吧
    2013-11-11
  • C#异步调用示例详解

    C#异步调用示例详解

    这篇文章主要为大家详细介绍了C#异步调用的示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#日历样式的下拉式计算器实例讲解

    C#日历样式的下拉式计算器实例讲解

    如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值。因此,用户就不得不使用计算器得到结果,再填入到输入字段中,或者在工作窗口上单独打开一个计算器窗口。总之,各种不便和麻烦。
    2015-09-09
  • C#操作字符串方法总结实例代码

    C#操作字符串方法总结实例代码

    这篇文章主要介绍了C#操作字符串方法总结实例代码,需要的朋友可以参考下
    2017-10-10
  • C#监测IPv4v6网速及流量的实例代码

    C#监测IPv4v6网速及流量的实例代码

    这篇文章主要介绍了C#监测IPv4v6网速及流量的实例代码,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C# salt+hash 加密

    C# salt+hash 加密

    本文主要介绍了C# salt+hash加密规则、C# salt产生伪随机数原理、hash原理、使用hash来加密的原因等等。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C#实现顺序队列和链队列的代码实例

    C#实现顺序队列和链队列的代码实例

    今天小编就为大家分享一篇关于C#实现顺序队列和链队列的代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • C#画笔Pen绘制光滑模式曲线的方法

    C#画笔Pen绘制光滑模式曲线的方法

    这篇文章主要介绍了C#画笔Pen绘制光滑模式曲线的方法,涉及C#图形绘制画笔Pen相关属性的设置技巧,需要的朋友可以参考下
    2015-06-06
  • C#简单操作MongoDB的步骤全纪录

    C#简单操作MongoDB的步骤全纪录

    最近花了不少时间研究学习了MongoDB数据库的相关知识,下面这篇文章主要给大家介绍了关于C#简单操作MongoDB的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧
    2018-09-09

最新评论