首先是预览图片,这个功能很好实现,无非就是创建一个html页面,嵌套一个<img>,为了限制图片类型,可以定义一个允许预览类型数组作为限制:

  /// <summary>
/// 预览图片
/// </summary>
/// <param name="physicalPath"></param>
/// <param name="physicalDicPath"></param>
/// <returns></returns>
public string PreviewPic(string physicalPath, string physicalDicPath)
{
string imageName = Path.GetFileNameWithoutExtension(physicalPath);
string htmlName = imageName + ".html";
if (!File.Exists(physicalDicPath + htmlName))
{
FileStream fs = new FileStream(physicalDicPath + 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:80%;
height:auto;
margin:0 auto;
padding:10px 20px;
}
img{
width:80%;
height:auto;
margin: 10px 10%;
}
</style>
</head>");
sb.Append(@"<body><div class='content'>");
var TruePath = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalPath);
sb.Append(@"<img src='" + TruePath + "'/>");
sb.Append(@"</div>");
sb.Append(@"</body>");
sw.Write(sb.ToString()); //这里是写入的内容
sw.Flush();
sw.Close();
}
var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);
return resultRul;
}

然后就是预览excel文件,这个微软为我们提供了现成的方法,打开nuget管理,安装Microsoft.Office.Interop.Excel;经测试xls和xlsx格式都可以成功解析,然后代码如下:

 /// <summary>
/// 预览Excel
/// </summary>
/// <param name="physicalPath">文件物理路径</param>
/// <param name="physicalDicPath">文件夹物理路径</param>
/// <returns>生成页面链接</returns>
public string PreviewExcel(string physicalPath, string physicalDicPath)
{
string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
if (!File.Exists(physicalDicPath + htmlName))
{
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 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(physicalDicPath + htmlName);
return resultRul;
}

最后就是word的预览了,word的话有两个常见格式:doc,docx;在测试该方法的时候,打开doc格式的word,每次都会弹出转换格式的弹窗,上网查了一下,原来的

Documents.Open()

方法的第二个参数是:真正显示转换文件对话框,如果该文件不是Microsoft Word格式。所以我们只需要将 ConfirmConversions设置为false即可规避这个问题;代码如下:

  /// <summary>
/// 预览Excel
/// </summary>
/// <param name="type">文件格式</param>
/// <param name="physicalPath">文件物理路径</param>
/// <param name="physicalDicPath">文件夹物理路径</param>
/// <returns>生成页面链接</returns>
public string PreviewWord(string physicalPath, string physicalDicPath)
{
string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
if (!File.Exists(physicalDicPath + htmlName))
{
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;
object falseObject = false;
application.Visible = false;
application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
doc = application.Documents.Open(physicalPath, falseObject, trueObject, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
object format = format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
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(physicalDicPath + htmlName);
return resultRul;
}

最后是预览text,这个实现思想和预览图片是一样的,读取文档内容,填充到html页面上,代码如下:

         /// <summary>
/// 预览Txt
/// </summary>
/// <param name="physicalPath">文件物理路径</param>
/// <param name="physicalDicPath">文件夹物理路径</param>
/// <returns>生成页面链接</returns>
public string PreviewTxt(string physicalPath, string physicalDicPath)
{
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(physicalDicPath + htmlName))
{
FileStream fs = new FileStream(physicalDicPath + 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(physicalDicPath + htmlName);
return resultRul;
}

c# word excel text转html的方法的更多相关文章

  1. 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法

    1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...

  2. 共享文件word / excel /ppt 被用戶自己锁定无法编辑-解決方法

    共享文件word / excel /ppt 被用戶自己鎖定無法編輯,但用戶嘗試過關閉所有文件和重啓過系統,依然無法編輯. 搜到解決方法: Just in case someone looking fo ...

  3. 【MVC】 非常简单的页面导出 WORD, EXCEL方法

    [MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...

  4. Office word excel电子表格在线编辑的实现方法

    Office xp之后的版本支持通过webdav协议(http的扩展)直接编辑服务器上的文件. IIS(6.0)支持webdav,这在IIS管理器的web服务扩展中可以看到.利用IIS作为webdav ...

  5. Word,Excel,pdf,txt等文件上传并提取内容

    近期项目需求:1.要用到各种文件上传,下载. 2.并对文件进行搜索. 3.仅仅要文件里包括有搜索的内容,所有显示出来. 今天正好有时间整理一下,方便以后阅读,及对须要用到的朋友提供微薄之力.首先在实现 ...

  6. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

  7. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

  8. ASP.NET Word/Excel 权限问题

    在部署Word/Excel到服务器的时候,经常会碰到权限问题.例如;   Retrieving the COM class factory for component with CLSID {0002 ...

  9. 读取Excel文件的两种方法

    第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...

随机推荐

  1. Linux系统是否被植入木马的排查流程梳理

    在日常繁琐的运维工作中,对linux服务器进行安全检查是一个非常重要的环节.今天,分享一下如何检查linux系统是否遭受了入侵? 一.是否入侵检查 1)检查系统日志 1 2 检查系统错误登陆日志,统计 ...

  2. WebView加载html实现网页上传本地文件(图片,拍照,语音等)

    前言: 这里有两个方案,第一个使用Andorid客户端和JavaScript互相调用方法来实现,这种方法极力不推荐,它会增加服务端和客户端的开发成本. 第二种就是继承WebViewChromeClie ...

  3. 微服务中的CAP定律

    说到微服务,先给大家提一下CAP分布式应用知识吧,无论你微服务使用的是阿里云开源的Dubbo还是基于Springboot的一整套实现微服务的Springcloud都必须遵循CAP定理不然你所实现的分布 ...

  4. 定位上下文(补充css的position属性)

    ]把元素的position属性设定为relative.absolute或fixed后,继而可以使用TRBL属性,相对于另一个元素移动该元素的位置.这里的“另一个元素”,就是该元素的定位上下文. 绝对定 ...

  5. Apple全系列缓冲区溢出内核RCE(CVE-2018-4407)poc

    # CVE-2018-4407 ICMP DOS # https://lgtm.com/blog/apple_xnu_icmp_error_CVE-2018-4407 # from https://t ...

  6. Linux上MongoDB一些设置

    MongoDB启动停止方法 官网安装介绍中依然有启动停止的方式 1 启动 sudo service mongod start 2 停止 sudo service mongod stop 3 重启 su ...

  7. hive端建表中文注释乱码

    背景:mysql编码是utf-8,mysql中建库建表中文显示都正常,但在hive窗口中建表时字段中文注释均乱码的问题. 问题:hive中建表后字段中文注释显示异常. 1. 定位 mysql 端问题 ...

  8. vue中自定义指令的使用

    原文地址 vue中除了内置的指令(v-show,v-model)还允许我们自定义指令 想要创建自定义指令,就要注册指令(以输入框获取焦点为例) 一.注册全局指令: // 注册一个全局自定义指令 `v- ...

  9. 在VM虚拟机Windows Server r2上部署安装Microsoft Dynamics CRM 2016 步骤详解(一)

    应公司需求,最近在学微软的Dynamics CRM.在搭建环境的过程中也遇到了一些雷坑,在这里分享一下安装部署过程当中所遇到的一些问题, 安装Microsoft Dynamics CRM 2016的几 ...

  10. Unity3D热更新之LuaFramework篇[10]--总结篇

    背景 19年年初的时候,进到一家新单位,公司正准备将现有的游戏做成支持热更的版本.于是寻找热更方案的任务就落在了我头上. 经过搜索了解,能做Unity热更的方案是有好几种,但是要么不够成熟,要么不支持 ...