word转html实现预览(asp.net)
word转html
需要通过nuget 安装
Microsoft.Office.Interop.Word Microsoft.Office.Interop.Excel
使用 Microsoft.AspNetCore.Hosting;获取绝对的物理地址,将生成的html放到对应的物理地址上
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Word;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using Microsoft.AspNetCore.Hosting; namespace wordTest.Controllers
{
public class HomeController : Controller
{
//依赖注入获取绝对物理地址的对象
private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
#region Index页面
/// <summary>
/// Index页面
/// </summary>
/// <paramname="url">例:/uploads/......XXX.xls</param>
public ActionResult Index(string url)
{
//绝对定位资源文件夹(wwwroot)下到D://.../wwwroot/
string webRootPath = _hostingEnvironment.WebRootPath;
//绝对定位到项目根目录下D://.../
string contentRootPath = _hostingEnvironment.ContentRootPath;
url = "/files/charts.docx";
string physicalPath = webRootPath + url;
//获取后缀名
string extension = Path.GetExtension(physicalPath); string htmlUrl = "";
switch (extension.ToLower())
{
case ".xls":
case ".xlsx":
htmlUrl = PreviewExcel(physicalPath, url);
break;
case ".doc":
case ".docx":
htmlUrl = PreviewWord(physicalPath, url);
break;
case ".txt":
htmlUrl = PreviewTxt(physicalPath, url);
break;
case ".pdf":
htmlUrl = PreviewPdf(physicalPath, url);
break;
case ".jpg":
case ".jpeg":
case ".bmp":
case ".gif":
case ".png":
htmlUrl = PreviewImg(physicalPath, url);
break;
default:
htmlUrl = PreviewOther(physicalPath, url);
break;
}
//跳转到转换后的html页面(转换后的页面)
return Redirect(Url.Content(htmlUrl));
}
#endregion #region 预览Excel
/// <summary>
/// 预览Excel
/// </summary>
public string PreviewExcel(string physicalPath, string url)
{
Microsoft.Office.Interop.Excel.Application application = null;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
application = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
object trueObject = true;
application.Visible = false;
application.DisplayAlerts = false;
workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
//Save Excelto Html
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
workbook.SaveAs(outputFile, format, missing, missing, missing,
missing, XlSaveAsAccessMode.xlNoChange, missing,
missing, missing, missing, missing);
workbook.Close();
application.Quit();
return Path.GetDirectoryName(WebUtility.UrlDecode(url)) + "\\" + htmlName;
}
#endregion #region 预览Word
/// <summary>
/// 预览Word
/// </summary>
public string PreviewWord(string physicalPath, string url)
{
Microsoft.Office.Interop.Word._Application application = null;
Microsoft.Office.Interop.Word._Document doc = null;
application = new Microsoft.Office.Interop.Word.Application();
object missing = Type.Missing;
object trueObject = true;
application.Visible = false;
application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//Save Excelto Html
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
doc.SaveAs(outputFile, format, missing, missing, missing,
missing, XlSaveAsAccessMode.xlNoChange, missing,
missing, missing, missing, missing);
doc.Close();
application.Quit();
return Path.GetDirectoryName(WebUtility.UrlDecode(url)) + "\\" + htmlName;
}
#endregion #region 预览Txt
/// <summary>
/// 预览Txt
/// </summary>
public string PreviewTxt(string physicalPath, string url)
{
return WebUtility.UrlDecode(url);
}
#endregion #region 预览Pdf
/// <summary>
/// 预览Pdf
/// </summary>
public string PreviewPdf(string physicalPath, string url)
{
return WebUtility.UrlDecode(url);
}
#endregion #region 预览图片
/// <summary>
/// 预览图片
/// </summary>
public string PreviewImg(string physicalPath, string url)
{
return WebUtility.UrlDecode(url);
}
#endregion #region 预览其他文件
/// <summary>
/// 预览其他文件
/// </summary>
public string PreviewOther(string physicalPath, string url)
{
return WebUtility.UrlDecode(url);
}
#endregion
}
}
注意,使用的office不兼容asp.netCore2.1版本变成netFramework版本
word转html实现预览(asp.net)的更多相关文章
- asp.net如何实现word文档在线预览
原文:asp.net如何实现word文档在线预览 实现方式:office文档转html,再在浏览器里面在线浏览 1.首先引入com组件中office库,然后在程序集扩展中引入word的dll 2.将M ...
- word文档在线预览解决方案
花了一整天在网上翻关于 “word文档在线预览解决方案” 相关的资料,感觉实现难度比较大还是用PDF来解决好了.. 下面列一下比较好的参考资料吧 参考资料 前端实现在线预览pdf.word.xls.p ...
- Java实现word文档在线预览,读取office文件
想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...
- word,excel,ppt在线预览功能
我们在开发web项目时,尤其类似oa功能时总会遇到上传附件并在线预览的功能,发现一款api比较好使,下面简单介绍一下. 微软官网本身提供了在线预览的API 首先将要预览的文档转成.docx,.xlsx ...
- java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)
背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...
- java 实现word文档在线预览
一.准备工具 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 下载地址:http://www.openoffice.org/download/in ...
- SpringBoot+FreeMarker开发word文档下载,预览
背景: 开发一个根据模版,自动填充用户数据并下载word文档的功能 使用freemarker进行定义模版,然后把数据进行填充. maven依赖: <parent> <groupId& ...
- 关于pc端 app端pdf,word xls等文件预览的功能
第一种用H5标签<iframe>标签实现 返回的文件类型,文件流,文件流返回必须在设置 contentType对应的Mime Type, 返回文件的物理位置. 已经实测可以支持的文件类型 ...
- word文档在线预览地址
文档网址 http://www.officeweb365.com/Default/Docview 对接 http://ow365.cn/?i=19604&furl=http:://www.ba ...
随机推荐
- ZooKeeper学习2---ZooKeeper安装配置
一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境:■ 伪集群模式:就是在一台物 ...
- IDEA 引入外部jar包 pom 配置,防止打包失败
1. <!--添加外部依赖--> <dependency> <groupId>Ice</groupId> ...
- Google 推出新搜索引擎以查找数据集
简评:谷歌推出了一个用于寻找数据集的新搜索引擎,有点厉害! 该工具可以更轻松地访问 Web 上数千个数据存储库中的数百万个数据集,当前还处于测试版: 什么是 Dataset Search? 数 ...
- 利用python 学习数据分析 (学习一)
内容学习自: Python for Data Analysis, 2nd Edition 就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...
- leetcode-91-解码方法(动态规划和递归两种解法)
题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...
- Asp.net的生命周期之页生命周期
参考:http://msdn.microsoft.com/zh-cn/library/ms178472%28v=vs.100%29.aspx http://msdn.microsoft.com/zh- ...
- 2016级算法第六次上机-G.ModricWang likes geometry
1116 ModricWang likes geometry 思路 难题,非常考察几何知识,放在这里作为计算几何场次的最难的题. 原题地址 原版题解 代码
- 能够放在文档的 <head> 中的各种配置元素
一份关于任何可以写入到你的文档中 <head> 部分的清单 最小推荐 <meta charset="utf-8"> <meta http-equiv= ...
- driver failed programming external connectivity on endpoint wordpress
docker run 镜像的时候报错: [root@docker ~]# docker run -itd --name wordpress -p 88:80 wordpress:v1b77482f80 ...
- springboot自定义jmx对象
在使用springboot-admin对springboot项目进行监控的时候我们发现其是具有web访问jmx对象的功能的,那它内部是怎么实现的呢. Jolokia是一个JMX-http桥梁,它提供了 ...