博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Office转HTML
阅读量:4576 次
发布时间:2019-06-08

本文共 7440 字,大约阅读时间需要 24 分钟。

///         /// word转成html        ///         ///         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();            }        }        ///         /// word转成html带分页        ///         ///         ///         public static ResultDTO WordToHtmls(string path, string id)        {            FileInfo f = new FileInfo(path);            if (!f.Exists)                return null;            var basePath = path.Substring(0,path.IndexOf("DownLoads", StringComparison.Ordinal));            //转换文件根路径            string root = basePath + "DownLoads/Html/";            if (!Directory.Exists(@root + id))            {                Directory.CreateDirectory(@root + id);            }            Word.Document doc = null;            var pages = "";            Word.Application word = new Word.Application();            Type wordType = word.GetType();            Word.Documents docs = word.Documents;            Type docsType = docs.GetType();            try            {                object oMissing = System.Reflection.Missing.Value;                doc =(Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs,new Object[] {path, true, true});                bool flag = true;                int index = 1;                do                {                    object objWhat = Word.WdGoToItem.wdGoToPage;                    object objWhich = Word.WdGoToDirection.wdGoToAbsolute;                    object objPage = index;                    Word.Range range1 = doc.GoTo(ref objWhat, ref objWhich, ref objPage, ref oMissing);                    Word.Range range2 = range1.GoToNext(Word.WdGoToItem.wdGoToPage);                    object objStart = range1.Start;                    object objEnd = range2.Start;                    if (range1.Start == range2.Start)                    {                        objEnd = range2.StoryLength;                        flag = false;                    }                    doc.Range(ref objStart, ref objEnd).Copy();                    Word.ApplicationClass wordapp = new Word.ApplicationClass();                    Word.Document doc2 = wordapp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);                    Word.Paragraph para = doc2.Content.Paragraphs.Add(ref oMissing);                    para.Range.Paste();                    var pagepath = id + "/" + index + ".html";                    doc2.GetType().InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc2,                        new object[] {root + pagepath, Word.WdSaveFormat.wdFormatHTML});                    wordapp.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, wordapp, null);                    pages += pagepath + ",";                    index++;                } while (flag);                object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;                doc.Close(ref saveOption, ref oMissing, ref oMissing);                return new ResultDTO                {                    status = true,                    info = pages.Substring(0, pages.Length - 1)                };            }            catch (Exception e)            {                log.Error(e.Message);                return new ResultDTO                {                    status = false,                    info = e.Message                };            }            finally            {                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);                GC.Collect();                GC.WaitForPendingFinalizers();            }        }        ///         /// Excel转成html        ///         ///         ///         public static ResultDTO ExcelToHtml(string path,string id)        {            FileInfo f = new FileInfo(path);            if (!f.Exists)                return null;            var basePath = path.Substring(0, path.IndexOf("DownLoads", StringComparison.Ordinal));            //转换文件根路径            string root = basePath + "DownLoads/Html/";            if (!Directory.Exists(@root + id))            {                Directory.CreateDirectory(@root + id);            }            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 =id + "/" + id + ".html";                 object ofmt = Excel.XlFileFormat.xlHtml;                workbook.SaveAs(root + htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing); // 进行另存为操作                   return new ResultDTO                {                    status = true,                    info = htmlFile.ToString()                };            }            catch(Exception e)            {                log.Error(e.Message);                return new ResultDTO                {                    status = false,                    info = e.Message                };            }            finally            {                if (workbook != null)                {                    workbook.Close(true, Type.Missing, Type.Missing);                }                repExcel.Quit();                GC.Collect();                GC.WaitForPendingFinalizers();                GC.Collect();                GC.WaitForPendingFinalizers();            }        }    }    ///     /// 输出结果    ///     public class ResultDTO    {        public bool status; //状态        public string info; //详情    }
注:此处获取文档页数不可用以下方法获取,因为它表示的页数是指页面视图的页大小,并不是页面视图的页大小。
              
  Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
                file_pages = doc.ComputeStatistics(stat, ref missing);

转载于:https://www.cnblogs.com/xuhang/p/5402465.html

你可能感兴趣的文章
万径人踪灭(FFT+manacher)
查看>>
技术规格说明书
查看>>
图写成一个类(2)
查看>>
Segmentation fault (core dumped) 错误的一种解决场景
查看>>
hdu1150 Machine Schedule (匈牙利算法模版)
查看>>
惠普 hpssacli 工具使用
查看>>
Solr综合案例深入练习
查看>>
关于strcpy的实现.
查看>>
mysql新建表示,时间字段timetamp碰到的问题
查看>>
在 RHEL/CentOS 7 上配置NTP时间服务器
查看>>
链表:创建一个简单的链表并输出链表内容
查看>>
python 集中基本数据类型
查看>>
may be a diary?
查看>>
洛谷.2590.[ZJOI2008]树的统计(树分块)
查看>>
实验一:JAVA实验环境搭建
查看>>
Abbreviation of job titles
查看>>
帝国cms灵动标签下常用标签
查看>>
STL学习笔记(关联式容器)
查看>>
Android生成xml
查看>>
python入到到实战--第十章----文件
查看>>