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[];
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:\Users\cnjazhu\Desktop\KLChina Intranet\Training\石油和石油产品.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, , , 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:\Users\cnjazhu\Desktop\KLChina Intranet\Training\石油和石油产品.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;
}
}
OfficeToHtmlHelper的更多相关文章
随机推荐
- linux内存源码分析 - 内存池
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 内存池是用于预先申请一些内存用于备用,当系统内存不足无法从伙伴系统和slab中获取内存时,会从内存池中获取预留的 ...
- 面试笔记--Fast-Fail(快速失败)机制
1.解决: fail-fast机制,是一种错误检测机制.它只能被用来检测错误,因为JDK并不保证fail-fast机制一定会发生.若在多线程环境下使用fail-fast机制的集合,建议使用“java. ...
- Hive执行sql文件
方法1: hive -f sql文件 t.sql文件内容: ; 执行命令 hive -f t.sql 方法2: 进入hive shell, 执行source命令 进入hive 终端 $ hive hi ...
- AT1219 歴史の研究
附带权值的类区间众数问题?不是很好策啊 发现题目没有强制在线,而且也只有询问操作,那么可以考虑莫队 但是这里的莫队有一个很显著的特征,插入的时候很好维护答案,但是删除的时候不好回退 那么有没有什么办法 ...
- 【原创】三招搞死你的IE11,可重现代码下载(IE Crash keyframes iframe)!
前言 很多人都知道我们在做FineUI控件库,而且我们也做了超过 9 年的时间,在和浏览器无数次的交往中,也发现了多个浏览器自身的BUG,并公开出来方便大家查阅: 分享IE7一个神奇的BUG(不是封闭 ...
- Fiddler抓包和修改WebSocket数据,支持wss
记录一下用Fiddler对WebSocket收发的数据进行抓包分析和篡改数据,只找到这么一个方法,能用就行吧. 时间:2019-3-29 环境: win7 + Fiddler 5.0 Fiddler抓 ...
- 使用Python的http.server实现一个简易的Web Api对外提供HanLP拼音转换服务
由于采集省市区镇数据需要对地名进行拼音转换,由于第三方高准确度接口对IP进行了限制,处理大量数据变得异常缓慢. 使用了一个折中的办法,省市区 3级(3千+)用高准确度接口(几乎没有拼错的地名),镇级( ...
- 04 Docker/基础设施 - DevOps之路
04 Docker/基础设施 - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi Docker是一个开源的引擎,可 ...
- Minesweeper
你玩过扫雷吗?这个可爱的小游戏带有一个我们记不住名字的操作系统.游戏的目标是找到所有地雷在M x N场中的位置.游戏在一个正方形中显示一个数字,它告诉你在这个正方形附近有 多少个地雷.每个方块最多有八 ...
- matplotlib 入门之Usage Guide
文章目录 Usage Guide plotting函数的输入 matplotlib, pyplot, pylab, 三者的联系 Coding style Backends 后端 matplotlib教 ...