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. JavaHTTP下载视频

    控制层类: package com.grab.video.controller; import java.io.BufferedOutputStream; import java.io.Buffere ...

  2. partial 的好处

    1.可以将一个类中的属 性, 方法分类来写 2.方法了可以写在多个类中, 这样可以对方法进行分类   由于项目上使用了代码生成工具, 自定义的一些按钮事件默认是不生成的,得自己定义,如果把定义的代码写 ...

  3. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  4. MySQL各种日期类型与整型(转)

    日期类型 存储空间 日期格式 日期范围 datetime 8 bytes YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 t ...

  5. ASP.NET自定义控件组件开发 第一章 第三篇

    原文:ASP.NET自定义控件组件开发 第一章 第三篇 第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待 ...

  6. int a[5]={1,2,3,4,5};printf(&quot;%d\n&quot;, *((int*)(&amp;a+1)-2);

    有说服力的笔试题有一定的期限,问:什么是结果,答案是4,为什么要挤? 我明白(不知道是不正确): &a这是一个数组指针,类型int[5],然后&a添加1其实a+sizeof(int)* ...

  7. JListDemo

    Tips: (1)JList不能自动滚动,要想为列表框加上滚动条,必须将JList插入到一个JScrollPane中,然后将JScollPane而不是JList,插入到外围JPanel上 (2)Lis ...

  8. JPA实体继承映射

    注意:据说,在本文所指的实体是@Entity注解的类. JPA在对象 - 关系映射通常情况下一个实体对应表,不管是什么这两个实体之间的关系.假设两个实体之间的继承关系.那么它是如何映射? JPA实体支 ...

  9. iOS UITableViewCell AccessoryType属性

    写了这么长时间的ios table 竟然不知道有一个Accessory来显示详情设置小图标,曾经竟然傻帽的取用一张图片来显示,如今想想真是..那痛啊.... cell.accessoryType = ...

  10. NSIS:在线下载并安装程序

    原文 NSIS:在线下载并安装程序 看到有同学留言说需要这方面的代码,所以贴出以下代码供参考(非完整脚本).需要用NSISdl插件. Section -.NET Framework    NSISdl ...