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)的更多相关文章

  1. asp.net如何实现word文档在线预览

    原文:asp.net如何实现word文档在线预览 实现方式:office文档转html,再在浏览器里面在线浏览 1.首先引入com组件中office库,然后在程序集扩展中引入word的dll 2.将M ...

  2. word文档在线预览解决方案

    花了一整天在网上翻关于 “word文档在线预览解决方案” 相关的资料,感觉实现难度比较大还是用PDF来解决好了.. 下面列一下比较好的参考资料吧 参考资料 前端实现在线预览pdf.word.xls.p ...

  3. Java实现word文档在线预览,读取office文件

    想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...

  4. word,excel,ppt在线预览功能

    我们在开发web项目时,尤其类似oa功能时总会遇到上传附件并在线预览的功能,发现一款api比较好使,下面简单介绍一下. 微软官网本身提供了在线预览的API 首先将要预览的文档转成.docx,.xlsx ...

  5. java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

  6. java 实现word文档在线预览

    一.准备工具 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 下载地址:http://www.openoffice.org/download/in ...

  7. SpringBoot+FreeMarker开发word文档下载,预览

    背景: 开发一个根据模版,自动填充用户数据并下载word文档的功能 使用freemarker进行定义模版,然后把数据进行填充. maven依赖: <parent> <groupId& ...

  8. 关于pc端 app端pdf,word xls等文件预览的功能

    第一种用H5标签<iframe>标签实现 返回的文件类型,文件流,文件流返回必须在设置 contentType对应的Mime Type, 返回文件的物理位置. 已经实测可以支持的文件类型 ...

  9. word文档在线预览地址

    文档网址 http://www.officeweb365.com/Default/Docview 对接 http://ow365.cn/?i=19604&furl=http:://www.ba ...

随机推荐

  1. Gogland编译LiteIDE工程需要注意问题!

    致歉声明:实在抱歉,因为自己对Go语言和Gogland的不熟悉,导致错误判断!无论LiteIDE和Goland都可以顺利使用同一个包下的其它文件内容!!Go语言本身就允许把一个包拆分成不同的文件,下面 ...

  2. Delphi XE7实现的任意位置弹出菜单

    Delphi XE7中目前还没有弹出菜单组件,这个弹出菜单应用很普遍,在JAVA开发的安卓程序中很简单就可以用上了,应该说是一个标准控件.看了一些例子,但是都不能满足我想在任意位置弹出菜单需求,于是自 ...

  3. ASP.NET MVC 发布后 IE 访问出现布局错乱问题

    ASP.NET MVC 网页debug启动跑一切正常,[Chrome],[FireFox],[Edge],[IE11] 发布后,使用机器名访问,[IE11]出现布局不正常的问题, 在head里:加↓可 ...

  4. Luogu P1951 收费站_NOI导刊2009提高(2)

    二分答案+堆优Dijkstra 这个题有些巧妙. 首先,因为要在油量耗完之前跑到终点,所以我们可以用最短路.只要从\(s\)出发到\(t\),它的最短距离大于油量,我们就可以断定它一定走不通,直接输出 ...

  5. SCOI2019 游记

    写在前面 其实冬令营之后就有一些想说的内容,由于心情原因没有写出来.PKUWC 失误频频,唯一可能还有点价值的就是 Day2T3 计算几何推了 76 分出来.NOIWC 更是无心再谈,感觉是被提答送走 ...

  6. BZOJ_3224 Tyvj 1728 普通平衡树 【离散化+权值线段树】

    一 题面 Tyvj 1728 普通平衡树 二 分析 比较明显是可以用平衡二叉搜索树(splay)做的. 用权值线段树做,前提就是要先离散化,因为权值线段树维护的值域信息. 板子. 三 AC代码 #in ...

  7. windows下python3.7.2内置venv虚拟环境下pyinstaller错误问题

    起因 开发一直使用python -m venv .pyenv 方式创建虚拟环境,在利用pyinstaller打包发布应用时,出现错误 3178 INFO: Warnings written to C: ...

  8. Unity3D碰撞触发函数

    首先要给被碰撞物体添加Box Collider,并在Is Trigger打勾 在被碰撞物体的脚本里,添加碰撞检测函数 void OnTriggerEnter(Collider other) { if ...

  9. Mac 10.12高级防火墙pfctl教程收集(待实践)

    在Mac 10.10之前使用ipfw.之后升级为pfctl.由于Mac自带的GUI防火墙只能针对软件进行放开,且无法针对特定端口入站及出站进行管理.所以使用pfctl能解决这类问题. 收集教程如下: ...

  10. codeblocks中文编码问题

    其实这是老调重弹的问题了,在windows下面出现中文乱码大多都是编码格式的问题不一致的问题,最简单的就是uft-8和gbk冲突的问题.如果一个文件本来是以utf-8存的,但是以gbk打开,当然会出现 ...