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(, 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() + ".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(, 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(, 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();
}
}
}

C#将Word,Excel与Html,PDF互转的更多相关文章

  1. Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

    Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...

  2. word/excel/ppt 2 PDF

    PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...

  3. word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

    其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...

  4. DataTable导出为word,excel,html,csv,pdf,.txt

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  5. word,excel,ppt转pdf

    第一步 需要下载jar包和jacob-1.14.3-x64.dll * <dependency> * <groupId>net.sf.jacob-project</gro ...

  6. PHP 实现 word/excel/ppt 转换为 PDF

    前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...

  7. 21.PHP实现Word/Excel/PPT转换为PDF

    参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...

  8. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

  9. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

随机推荐

  1. NET WEB

    .NET WEB程序员需要掌握的技能 2015-12-28 08:50 by 敏捷的水, 3997 阅读, 66 评论, 收藏, 编辑 本来这个是我给我们公司入职的新人做一个参考,由于 @张善友 老师 ...

  2. html 格式的email 编辑

    本篇文章只讲如何编辑html格式的email 模板,并不讲述如何用程序发送email. 1.做email的重要思想:“复古” 抛弃现代化的div+css技术,回到html4.0+table的时代.少用 ...

  3. 最简单的视音频播放演示样例8:DirectSound播放PCM

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  4. 解决Fedora升级时nvidia显卡问题

    ​ 升级到新版Fedora后登录不了gnome 小编最近升级了Fedora 20到21,结果就如之前从Fedora 19升级到20时类似,又出问题了.Fedora你到底行不行... gnome登录不了 ...

  5. bash构造tmux显示tmux ssh状态

    要求: 于tmux内部链接ssh什么时候, 假设有多个ssh主机. 我们要显示相应的主机ip要显示筛查 实现效果 编辑你的bashrc, 由于我习惯在root下操作, 所以/root/.bashrc. ...

  6. hive内置函数大全

    ====================================== 一.关系函数 1.等值比較:=     语法:A=B 操作类型:全部基本类型 2.不等值比較:<>     语 ...

  7. DuiVision开发教程(15)-DUI文本控制基础类

    CControlBaseFont类是DuiVision支持所有基类的控件的文本属性. 此控件例如属性列表,下面: 物业名称 类型 说明 title 字符串 控件的显示标题 font 字体 控件的字体, ...

  8. 设计模式模式游客(Visitor)摘录

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化托付给还 ...

  9. UVA11396-Claw Decomposition(二分图判定)

    题目链接 题意:能否将一张无向连通图分解成多个爪型.每一条边仅仅能属于一个爪型,每一个点的度数为3. 思路:当图分解成类干个爪型时,每条边仅仅属于一个爪子,所以每条边的两个点一定要处于2个不同的鸡爪中 ...

  10. 达到HTTP合约Get、Post和文件上传功能——采用WinHttp介面

    于<采用WinHttp实现HTTP协议Get.Post和文件上传功能>一文中,我已经比較具体地解说了怎样使用WinHttp接口实现各种协议. 在近期的代码梳理中,我认为Post和文件上传模 ...