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