/// <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(, 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转成html带分页
/// </summary>
/// <param name="path"></param>
/// <param name="id"></param>
public static ResultDTO WordToHtmls(string path, string id)
{
FileInfo f = new FileInfo(path);
if (!f.Exists)
return null; var basePath = path.Substring(,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 = ;
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(, pages.Length - )
};
}
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();
}
} /// <summary>
/// Excel转成html
/// </summary>
/// <param name="path"></param>
/// <param name="id"></param>
public static ResultDTO ExcelToHtml(string path,string id)
{
FileInfo f = new FileInfo(path);
if (!f.Exists)
return null; var basePath = path.Substring(, 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();
}
}
} /// <summary>
/// 输出结果
/// </summary>
public class ResultDTO
{
public bool status; //状态
public string info; //详情
}
注:此处获取文档页数不可用以下方法获取,因为它表示的页数是指页面视图的页大小,并不是页面视图的页大小。
                Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
                file_pages = doc.ComputeStatistics(stat, ref missing);

Office转HTML的更多相关文章

  1. .NET Core 首例 Office 开源跨平台组件(NPOI Core)

    前言 最近项目中,需要使用到 Excel 导出,找了一圈发现没有适用于 .NET Core的,不依赖Office和操作系统限制的 Office 组件,于是萌生了把 NPOI 适配并移植到 .NET C ...

  2. [.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office

    打造一个很简单的文档转换器 - 使用组件 Spire.Office [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6024827.html 序 之前,& ...

  3. 高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)

    在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是”这个系统似乎就是专门操作文档的“.毕竟现在的很多办公中大都是在PC端操作文档等软件,在这些庞大而繁重的业务中,单 ...

  4. 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览

    条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上)     实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...

  5. RMS:Microsoft Office检测到您的信息权限管理配置有问题。有关详细信息,请与管理员联系。(转)

    原文:https://zhidao.baidu.com/question/435088233.html RMS有两种方式: 1.使用微软的服务器,这个是连接到微软的服务器上面做权限控制,在今年5月份之 ...

  6. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  7. 针对每种Windows Server 操作Excel、Word等Office组件遇到“ComException"、”80070005“等COM错误的解决方案大汇总

    以下所有Excel错误的解决方案,同样适用于Word.PowerPoint等Office产品. 以下解决方案中,如果出现"安装Excel组件",是适用于遇到Excel错误的.如果是 ...

  8. 解决NTKO Office中文文件名保存到服务器时出现乱码的问题

    再使用NTKO office控件时,在ntko往服务器提交文件时,中文文件名会出现乱码的问题! 其实解决这个问题可以换一种思路,在ntko往服务器提交文件时英文肯定是不会出现乱码的问题的! 那么想办法 ...

  9. office 2010 word每次启动都需要配置

    解决方式: 进入cmd,运行以下命令即可,如果提示已存在,选择Y覆盖就行了 reg add HKCU\Software\Microsoft\Office\14.0\Word\Options /v No ...

  10. 非域客户端的office使用RMS加密服务出现‘介绍“信息权限管理服务”’服务的提示

    环境:office2007,需要使用windows RMS服务,客户机处于工作组模式,如图: 出现这个说明客户机没有发现RMS服务,可以通过导入注册表解决,如下: Windows Registry E ...

随机推荐

  1. MSSQL连接字符串,你真的清楚吗?

    原文:MSSQL连接字符串,你真的清楚吗? 几年前当我第一次面试时,考官发现我是个新手于是他让我写个连接字符串,虽然当时就知道X种连接字符串的写法,但是当时却没能写对一个,工作多年后我仍然不能写一个正 ...

  2. ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

  3. MY WAY程序(十三) 理念和技术

    背部,该项目团队去了一半多,我们出差.我将离开之前,闪亮强哥给了我学习技术的列表,以了解它:AngularJs,bootsrap,smartadmin,html5,css3.很多前景的技术.哎,学吧, ...

  4. ubuntu 下搭建apache+python的运行环境

    ubuntu下怎么搭建apache+python运行环境,可以参考http://www.01happy.com/ubuntu-apache-mod-python/ ,这里只是简单的记录下步骤,本文主要 ...

  5. JBoss配置解决高并发连接异常问题(转)

    这两天一个项目在做压力测试的时候,发现只要并发数超过250个,连续测试两轮就会有连接异常出现,测试轮数越多出现越频繁,异常日志如下: Caused by: com.caucho.hessian.cli ...

  6. [Python学习] 模块三.基本字符串

            于Python最重要的数据类型包含字符串.名单.元组和字典.本文重点介绍Python基础知识. 一.字符串基础         字符串指一有序的字符序列集合,用单引號.双引號.三重(单 ...

  7. Android docs4.3API

    查找在线课程,加速进入Android docs API,最主要的原因是网上加载js文件速度慢,另一种是装google字体缓慢! import java.io.BufferedReader; impor ...

  8. Cocos2d Lua 越来越小样本 内存游戏

    1.游戏简介 一个"记忆"类的比赛游戏.你和电脑对战,轮到谁的回合,谁翻两张牌,假设两张牌一样.就消掉这两张牌,得2分,能够继续翻牌,假设两张牌不一样,就换一个人.直到最后.看谁的 ...

  9. Grant的时候报错的解决:Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)

    mysql> grant all on *.* to 'root'@'192.168.1.1' identified by 'password'; ERROR 1045 (28000): Acc ...

  10. 移动端 常见问题整理 iOS下的 Fixed + Input 调用键盘的时候fixed无效问题解决方案

    使用iScroll时,input等不能输入内容的解决方法 <script> function allowFormsInIscroll(){ [].slice.call(document.q ...