博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#将Word,Excel与Html,PDF互转

Posted on 2017-04-19 15:51  hyruur  阅读(211)  评论(0编辑  收藏  举报
public class OfficeHelper
    {
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="path"></param>
        public static string WordToHtml(string path)
        {
            //在此处放置用户代码以初始化页面
            Word.Application word = new Word.Application();
            Type wordType = word.GetType();
            Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
            try
            {
                Word.Document doc =(Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs,new Object[] {path, true, true});
                //转换格式,另存为
                Type docType = doc.GetType();
                string strSaveFileName = path.Substring(0, path.LastIndexOf('.')) + ".html";
                object saveFileName = (object) strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc,
                    new object[] {saveFileName, Word.WdSaveFormat.wdFormatHTML});
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                return saveFileName.ToString();
            }
            catch
            {
                throw new Exception("文件转换出错");
            }
            finally
            {
                //退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        /// <summary>
        /// word转成pdf
        /// </summary>
        /// <param name="path"></param>
        public static string WordToPdf(string path)
        {
            //在此处放置用户代码以初始化页面
            Word.Application word = new Word.Application();
            Type wordType = word.GetType();
            Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
            try
            {
                Word.Document doc =(Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {path, true, true});
                //转换格式,另存为
                Type docType = doc.GetType();
                string strSaveFileName = path.Split('.').GetValue(0) + ".pdf";
                object saveFileName = (object) strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc,
                    new object[] {saveFileName, Word.WdSaveFormat.wdFormatPDF});
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                return saveFileName.ToString();
            }
            catch
            {
                throw new Exception("文件转换出错");
            }
            finally
            {
                //退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }


        /// <summary>
        /// Excel转成html
        /// </summary>
        /// <param name="path"></param>
        public static string ExcelToHtml(string path)
        {
            Excel.Application repExcel = new Excel.Application();//实例化Excel
            Excel.Workbook workbook = null;
            try
            {
                workbook = repExcel.Application.Workbooks.Open(path, 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);
                object htmlFile = path.Substring(0, path.LastIndexOf('.')) + ".html";
                object ofmt = Excel.XlFileFormat.xlHtml;
                workbook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing); // 进行另存为操作   
                return htmlFile.ToString();
            }
            catch
            {
                throw new Exception("文件转换出错");
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Close(true, Type.Missing, Type.Missing);
                }
                repExcel.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        /// <summary>
        /// 把Excel文件转换成PDF格式文件
        /// </summary>
        /// <param name="path">源文件路径</param>
        /// <returns>true=转换成功</returns>
        public static string ExcelToPdf(string path)
        {
            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;
            Excel.Application repExcel = new Excel.Application();
            Excel.Workbook workBook = null;
            try
            {
                string savePath = path.Substring(0, path.LastIndexOf('.')) + ".pdf";
                object target = savePath;
                workBook = repExcel.Application.Workbooks.Open(path, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                return savePath;
            }
            catch
            {
                throw new Exception("文件转换出错");
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                }
                repExcel.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }