#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 Excel to 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(); var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(url + htmlName); return resultRul; } #endregion #regionPreview Word /// <summary> /// Preview Excel /// </summary> /// <param name="physicalPath"> File physical path </param> /// <param name=" url"> Folder physical path </param> /// <returns></returns> 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 Excel to 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, missing, missing, missing, missing, missing, missing); doc.Close(); application.Quit(); var resultRul= "http://"+HttpContext.Current.Request.Url.Authority +"/"+ urlconvertor(url + htmlName); return resultRul; } #endregion #region Preview Txt /// <summary> /// Preview Txt /// </summary> /// <param name="physicalPath"> File physical path </param> /// <param name="url" > Folder physical path </param> public string PreviewTxt( string physicalPath, string url) { FileStream aFile = new FileStream(physicalPath, FileMode.Open); // I don't know why the obtained encoding method will cause the read content to be empty // Encoding codeType = GetType(aFile); StreamReader sr = new StreamReader(aFile, Encoding .GetEncoding( " GB2312 " )); var content= sr.ReadToEndAsync().Result.Replace( " \r\n " , " </br> " ); string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + " .html " ; if (!File.Exists(url+htmlName)) { FileStream fs = new FileStream(url + htmlName, FileMode.CreateNew); StreamWriter sw = new StreamWriter(fs,Encoding.UTF8); StringBuilder sb = new StringBuilder(); sb.Append(@"<!DOCTYPE html> <html lang = 'zh-CN'><head> <meta http - equiv = 'Content-Type' content = 'text/html; charset=UTF-8'> <meta http - equiv = 'X-UA-Compatible' content = 'IE=edge'> <meta name = 'viewport' content = 'width=device-width, initial-scale=1'> <title>Text reading</title> <style> .content { width:60%; height:auto; margin:0 auto; border:1px solid #333; padding:10px 20px; } </style> </head>"); sb.Append(@"<body><div class='content'>"); sb.Append(@"<p>"); sb.Append(content); sb.Append(@"</p></div>"); sb.Append(@"</body>"); sw.Write(sb.ToString()); // here is the written content sw.Flush(); sw.Close(); } sr.Close(); var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(url + htmlName); return resultRul; } /// <summary> /// Convert to relative path /// </summary> /// <param name="imagesurl1"></param> /// <returns></returns> private string urlconvertor( string imagesurl1) { string tmpRootDir = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录 string imagesurl2 = imagesurl1.Replace(tmpRootDir, "" ) ; // Convert to relative path imagesurl2 = imagesurl2.Replace( @" \ " , @" / " ); return imagesurl2; }
The method of converting excel to html requires adding the Microsoft.Office.Interop.Excel Nuget package, and then referencing Microsoft Excel 12.0 Object Library.dll