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. C++11:强类型枚举(enum)

    // C++11之前的enum类型是继承C的,不温不火: // C++11对enum动刀了,加强了类型检查,推出强类型enum类型,眼前一亮 // 使用过QT 的都知道,早就应该这么做了,用的非常爽! ...

  2. 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

    原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...

  3. Nginx各版本的区别

    Nginx官网提供了三个类型的版本Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版Stable version:最新稳定版,生产环境上建议使用的版 ...

  4. 移动开发中Fiddler的那些事儿 (转)

    当我以前在调PC端网页的时候,Firefox有Firebug,Chrom也有自己的调试抓包工具,用起来很方便. 但是现在我要在手机上面调试页面,这个时候就没有这么方便的现成工具了. 后面发现了Fidd ...

  5. 集成ejs和angular

    我们也有一个系统angular用在应用中单页.正确angular做一些定制.集成seajs.不实用angular自己的模块管理. 只要angular也可单独使用在,一个较小的系统新开发,我不会用前js ...

  6. Unity3D合并着色器

    unity 3d倒每次模型更多的是一种着色器.我可以拥有这些车型共享的地图想分享一个着色器.所以每次删除,然后附加,很麻烦.如何才能合并这些着色器? 采纳TexturePacking对 1.遍历gam ...

  7. Windows 事件查看器(收集)

    原文:Windows 事件查看器(收集) 事件查看器相当于一本厚厚的系统日志,可以查看关于硬件.软件和系统问题的信息,也可以监视 Windows 的安全事件 提示:除了可以在"控制面板→管理 ...

  8. boostrap-非常好用但是容易让人忽略的地方------modal

    使用bootstrap框架好久了,在开发中也用到了或者遇到了很多的问题,所以跟大家分享一下 bootstrap modal 组件的样式 .modal-lg .modal-sm 说明:这个是bootst ...

  9. [LeetCode] Longest Palindrome Substring 具体分析

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  10. 泛泰A900 刷4.4中国民营TWRP2.7.1.1版本 支持自己主动识别移动版本号(世界上第一)

    因本人手上的A900S已砖, 所以临时弄不了ROM了. 先上传之前已经弄好的刷4.4专用的新版TWRP recovery 2.7.1.1  这个版本号是我自己定义的,为差别之前公布的2.7.0.0版( ...