c# word excel text轉html的方法


首先是預覽圖片,這個功能很好實現,無非就是創建一個html頁面,嵌套一個<img>,為了限制圖片類型,可以定義一個允許預覽類型數組作為限制:

 1  /// <summary>
 2         /// 預覽圖片
 3         /// </summary>
 4         /// <param name="physicalPath"></param>
 5         /// <param name="physicalDicPath"></param>
 6         /// <returns></returns>
 7         public string PreviewPic(string physicalPath, string physicalDicPath)
 8         {
 9             string imageName = Path.GetFileNameWithoutExtension(physicalPath);
10             string htmlName = imageName + ".html";
11             if (!File.Exists(physicalDicPath + htmlName))
12             {
13                 FileStream fs = new FileStream(physicalDicPath + htmlName, FileMode.CreateNew);
14                 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
15                 StringBuilder sb = new StringBuilder();
16                 sb.Append(@"<!DOCTYPE html>
17                             <html lang = 'zh-CN'><head>
18                             <meta http - equiv = 'Content-Type' content = 'text/html; charset=UTF-8'>
19                             <meta http - equiv = 'X-UA-Compatible' content = 'IE=edge'>
20                             <meta name = 'viewport' content = 'width=device-width, initial-scale=1'>
21                             <title>圖片預覽</title> 
22                             <style>
23                                 .content
24                                 {
25                                     width:80%;
26                                     height:auto;
27                                     margin:0 auto;
28                                     padding:10px 20px;
29                                 }
30                                 img{
31                                     width:80%;
32                                     height:auto;
33                                     margin: 10px 10%;
34                                 }
35                             </style>
36                             </head>");
37                 sb.Append(@"<body><div class='content'>");
38                 var TruePath = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalPath);
39                 sb.Append(@"<img src='" + TruePath + "'/>");
40                 sb.Append(@"</div>");
41                 sb.Append(@"</body>");
42                 sw.Write(sb.ToString());  //這里是寫入的內容
43                 sw.Flush();
44                 sw.Close();
45             }
46             var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);
47             return resultRul;
48         }

然后就是預覽excel文件,這個微軟為我們提供了現成的方法,打開nuget管理,安裝Microsoft.Office.Interop.Excel;經測試xls和xlsx格式都可以成功解析,然后代碼如下:

 1 /// <summary>
 2         /// 預覽Excel
 3         /// </summary>
 4         /// <param name="physicalPath">文件物理路徑</param>
 5         /// <param name="physicalDicPath">文件夾物理路徑</param>
 6         /// <returns>生成頁面鏈接</returns>
 7         public string PreviewExcel(string physicalPath, string physicalDicPath)
 8         {
 9             string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
10             if (!File.Exists(physicalDicPath + htmlName))
11             {
12                 Microsoft.Office.Interop.Excel.Application application = null;
13                 Microsoft.Office.Interop.Excel.Workbook workbook = null;
14                 application = new Microsoft.Office.Interop.Excel.Application();
15                 object missing = Type.Missing;
16                 object trueObject = true;
17                 application.Visible = false;
18                 application.DisplayAlerts = false;
19                 workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,
20                   missing, missing, missing, missing, missing, missing, missing, missing, missing);
21                 //Save Excel to Html
22                 object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
23                 String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
24                 workbook.SaveAs(outputFile, format, missing, missing, missing,
25                          missing, XlSaveAsAccessMode.xlNoChange, missing,
26                         missing, missing, missing, missing);
27                 workbook.Close();
28                 application.Quit();
29             }
30             var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);
31             return resultRul;
32         }

最后就是word的預覽了,word的話有兩個常見格式:doc,docx;在測試該方法的時候,打開doc格式的word,每次都會彈出轉換格式的彈窗,上網查了一下,原來的

Documents.Open()

方法的第二個參數是:真正顯示轉換文件對話框,如果該文件不是Microsoft Word格式。所以我們只需要將 ConfirmConversions設置為false即可規避這個問題;代碼如下:

 1  /// <summary>
 2         /// 預覽Excel
 3         /// </summary>
 4         /// <param name="type">文件格式</param>
 5         /// <param name="physicalPath">文件物理路徑</param>
 6         /// <param name="physicalDicPath">文件夾物理路徑</param>
 7         /// <returns>生成頁面鏈接</returns>
 8         public string PreviewWord(string physicalPath, string physicalDicPath)
 9         {
10             string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
11             if (!File.Exists(physicalDicPath + htmlName))
12             {
13                 Microsoft.Office.Interop.Word._Application application = null;
14                 Microsoft.Office.Interop.Word._Document doc = null;
15                 application = new Microsoft.Office.Interop.Word.Application();
16                 object missing = Type.Missing;
17                 object trueObject = true;
18                 object falseObject = false;
19                 application.Visible = false;
20                 application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
21                 doc = application.Documents.Open(physicalPath, falseObject, trueObject, missing, missing, missing,
22                   missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
23                 object format = format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
24                 String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
25                 doc.SaveAs(outputFile, format, missing, missing, missing,
26                          missing, missing, missing,
27                          missing, missing, missing, missing);
28                 doc.Close();
29                 application.Quit();
30             }
31             var resultRul= "http://"+HttpContext.Current.Request.Url.Authority +"/"+ urlconvertor(physicalDicPath + htmlName);
32             return resultRul;
33         }

 最后是預覽text,這個實現思想和預覽圖片是一樣的,讀取文檔內容,填充到html頁面上,代碼如下:

 1         /// <summary>
 2         /// 預覽Txt
 3         /// </summary>
 4         /// <param name="physicalPath">文件物理路徑</param>
 5         /// <param name="physicalDicPath">文件夾物理路徑</param>
 6         /// <returns>生成頁面鏈接</returns>
 7         public string PreviewTxt(string physicalPath, string physicalDicPath)
 8         {
 9             FileStream aFile = new FileStream(physicalPath, FileMode.Open);
10             //暫時不知道為什么獲取的編碼方式會導致讀取的內容為空
11             //Encoding codeType = GetType(aFile);
12             StreamReader sr = new StreamReader(aFile, Encoding.GetEncoding("GB2312"));
13             var content= sr.ReadToEndAsync().Result.Replace("\r\n","</br>");
14             string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
15             if (!File.Exists(physicalDicPath + htmlName))
16             {
17                 FileStream fs = new FileStream(physicalDicPath + htmlName, FileMode.CreateNew);
18                 StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
19                 StringBuilder sb = new StringBuilder();
20                 sb.Append(@"<!DOCTYPE html>
21                             <html lang = 'zh-CN'><head>
22                             <meta http - equiv = 'Content-Type' content = 'text/html; charset=UTF-8'>
23                             <meta http - equiv = 'X-UA-Compatible' content = 'IE=edge'>
24                             <meta name = 'viewport' content = 'width=device-width, initial-scale=1'>
25                             <title>文本預覽</title> 
26                             <style>
27                                 .content
28                                 {
29                                     width:60%;
30                                     height:auto;
31                                     margin:0 auto;
32                                     border:1px solid #333;
33                                     padding:10px 20px;
34                                 }
35                             </style>
36                             </head>");
37                 sb.Append(@"<body><div class='content'>");
38                 sb.Append(@"<p>");
39                 sb.Append(content);
40                 sb.Append(@"</p></div>");
41                 sb.Append(@"</body>");
42                 sw.Write(sb.ToString());  //這里是寫入的內容
43                 sw.Flush();
44                 sw.Close();
45             }
46             sr.Close();
47             var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);
48             return resultRul;
49         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM