Word转Html

需要引用两个dll,

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Diagnostics;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        GenerationWordHTML("D://20110502.doc", "D://20110502.html");
        GenerationExcelHTML("E://20110502.xls", "E://20110502.html");
    }

    /// <summary>     
    /// Ecxel文件生成HTML并保存     
    /// </summary>     
    /// <param name="FilePath">需要生成的ecxel文件的路径</param>     
    /// <param name="saveFilePath">生成以后保存HTML文件的路径</param>     
    /// <returns>是否生成成功,成功为true,反之为false</returns>     
    protected bool GenerationExcelHTML(string FilePath, string saveFilePath)
    {
        try
        {
            Excel.Application app = new Excel.Application();
            app.Visible = false;
            Object o = Missing.Value;

            ///打开文件     
            /*下面是Microsoft Excel 9 Object Library的写法: */
            /*_Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o);*/

            /*下面是Microsoft Excel 10 Object Library的写法: */
            _Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o, o, o);

            ///转换格式,另存为 HTML     
            /*下面是Microsoft Excel 9 Object Library的写法: */
            /*xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);*/

            /*下面是Microsoft Excel 10 Object Library的写法: */
            xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o);

            ///退出 Excel     
            app.Quit();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            //最后关闭打开的excel 进程     
            Process[] myProcesses = Process.GetProcessesByName("EXCEL");
            foreach (Process myProcess in myProcesses)
            {
                myProcess.Kill();
            }
        }
    }

    /// <summary>     
    /// WinWord文件生成HTML并保存     
    /// </summary>     
    /// <param name="FilePath">需要生成的word文件的路径</param>     
    /// <param name="saveFilePath">生成以后保存HTML文件的路径</param>     
    /// <returns>是否生成成功,成功为true,反之为false</returns>     
    private bool GenerationWordHTML(string FilePath, string saveFilePath)
    {
        try
        {
            Word.ApplicationClass word = new Word.ApplicationClass();
            Type wordType = word.GetType();
            Word.Documents docs = word.Documents;

            /// 打开文件      
            Type docsType = docs.GetType();
            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { FilePath, true, true });

            /// 转换格式,另存为 HTML      
            Type docType = doc.GetType();

            /*下面是Microsoft Word 9 Object Library的写法: */
            /*docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatHTML });*/

            /*下面是Microsoft Word 10 Object Library的写法: */
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
            null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML });

            /// 退出 Word     
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            //最后关闭打开的winword 进程     
            Process[] myProcesses = Process.GetProcessesByName("WINWORD");
            foreach (Process myProcess in myProcesses)
            {
                myProcess.Kill();
            }
        }
    }   
}

posted @   易独  阅读(747)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
乐巴儿 一个有声音的公众号
长按,识别二维码,加关注
点击右上角即可分享
微信分享提示