基于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> /// < ...
随机推荐
- 20165233 Java第八、十五章学习总结
20165233 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 ch08 基础:String类 重点:StringTokenizer类.Scanner类:获 ...
- Go语言优势与劣势
Go语言的优势: 1.脚本化的语法:开发效率高,容易上手 2.静态类型+编译型,程序运行速度有保障:静态类型+编译型语言相对于动态类型+解释型语言的效率高 3.原生的支持并发编程:降低开发.维护成本/ ...
- Rhythmk 一步一步学 JAVA (18): Enum枚举学习
枚举定义: public enum SizeEnum { SMALL, BIG, BIGEST }; public enum SizeStringEnum { SMALL("小") ...
- Git---远程仓库之从远程仓库克隆03
远程仓库之添加远程仓库02我们讲了先有本地库,后有远程库,如何关联远程库. 现在假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆. 首先,登录GitHub,创建一个新的仓库,名字叫g ...
- 迷你MVVM框架 avalonjs 学习教程4、数据填充
MVVM是前端的究极解决方案,你们可能用过jQuery,但那个写的代码不易维护:你们可以听过说requirejs与seajs,传说中的模块开发,加载器,但它们的最终目标是打包:你们可能听过unders ...
- python's import mechanism
[python's import mechanism] 问题描述: [A.py] from B import D class C:pass [B.py] from A import C class D ...
- ORACLE BI Publisher Enterprise
二.带参数 BEGIN:{$FIRST_DAY_OF_MONTH()$} END:{$SYSDATE()$} 三\加下拉菜单值
- go_组合接口
main函数入口 package main import ( "fmt" "learngo/retriever/mock" "learngo/retr ...
- Ant+jmeter+jenkins搭建测试的持续集成
前提: Ant+jmeter 已经搭建完成并成功运行(参看ant+jmeter自动化性能测试) Jenkins在本地已经安装可运行(参看上一篇) 1.下载Jenkins安装 2.浏览器输入地址http ...
- WEB开发常见错误-php无法操作数据库
Ubuntu 安装phpmyadmin 和配置 ubuntu 安装 phpmyadmin 两种 : 1: apt-get 安装 然后使用 已有的虚拟主机目录建立软连接 sudo apt-g ...