代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
doc、xls、ppt转换html代码
作者:
Diana
/ 发布于
2015/12/9
/
1058
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using Microsoft.Office.Interop.Excel; using System.Diagnostics; using Microsoft.Office.Interop.PowerPoint; using Microsoft.Office; using System.Reflection; /// <summary> /// FileHtml 的摘要说明 /// </summary> public class FileHtml { //public static string FileToHtml(string filePath) public static string FileToHtml(FileUpload FileUpload1) { string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); string strss = "|doc|docx|"; string strss2 = "|xls|xlsx|"; string strss3 = "|ppt|pptx|"; if (strss.IndexOf("|" + fileExtension.Substring(1) + "|") > -1) { return wordToHtml(FileUpload1); } else if (strss2.IndexOf("|" + fileExtension.Substring(1) + "|") > -1) { return ExcelToHtml(FileUpload1); } else if (strss3.IndexOf("|" + fileExtension.Substring(1) + "|") > -1) { return PPTToHtlm(FileUpload1); } else { return ""; } } /// <summary> /// /// </summary> /// <param name="filePath">上传后文件的路径</param> /// <returns></returns> public static string wordToHtml(FileUpload FileUpload1) { try { Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); Type wordType = word.GetType(); Microsoft.Office.Interop.Word.Documents docs = word.Documents; Type docsType = docs.GetType(); FilesOperate fo = new FilesOperate(); //定义服务器doc存储地址 string wordPath = System.Web.HttpContext.Current.Server.MapPath(fo.GetOriginalFullName(System.IO.Path.GetExtension(FileUpload1.FileName).ToLower().Substring(1))); //保存至服务器 FileUpload1.SaveAs(wordPath); object fileName1 = wordPath; Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName1, true, true }); // 转换格式,另存为html Type docType = doc.GetType(); string filename = fo.GetNewFileName() + ".html"; string fpath = fo.GetConvertDirectory(); string path = (System.Web.HttpContext.Current.Server.MapPath(fpath)); Directory.CreateDirectory(path); //被转换的html文档保存的位置 object saveFileName = path + filename; docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); // 退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); //返回文件名 return (fpath + filename); } catch (Exception ex) { return ex.ToString(); } } /// <summary> /// /// </summary> /// <param name="filePath">上传后文件的路径</param> /// <returns></returns> public static string PPTToHtlm(FileUpload FileUpload1) { try { FilesOperate fo = new FilesOperate(); //定义服务器doc存储地址 string wordPath = System.Web.HttpContext.Current.Server.MapPath(fo.GetOriginalFullName(System.IO.Path.GetExtension(FileUpload1.FileName).ToLower().Substring(1))); //保存至服务器 FileUpload1.SaveAs(wordPath); Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application(); string filename = fo.GetNewFileName() + ".html"; string fpath = fo.GetConvertDirectory(); string path = (System.Web.HttpContext.Current.Server.MapPath(fpath)); Directory.CreateDirectory(path); string saveFileName = path + filename; Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(wordPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); //被转换的html文档保存的位置 prsPres.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTrue); prsPres.Close(); ppApp.Quit(); return (fpath + filename); } catch (Exception ex) { return ex.ToString(); } } /// <summary> /// /// </summary> /// <param name="filePath">上传后文件的路径</param> /// <returns></returns> public static string ExcelToHtml(FileUpload FileUpload1) { try { FilesOperate fo = new FilesOperate(); //定义服务器doc存储地址 string wordPath = System.Web.HttpContext.Current.Server.MapPath(fo.GetOriginalFullName(System.IO.Path.GetExtension(FileUpload1.FileName).ToLower().Substring(1))); //保存至服务器 FileUpload1.SaveAs(wordPath); string filename = fo.GetNewFileName() + ".html"; string fpath = fo.GetConvertDirectory(); string path = (System.Web.HttpContext.Current.Server.MapPath(fpath)); Directory.CreateDirectory(path); Directory.CreateDirectory(path); //被转换的html文档保存的位置 object saveFileName = path + filename.Replace(":", "-"); Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = false; object o = System.Reflection.Missing.Value; _Workbook xls = app.Workbooks.Open(wordPath, o, true, o, o, o, o, o, o, o, o, o, o, o, o); object fileName = saveFileName; object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;//Html xls.SaveAs(fileName, format, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o); app.Quit(); return fpath + filename.Replace(":", "-"); } catch (Exception ex) { return ex.ToString(); } } }
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
Diana
贡献的其它代码
(
6
)
.
动态sql语句基本语法
.
doc、xls、ppt转换html代码
.
Grdiview 查找自定义列中的控件,并改变其属性
.
Gridview 行绑定的时候读取datakeynames中的数据源字
.
HTTP请求类, 已封装get,post,delete,put
.
查找jar中的pom文件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3