C#中mvc模式在线预览Office(word 可编辑、txt)文件
来源:互联网 发布:软件测试薪资 编辑:程序博客网 时间:2024/06/08 14:28
/// <summary> /// 在线预览Office文件 /// </summary> public class OfficeViewController : Controller { #region Index页面 /// <summary> /// Index页面 /// </summary> /// <param name="url">例:/uploads/......XXX.xls</param> public ActionResult Index(string url) { string physicalPath = Server.MapPath(Server.UrlDecode(url)); string extension = Path.GetExtension(physicalPath); string htmlUrl = ""; switch (extension.ToLower()) { case ".xls": case ".xlsx": htmlUrl = PreviewExcel(physicalPath, url); break; case ".doc": case ".docx": htmlUrl = PreviewWord(physicalPath, url); break; case ".txt": htmlUrl = PreviewTxt(physicalPath, url); break; case ".pdf": htmlUrl = PreviewPdf(physicalPath, url); break; } return Redirect(Url.Content(htmlUrl)); } #endregion #region 预览Excel /// <summary> /// 预览Excel /// </summary> public string PreviewExcel(string physicalPath, string url) { 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 htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; 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(); return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName; } #endregion #region 预览Word /// <summary> /// 预览Word /// </summary> public string PreviewWord(string physicalPath, string url) { 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; application.Visible = false; application.DisplayAlerts = WdAlertLevel.wdAlertsNone; doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //Save Excel to Html object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName; doc.SaveAs(outputFile, format, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); doc.Close(); application.Quit(); return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName; } #endregion #region 预览Txt /// <summary> /// 预览Txt /// </summary> public string PreviewTxt(string physicalPath, string url) { return Server.UrlDecode(url); } #endregion #region 预览Pdf /// <summary> /// 预览Pdf /// </summary> public string PreviewPdf(string physicalPath, string url) { return Server.UrlDecode(url); } #endregion }
原文地址:http://www.jb51.net/article/70561.htm
阅读全文
0 0
- C#中mvc模式在线预览Office(word 可编辑、txt)文件
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- C#在线预览文档(word,excel,pdf,txt,png)
- C# office在线预览
- C# web实现word 转Html、office转Html、pdf转图片 在线预览文件
- C# web实现word 转Html、office转Html、pdf转图片 在线预览文件
- office文件在线预览
- 直接在线预览Word、Excel、TXT文件之ASP.NET
- office(如:Word、Excel、PPT 等)文件轻松实现在线预览
- .net 实现Office文件预览,word文件在线预览、excel文件在线预览、ppt文件在线预览,excel转html,office格式转换,
- apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码
- Android实现在线预览office文档(Word,Pdf,excel,PPT.txt等格式)
- c#中做在线预览时没有在利用using时发现没有Microsoft.Office.Interop.Word;这个引用解决方案
- office办公文件在线预览
- word在线预览,文件在线预览,SWFTools
- java word文件转html (转换后可在线预览)
- C#在线预览文件
- java将office文档,word,ppt,pdf文档转换成swf文件在线预览
- MAVEN实现多环境搭建
- hiho一下 第161周 树结构判定
- C语言输入密码时的回显处理
- 8.15 M
- JS12
- C#中mvc模式在线预览Office(word 可编辑、txt)文件
- iptables-1.6.0的ARM移植
- 是否有网络
- IntelliJ打开浏览器的快捷键
- 深度学习论文(八)---DeepLabV1-SEMANTIC IMAGE SEGMENTATION WITH DEEP CONVOLUTIONAL NETS AND FULLY CONNECTED C
- 对区间的模糊排序(原题算法导论第三版思考题7-6)
- gson转换问题Date
- Android之数据库框架greenDAO3.0使用指南
- intellij与eclipse默认快捷键对比