专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

C#Word转Html的类

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:58:03       共计:3630 浏览

已验证,可以在指定的文件目录下成功将doc文件转html

using System;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using System.Threading.Tasks;

 class WordToHtml
    {
        public static void CreateWordToHtmlFile(string WordFileDir)
        {
            DealWithWordFile(WordFileDir);
        }
        /// <summary>
        /// 搜索WordFileDir在的*.doc文件
        /// </summary>
        /// <param name="WordFileDir"></param>
        private static void DealWithWordFile(string WordFileDir)
        {
            //创建数组保存源文件夹下的文件名
            string[] strFiles = Directory.GetFiles(WordFileDir, "*.doc");
            for (int i = 0; i < strFiles.Length; i++)
            {
                WordToHtmlFile(strFiles[i]);
            }

            DirectoryInfo dirInfo = new DirectoryInfo(WordFileDir);
            //取得源文件夹下的所有子文件夹名称
            DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
            for (int j = 0; j < ZiPath.Length; j++)
            {
                //获取所有子文件夹名
                string strZiPath = WordFileDir + "/" + ZiPath[j].ToString();
                //把得到的子文件夹当成新的源文件夹,从头开始新一轮的搜索
                DealWithWordFile(strZiPath);
            }
        }
        /// <summary>
        /// 转化
        /// </summary>
        /// <param name="WordFilePath"></param>
        private static void WordToHtmlFile(string WordFilePath)
        {
            try
            {
                Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
                // 指定原文件和目标文件
                object Source = WordFilePath;
                string SaveHtmlPath = WordFilePath.Substring(0, WordFilePath.Length - 3) + "html";
                object Target = SaveHtmlPath;

                // 缺省参数  
                object Unknown = Type.Missing;

                //为了保险,只读方式打开
                object readOnly = true;

                // 打开doc文件
                Microsoft.Office.Interop.Word.Document doc = newApp.Documents.Open(ref Source, ref Unknown,
                     ref readOnly, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown);

                // 指定另存为格式(rtf)
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
                // 转换格式
                doc.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown);

                // 关闭文档和Word程序
                doc.Close(ref Unknown, ref Unknown, ref Unknown);
                newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
        }
    }

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C# Net 使用 openxml 提取word中的文本和图片并转为Html | ·下一条:c#把word文档转换为html页面

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有