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                                     80%;
26                                     height:auto;
27                                     margin:0 auto;
28                                     padding:10px 20px;
29                                 }
30                                 img{
31                                     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("
","</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                                     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         }
原文地址:https://www.cnblogs.com/yuchenghao/p/8954010.html