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 ...
随机推荐
- RabbitMq初探——发布与订阅
publish and subscribe 前言 前面的例子 我们都是用到的都是消息单一消费,即一条消息被单个消费者消费.像微博系统的消息推送,是一条消息推送给所有订阅到该频道的用户. 这里我们就需要 ...
- python中的列表和元组
1. 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求,列表存在索引和切片. 和字符串是一样的. 2.相关的增删改查操作 切片 列表和 ...
- API接口安全加强设计方法
前面两篇相关文章: <Web Api 内部数据思考 和 利用http缓存优化 Api> <Web Api 端点设计 与 Oauth> 1.开放的接口 这样的接口我们天天都在接触 ...
- Java多线程(汇聚页)
Java多线程(汇聚页) Java多线程总结
- JS 返回上一页并刷新,但不用重新加载整个页面(ajax实现)
需求 有三个页面A.B.C,点击A=>B,点击B=>C,在C中添加内容,点击确定返回到B,此时B页面需重新加载新的内容.再次点击B的返回按钮,希望返回到A而不是C. ===== 2017/ ...
- linux中pipe和dup2详解
1.什么是管道 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管道: 只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程): 单独构成一种独立的文件系统:管道对于管道两端的进 ...
- C#-WebForm-LinQ-条件精确查询、高级查询
前台界面,并在后台绑定数据 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ca ...
- [Alpha]Scrum Meeting#8
github 本次会议项目由PM召开,时间为4月10日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写每日例会报告 撰写每日例会报告 SiMrua 优化模型速度(iss ...
- AI简单平移追踪算法
1.比较坐标追踪法 追踪者会不停地比较自身和目标的x坐标和y坐标,每x和y上一个单位的移动为一个周期,该算法虽然简单好用,但实用性差且不智能化,如果追踪者数量增加,路线会显得单调,由于都是先走个对角线 ...
- The Ugly Duckling
THE UGLY DUCKLING Chapter 1: The Ugly Duckling Hatches A mother duck sits on many eggs.Crack! Cr ...