博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# word excel text转html的方法
阅读量:5068 次
发布时间:2019-06-12

本文共 7230 字,大约阅读时间需要 24 分钟。

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

1  ///  2         /// 预览图片 3         ///  4         ///  5         ///  6         /// 
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(@"17 18
19
20
21 图片预览 22 36 ");37 sb.Append(@"
");38 var TruePath = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalPath);39 sb.Append(@"
");40 sb.Append(@"
");41 sb.Append(@"");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 ///  2         /// 预览Excel 3         ///  4         /// 文件物理路径 5         /// 文件夹物理路径 6         /// 
生成页面链接
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 Html22 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  ///  2         /// 预览Excel 3         ///  4         /// 文件格式 5         /// 文件物理路径 6         /// 文件夹物理路径 7         /// 
生成页面链接
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         ///  2         /// 预览Txt 3         ///  4         /// 文件物理路径 5         /// 文件夹物理路径 6         /// 
生成页面链接
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","
");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(@"21 22
23
24
25 文本预览 26 36 ");37 sb.Append(@"
");38 sb.Append(@"

");39 sb.Append(content);40 sb.Append(@"

");41 sb.Append(@"");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

你可能感兴趣的文章
Python 3.X 练习集100题 05
查看>>
设计器 和后台代码的转换 快捷键
查看>>
Monkey测试结果分析
查看>>
STL——配接器、常用算法使用
查看>>
STL容器之vector
查看>>
无法向会话状态服务器发出会话状态请求
查看>>
数据中心虚拟化技术
查看>>
01入门
查看>>
复习文件操作
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
一步步学习微软InfoPath2010和SP2010--第七章节--从SP列表和业务数据连接接收数据(4)--外部项目选取器和业务数据连接...
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
基于grunt构建的前端集成开发环境
查看>>
利用循环播放dataurl的视频来防止锁屏:NoSleep.js
查看>>
python3 生成器与迭代器
查看>>
java编写提升性能的代码
查看>>