直接在线预览Word、Excel、TXT文件之ASP.NET
程序员文章站
2023-11-16 23:21:22
具体实现过程不多说了,直接贴代码了。
using system;
using system.collections.generic;
using sy...
具体实现过程不多说了,直接贴代码了。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using microsoft.office.interop.excel; using system.diagnostics; using system.io; using microsoft.office.interop.word; namespace suya.web.apps.areas.pmp.controllers { /// <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 } }
以上就是针对直接在线预览word、excel、text、pdf文件的全部内容,希望大家喜欢。
推荐阅读
-
直接在线预览Word、Excel、TXT文件之ASP.NET
-
Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
-
ASP.NET Core 2.0集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
-
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
-
[Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!
-
Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
-
ASP.NET Core 2.0集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
-
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
-
详解html实现在线预览word、excel、pdf等文件的功能(附代码)
-
[Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!