使用Aspose把office文件轉換爲Html文件及生成文件瀏覽亂碼的解決

使用Aspose把office文件轉換爲Html 文件轉換方法如下:

        /// <summary>
        /// office文件轉換爲Html
        /// </summary>
        /// <param name="extFileName">擴展名</param>
        /// <param name="sourceFile">源文件路徑</param>
        /// <param name="desFile">目標文件</param>
        /// <returns></returns>
        public static string FileToHtmlFile(string extFileName, string sourceFile, string desFile)
        {
            if (string.IsNullOrEmpty(sourceFile))
            {
                return "0";//沒有文件
            }

            switch (extFileName.ToUpper())
            {
                case "PPT":
                case "PPTX":
                    Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(sourceFile);
                    ppt.Save(desFile, Aspose.Slides.Export.SaveFormat.Html);
                    break;
                case "DOC":
                case "DOCX":
                    Aspose.Words.Document doc = new Aspose.Words.Document(sourceFile);
                    doc.Save(desFile, Aspose.Words.SaveFormat.Html);
                    break;
                //case "XLS":
                    //case "XLSX":
                   //    break;
            }
            return "ok";
        }

PPT文件生成的html 會出現編碼識別錯誤 導致瀏覽器網頁亂碼

解決方法如下,

                System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath);
                string html = sr.ReadToEnd();
                sr.Close();
                System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false);
                //System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.UTF8);
                //特別重要:添加編碼標誌,解決瀏覽器識別錯誤
                html = html.Replace("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\">", "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
                //去除試用標誌
                html = html.Replace("Evaluation only", "");
                html = html.Replace("Created with Aspose.Slides for .NET 2.0 14.8.1.0.", "");
                html = html.Replace("Copyright 2004-2014 Aspose Pty Ltd.", "");
                //html = html.Replace("Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.", "");
                html = html.Replace("Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.", "");
                html = html.Replace("This document was truncated here because it was created using Aspose.Words in Evaluation Mode.", "");
                // < meta http - equiv = "X-UA-Compatible" content = "IE=9" >< meta http - equiv = "Content-Type" content = "text/html; charset=utf-8" />
                sw.Write(html);
                sw.Close();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章