zoukankan      html  css  js  c++  java
  • OfficeToHtmlHelper

    public class Office2HtmlHelper
        {
            /// <summary>
            /// Word转成Html
            /// </summary>
            /// <param name="path">要转换的文档的路径</param>
            /// <param name="savePath">转换成html的保存路径</param>
            /// <param name="wordFileName">转换成html的文件名字</param>
            public static void Word2Html(string path, string savePath, string wordFileName)
            {
    
                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[] { (object)path, true, true });
                Type docType = doc.GetType();
                string strSaveFileName = savePath + wordFileName + ".html";
                object saveFileName = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
    
            }
            /// <summary>
            /// Excel转成Html
            /// </summary>
            /// <param name="path">要转换的文档的路径</param>
            /// <param name="savePath">转换成html的保存路径</param>
            /// <param name="wordFileName">转换成html的文件名字</param>
            public static void Excel2Html(string path, string savePath, string wordFileName)
            {
                string str = string.Empty;
                Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workbook = null;
                Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
                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);
                worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                object htmlFile = savePath + wordFileName + ".html";
                object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
                workbook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                object osave = false;
                workbook.Close(osave, Type.Missing, Type.Missing);
                repExcel.Quit();
            }
            /// <summary>
            /// ppt转成Html
            /// </summary>
            /// <param name="path">要转换的文档的路径</param>
            /// <param name="savePath">转换成html的保存路径</param>
            /// <param name="wordFileName">转换成html的文件名字</param>
            public static void PPT2Html(string path, string savePath, string wordFileName)
            {
                path = @"C:UserscnjazhuDesktopKLChina IntranetTraining石油和石油产品.ppt";
                Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
                string strSourceFile = path;
                string strDestinationFile = savePath + wordFileName + ".html";
                Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
    
                prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
                prsPres.Close();
                ppApp.Quit();
            }
            //// <summary>
            /// 把Word文件转换成pdf文件2
            /// </summary>
            /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
            /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
            /// <returns>成功返回true,失败返回false</returns>
            public bool WordToPdf(object sourcePath, string targetPath)
            {
                bool result = false;
                Word.WdExportFormat wdExportFormatPDF = Word.WdExportFormat.wdExportFormatPDF;
                object missing = Type.Missing;
                Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
                Microsoft.Office.Interop.Word.Document document = null;
                try
                {
                    applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
                    document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                    if (document != null)
                    {
                        document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Word.WdExportRange.wdExportAllDocument, 0, 0, Word.WdExportItem.wdExportDocumentContent, true, true, Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
                    }
                    result = true;
                }
                catch
                {
                    result = false;
                }
                finally
                {
                    if (document != null)
                    {
                        document.Close(ref missing, ref missing, ref missing);
                        document = null;
                    }
                    if (applicationClass != null)
                    {
                        applicationClass.Quit(ref missing, ref missing, ref missing);
                        applicationClass = null;
                    }
                }
                return result;
            }
            public static bool PPTConvertToPDF(string sourcePath, string targetPath)
            {
                sourcePath= @"C:UserscnjazhuDesktopKLChina IntranetTraining石油和石油产品.ppt";
                bool result;
                Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType ppSaveAsFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
                object missing = Type.Missing;
                Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
                Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
                try
                {
                    application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                    persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                    if (persentation != null)
                    {
                        persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
                    }
                    result = true;
                }
                catch(Exception e)
                {
                    result = false;
                }
                finally
                {
                    if (persentation != null)
                    {
                        persentation.Close();
                        persentation = null;
                    }
                    if (application != null)
                    {
                        application.Quit();
                        application = null;
                    }
                }
                return result;
            }
        }
  • 相关阅读:
    jetbrains 官网进不去以及webstorm、node下载慢的问题
    因为后端 xss 全局过滤器导致的 jquery ajax post 提交的数据全部为null的问题
    华为jdk镜像地址
    shiro解决两个请求的sessionId相同,但是某一个传入sessionId之后找不到session的原因
    推荐一个好的webapp的服务,支持android/ios/小程序打包,集成了大量本地api
    对前后端分离的一些经验记录
    「caffe编译bug」python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
    「caffe编译bug」 undefined reference to `boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11
    「caffe编译bug」.build_release/lib/libcaffe.so: undefined reference to cv::imread
    python的sorted函数对字典按value进行排序
  • 原文地址:https://www.cnblogs.com/Javi/p/6692254.html
Copyright © 2011-2022 走看看