c# word excel text转html的方法

#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
        #region 预览Word
        /// <summary>
        /// 预览Excel
        /// </summary>
        /// <param name="physicalPath">文件物理路径</param>
        /// <param name="url">文件夹物理路径</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 预览Txt
        /// <summary>
        /// 预览Txt
        /// </summary>
        /// <param name="physicalPath">文件物理路径</param>
        /// <param name="url">文件夹物理路径</param>
        public string PreviewTxt(string physicalPath, string url)
        {
            FileStream aFile = new FileStream(physicalPath, FileMode.Open);
            //暂时不知道为什么获取的编码方式会导致读取的内容为空
            //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>文本阅读</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());  //这里是写入的内容
                sw.Flush();
                sw.Close();
            }
            sr.Close();
            var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(url + htmlName);
            return resultRul;
        }
 /// <summary>
        /// 转换成相对路径
        /// </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, ""); //转换成相对路径
            imagesurl2 = imagesurl2.Replace(@"\", @"/");
            return imagesurl2;
        }

其中excel转html的方法,需要添加Microsoft.Office.Interop.Excel Nuget包,然后引用Microsoft Excel 12.0 Object Library.dll

猜你喜欢

转载自www.cnblogs.com/yuchenghao/p/8954010.html
今日推荐