Csharp调用微软COM转换excel为HTML

使用微软的office中的excel软件只要使用另存为就可以把一个excel保存为HTML网页文件。但如何通过程序调用完成转化呢?

以下使用office 2007为例,其他版本略有不同。


 1添加引用,对于不同的版本,有所不一样。当然前提自然是安装了对应的微软office软件。



2转换代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace CommonConvert
{
    public class ExcelToHtml
    {
        /// <summary>
        /// 使用反射调用方法,返回生成的HTML文件路径
        /// </summary>
        /// <param name="excelFileName"></param>
        /// <returns></returns>
        public string ExcelToHtmlFile(object excelFileName)
        {
            //在此处放置用户代码以初始化页面 
            Excel.Application excel = new Excel.Application();
            Type wordType = excel.GetType();
            Excel.Workbooks docs = excel.Workbooks;
            //打开文件 
            Type docsType = docs.GetType();
            Excel.Workbook doc = (Excel.Workbook)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { excelFileName, true, true });
            
            Type docType = doc.GetType();             
            string htmlFullFileName = excelFileName + ".html"; //HTML文件路径   
            object ofmt = Excel.XlFileFormat.xlHtml;
            //转换格式,另存为 
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { htmlFullFileName, ofmt});
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);      
            //退出  
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, excel, null);
            return htmlFullFileName.ToString();
        }
        /// <summary>
        /// 直接调用方法,不使用反射
        /// </summary>
        /// <param name="excelFullFileName"></param>
        /// <returns></returns>
        public string ExcelToHtmlFile2(string  excelFullFileName)
        {

            Excel.Application excel = new Excel.Application();           
            Excel.Workbooks docs = excel.Workbooks;
            //打开文件  
          //  Excel.Workbook doc = docs.Open(excelFullFileName.ToString(), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Excel.Workbook doc = docs.Open(excelFullFileName.ToString());
           
            string htmlFullFileName = excelFullFileName + ".html"; //HTML文件路径 
            object ofmt = Excel.XlFileFormat.xlHtml;
            //转换格式,另存为 
            // doc.SaveAs(htmlFullFileName, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
           // doc.SaveAs(htmlFullFileName, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange);
            doc.SaveAs(htmlFullFileName, ofmt);
            doc.Close();
            //退出             
            excel.Quit();
            return htmlFullFileName;
        }    
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值