基于ASP.NET MVC 利用(Aspose+Pdfobject.js) 实现在线预览Word、Excel、PPT、PDF文件
#region
- VS2010版本以及以上版本源码下载地址:http://download.csdn.net/download/u012949335/10231812
- VS2012版本以及以上版本源码下载地址:http://download.csdn.net/download/u012949335/10232057
private bool PdfToHtml(string fileName, string tempFile, string saveDoc)
{
//---------------------读html模板页面到stringbuilder对象里----
StringBuilder htmltext = new StringBuilder();
using (StreamReader sr = new StreamReader(tempFile)) //模板页路径
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
} fileName = fileName.Replace("\\", "/");
//----------替换htm里的标记为你想加的内容
htmltext.Replace("$PDFFILEPATH", fileName); //----------生成htm文件------------------――
using (StreamWriter sw = new StreamWriter(saveDoc, false,
System.Text.Encoding.GetEncoding("utf-8"))) //保存地址
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close(); } return true;
} private bool OfficeDocumentToHtml(string sourceDoc, string saveDoc)
{
bool result = false; //获取文件扩展名
string docExtendName = System.IO.Path.GetExtension(sourceDoc).ToLower();
switch (docExtendName)
{
case ".doc":
case ".docx":
Aspose.Words.Document doc = new Aspose.Words.Document(sourceDoc);
doc.Save(saveDoc, Aspose.Words.SaveFormat.Html); result = true;
break;
case ".xls":
case ".xlsx":
Workbook workbook = new Workbook(sourceDoc);
//workbook.Open(sourceDoc);
//workbook.Save(saveDoc, FileFormatType.AsposePdf);
workbook.Save(saveDoc, Aspose.Cells.SaveFormat.Html); result = true;
break;
case ".ppt":
case ".pptx":
//templateFile = templateFile.Replace("/", "\\");
//string templateFile = sourceDoc;
//templateFile = templateFile.Replace("/", "\\");
PresentationEx pres = new PresentationEx(sourceDoc);
pres.Save(saveDoc, Aspose.Slides.Export.SaveFormat.Html); result = true;
break;
default:
break;
} return result;
} #endregion [HttpGet]
public string CourseViewOnLine(string fileName)
{
//DataTable dtlist = new DataTable();
//dtlist.Columns.Add("TempDocHtml", typeof(string));
string json = "";
string fileDire = "../Files";
string sourceDoc = Path.Combine(fileDire, fileName);
string saveDoc = ""; string docExtendName = System.IO.Path.GetExtension(sourceDoc).ToLower();
bool result = false;
if (docExtendName == ".pdf")
{
//pdf模板文件
string tempFile = Path.Combine(fileDire, "temppdf.html");
saveDoc = Path.Combine(fileDire, "viewFiles/onlinepdf.html");
result = PdfToHtml(
sourceDoc,
System.Web.HttpContext.Current.Server.MapPath(tempFile),
System.Web.HttpContext.Current.Server.MapPath(saveDoc));
}
else
{
saveDoc = Path.Combine(fileDire, "viewFiles/onlineview.html");
result = OfficeDocumentToHtml(
System.Web.HttpContext.Current.Server.MapPath(sourceDoc),
System.Web.HttpContext.Current.Server.MapPath(saveDoc));
} if (result)
{
json = "{\"TempDocHtml\":\"" + saveDoc.Replace("\\","/") + "\"}";
//dtlist.Rows.Add(saveDoc);
} return json;
}
$(function () {
SexyLightbox.initialize({ color: 'white' });
});
var viewDoc = function (fileName) {
showLoading("body", "正在生成预览");
//生成html文件
$.ajax({
url: "CourseViewOnLine?fileName=" + fileName,
type: "GET",
dataType: "json",
success: function (data) {
closeLoading();
//alert(JSON.stringify(data));
//alert(data[0].TempDocHtml);
var diag = new Dialog();
diag.Width = ;
diag.Height = ;
diag.Title = "内容页为外部连接的窗口";
diag.URL = data.TempDocHtml + "?ver=" + Math.random() * ;
diag.show();
//$("#hidePopupDialog").attr('href', '' + data[0].TempDocHtml + '?TB_iframe=true&height=450&width=920');
//$("#hidePopupDialog").click();
},
error: function () {
closeLoading();
alert('生成失败');
}
});
}
// 加载遮罩
var showLoading = function (elementTag, message) {
var msg = message ? message : "加载数据,请稍候...";
$("<div class=\"datagrid-mask\"></div>").css({
display: "block", width: "100%",
height: $(elementTag).height()
}).appendTo(elementTag);
$("<div class=\"datagrid-mask-msg\"></div>")
.html(msg)
.appendTo(elementTag).css({ display: "block", left: "30%", top: ($(elementTag).height() - ) / });
};
//关闭遮罩
var closeLoading = function () {
$('.datagrid-mask').remove();
$('.datagrid-mask-msg').remove();
};
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
IndexOnline
</asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>IndexOnline</h2>
<script src="../Scripts/jquery-1.8.2.js" type="text/javascript"></script>
<script src="../Scripts/Js/pdfobject/pdfobject.js" type="text/javascript"></script>
<script src="../Scripts/Js/index.js" type="text/javascript"></script>
<script src="../Scripts/Js/PopupDialog/jquery.easing.1.3.js" type="text/javascript"></script>
<script src="../Scripts/Js/PopupDialog/sexylightbox.v2.3.jquery.min.js" type="text/javascript"></script>
<script src="../Scripts/Js/zDialog/zDrag.js" type="text/javascript"></script>
<script src="../Scripts/Js/zDialog/zDialog.js" type="text/javascript"></script> <a class="docViewDownload"
onclick="viewDoc('wordtest1.doc');">Word 预 览
</a> <a class="docViewDownload"
onclick="viewDoc('exceltest.xls');">Excel 预 览
</a> <a class="docViewDownload"
onclick="viewDoc('ppttest.pptx');">PPT 预 览
</a>
<a class="docViewDownload"
onclick="viewDoc('pdftest.pdf');">PDF 预 览
</a>
</asp:Content>
基于ASP.NET MVC 利用(Aspose+Pdfobject.js) 实现在线预览Word、Excel、PPT、PDF文件的更多相关文章
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
在线预览word.excel.ppt利用aspose动态生成html 主要代码 private bool OfficeDocumentToHtml(string sourceDoc, string s ...
- Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件
Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...
- Asp.Net在线预览Word文档的解决方案与思路
前几天有个老项目找到我,有多老呢?比我工作年限都长,见到这个项目我还得叫一声前辈. 这个项目目前使用非常稳定,十多年了没怎么更新过,现在客户想加一个小功能:在线预览Word文档. 首先想到的是用第三方 ...
- java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)
背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...
- C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
由于项目需要,需要一个在线预览office的功能,小编一开始使用的是微软提供的方法,简单快捷,但是不符合小编开发需求, 就另外用了:将文件转换成html文件然后预览html文件的方法.对微软提供的方法 ...
- 【ASP.NET 进阶】仿百度文库文档在线预览(支持格式.pdf,.doc,docx,xls,xlsx,.ppt,pptx)
在[ASP.NET]PDF文件在线预览(类似百度文库)基础上进行了office文件到pdf文件的转换,然后在显示出来,效果如下: 问题说明: 1.请通过以下方式添加 Office COM 组件. 2. ...
- ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...
- [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
转载自:https://www.cnblogs.com/Andre/p/9549874.html Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架 ...
- 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像
纯代码不解释. 在CusotmerControllers中添加上传方法 /// <summary> /// ajax上传用户头像 /// </summary> /// < ...
随机推荐
- Linux安装Python虚拟环境virtualenv
1.安装virtulenv.virtulenvwrapper包 pip install virtualenv virtualenvwrapper 2.virtualenvwrapper是virtual ...
- django-上下文渲染器,将后端内容提供给模板使用,自定义渲染器
使用场景:当同一个类容需要出现在多个模板中, 上下文管理器就是将这个内容以字典的形式返回给所有模板,模板通过 {{ key }}取值. 结构:上下文管理器 文件名一般为context_processo ...
- 设置GO环境变量
linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...
- CB XE7 C11 64位编译器 成员变量初始化
看到了C++11,看到了XE7的64位,想实现下面方便的类成员初始化,失望. 一.64位用法 clang3,64位编译器,不支持中文变量名,编写代码提示没有32位快,风格简单不用写单独的赋值语句函数, ...
- Installing Eclipse Plug-ins from an Update Site with a self-signed certificate
If you try and connect to a p2 repository on a server with a self-signed cert, you will more than li ...
- 【转】从Mac/OS和iOS开放源码浅谈UNIX家谱
阅读数:1245 苹果公司在各类开源项目中长期贡献着自己的力量,但其UNIX系统技术一直都属于闭源阵营(这一点可以从NUX OS阵营和家谱图中得到答案).然而,以封闭闻名的苹果公司,2017年国庆期间 ...
- chrome36可以使用自定义元素的回调了
<!DOCTYPE html> <html> <head> <title>ms-attr-*</title> <meta charse ...
- Multithread之为什么spinlock必须是volatile?
[Multithread之为什么spinlock必须是volatile?] 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后再取变量 ...
- Shiro01 功能点框图、架构图、身份认证逻辑、身份认证代码实现
基本功能点 功能点框图 功能点说明 1.Authentication:身份认证/登录,验证用户是不是拥有相应的身份: 2.Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个 ...
- Homestead window10 storage:link 不能建立符号链接的处理办法
重启电脑 1. 以管理员身份运行 cmd 2. vagrant up 3. vagrant ssh 4. php artisan storage:link