C#将Word,Excel与Html,PDF互转
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互转的更多相关文章
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- word/excel/ppt 2 PDF
PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...
- word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览
其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...
- DataTable导出为word,excel,html,csv,pdf,.txt
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- word,excel,ppt转pdf
第一步 需要下载jar包和jacob-1.14.3-x64.dll * <dependency> * <groupId>net.sf.jacob-project</gro ...
- PHP 实现 word/excel/ppt 转换为 PDF
前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...
- 21.PHP实现Word/Excel/PPT转换为PDF
参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
随机推荐
- 用SourceTree轻巧Git项目图解
用SourceTree轻松Git项目图解 这篇文档的目的是:让使用Git更轻松. 看完这篇文档你能做到的是: 1.简单的用Git管理项目. 2.怎样既要开发又要处理发布出去的版本bug情况. Sour ...
- 利用__index和__newindex实现默认值表、监控表、只读表(转)
__index和__newindex实际上相当于是在读写表的时候分别加了一道过滤的逻辑,让读写表的操作可以被监控或说回调,利用这个特性可以实现一些带有特殊功能的表. 带有默认值的表: setdefau ...
- C标签之forEach
<c:forEach>标签用于通用数据循环,它有下面属性 属 性 描 述 是否必须 缺省值 items 进行循环的项目 否 无 begin 開始条件 否 0 end 结束条件 否 集合中的 ...
- 8.Eclipse中创建Maven Web项目
第一步: 创建maven webproject 注意以下一步: 第二步: 继承parent 改动pom.xml文件例如以下 <projectxmlns="http://maven ...
- 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏
内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...
- 如何使盘ISO图像文件
原创作品.从 "深蓝blog" 博客,欢迎转载,请务必注明转载的来源,权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/articl ...
- 搜索引擎排名不友好的五个地点-SEO
搜索引擎(百度/谷歌/雅虎)排名不友好的五个地点 别的站点,推断标准和考核得分点是不 一样的,避免对百度排名不友好的五种站点操作 你的站点是否在这五种站点里,决定你的站点能否获得排 名. 1.有没有同 ...
- 3D数学--学习笔记(五岁以下儿童):总结一些概念(避免遗忘!)
下面是一些概念只是一个简单的解释,这里是它的一个简单的了解! 当人们谈论,我能理解有关. 1.正交投影: 投影.这意味着降维操作. 全部的点都被拉平至垂直的轴(2D)或平面(3D)上.这样的类型的投影 ...
- MiniGUI文档参考手册 基于v1.6.10文本
MiniGUI各种功能都分布在预先定义宏对每个文档标题.特别不方便查找,这是不利于初学者学习. 有一天,我发现doxygen,因此,使用该工具可以生成一个minigui参考文献 .基于v1.6.10文 ...
- atcoder它A Mountaineer
Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Problem Dave is a mountaineer. He is ...