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版本

 

  

转载于:https://www.cnblogs.com/xuqp/p/9711684.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值