office文件在线预览,模仿网易邮箱在线预览的
最近研究了半天,代码是倾情奉送啊,C#,asp.net的
这个原理是office文件转换为PDF文件,然后再转换成SWF文件,FlexPaper+swfTools。
有个问题,需要在web.config中加这么一行<identity impersonate="true" userName="administrator" password="你的服务器登录密码" />
/// <summary>
/// 转换压缩文件,以便于预览(图片大小调整,office、pdf转成swf)
/// </summary>
/// <param name="sfilePath">真实文件路径(虚拟),含文件名,精确到网站根目录</param>
/// <param name="sfileExtention">真实的文件扩展名,要判断如何转换</param>
/// <param name="showWait">真是否显示提示等待转换信息,默认不提示</param>
public static void FileConvert(string sfilePath, string sfileExtention, bool showWait = false)
{
string PfilePath = HttpContext.Current.Server.MapPath(Path.GetDirectoryName(sfilePath));//当前操作的物理路径
string PfileName = Path.GetFileName(sfilePath);// 当前操作的文件名
string PfileExtention = sfileExtention.ToLower();// 当前操作的文件扩展名
if (File.Exists(PfilePath + "\\swf\\" + PfileName + ".swf"))
{ return; }
else
{
if (!Directory.Exists(PfilePath + "\\swf\\")) { Directory.CreateDirectory((PfilePath + "\\swf\\")); }
try
{
switch (DocuType(PfileExtention))
{
case TypeOfDocu.office:
if (showWait)
{
System.Text.StringBuilder StrB = new System.Text.StringBuilder();
StrB.Append("<div style='color: #0000CC; font-size: 14px;'>文档需转换格式,才可以在线预览。<br /><br />转换中,马上就好,请稍后……</div>");
StrB.Append("<div style='color:white;'>0000000000000000000000000000000000000000000000000您是第一个浏览的人</div>");//兼容IE256字符才显示,这里180就可以
HttpContext.Current.Response.Write(StrB.ToString());
HttpContext.Current.Response.Flush();
}
//将office文件转换成PDF,保存到swf文件夹下
string pdfFileName = PfileName + ".pdf";
string fileOutPath = PfilePath + "\\swf\\" + pdfFileName;
ExportPdf(PfilePath + "\\" + PfileName, fileOutPath, PfileExtention);
//切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败
string cmdStr = HttpContext.Current.Server.MapPath("~/SWF/pdf2swf.exe");
string savePath = PfilePath + "\\swf\\";
//string saveSWFPath = HttpContext.Current.Server.MapPath("~/SWF/");
//将PDF文件转换成SWF格式文件
string sourcePath = @"""" + savePath + pdfFileName + @"""";//要转换的pdf文件路径
string targetPath = @"""" + savePath + PfileName + ".swf" + @"""";//转换之后swf文件存放的目标路径
//@"""" 四个双引号得到一个双引号,如果你所存放的文件所在文件夹名有空格的话,要在文件名的路径前后加上双引号,才能够成功
// -t 源文件的路径
// -s 参数化(也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略))
string argsStr = " -p 1-100 -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
//执行pdf到swf的转换
ExcutedCmd(cmdStr, argsStr);
File.Delete(savePath + pdfFileName);
break;
case TypeOfDocu.pdf:
cmdStr = HttpContext.Current.Server.MapPath("~/SWF/pdf2swf.exe");
sourcePath = @"""" + PfilePath + "\\" + PfileName + @"""";
targetPath = @"""" + PfilePath + "\\swf\\" + PfileName + ".swf" + @"""";
argsStr = " -p 1-100 -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
ExcutedCmd(cmdStr, argsStr);
break;
case TypeOfDocu.jpg:
if (!Directory.Exists(PfilePath + "\\img\\")) { Directory.CreateDirectory((PfilePath + "\\img\\")); }
ImageSize(, , PfilePath + "\\" + PfileName, PfilePath + "\\img\\" + PfileName + ".jpg", false);
break;
}
}
catch (Exception ex)
{
throw ex;
}
}
} private static void ExcutedCmd(string cmd, string args)
{
using (Process p = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = psi;
p.Start();
p.WaitForExit();
}
} private static bool ExportPdf(string fileName, string outputFileName, string fileExt)
{
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(outputFileName))
return false;
if (!File.Exists(fileName))
return false;
string formatExtension = Path.GetExtension(outputFileName);
if (string.IsNullOrEmpty(fileExt) || string.IsNullOrEmpty(formatExtension))
return false;
if (formatExtension != ".pdf")
return false;
switch (fileExt)
{
case "doc":
case "docx":
return WordExportAsPdf(fileName, outputFileName);
case "xls":
case "xlsx":
return ExcelExportAsPdf(fileName, outputFileName);
case "ppt":
case "pptx":
return PowerPointExportAsPdf(fileName, outputFileName);
default:
return false;
}
} /// <summary>
/// 转换为pdf文件,适合(.doc、.docx、.mht、.htm文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool WordExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
Word.WdExportFormat fileFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Word._Application wordApp = null;
if (wordApp == null) wordApp = new Word.Application();
Word._Document wordDoc = null; try
{
wordDoc = wordApp.Documents.Open(fileName);
wordDoc.ExportAsFixedFormat(outputFileName, fileFormat);
isSucceed = true;
} finally
{
if (wordDoc != null)
{
wordDoc.Close();
wordDoc = null;
}
if (wordApp != null)
{
wordApp.Quit();
wordApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
} /// <summary>
/// 转换为pdf文件,适合(.xls、.xlsx文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool ExcelExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
Excel.XlFixedFormatType fileFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
Excel.Application excelApp = null;
if (excelApp == null) excelApp = new Excel.Application();
Excel.Workbook workBook = null; try
{
workBook = excelApp.Workbooks.Open(fileName);
workBook.ExportAsFixedFormat(fileFormat, outputFileName);
isSucceed = true;
} finally
{
if (workBook != null)
{
workBook.Close();
workBook = null;
}
if (excelApp != null)
{
excelApp.Quit();
excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
KillExcel();
}
return isSucceed;
}
/// <summary>
/// EXCEL进程无法正常退出
/// </summary>
private static void KillExcel()
{
Process[] pp = Process.GetProcessesByName("EXCEL");
foreach (Process p in pp)
{
if (p.SessionId == )
{ p.Kill(); }
}
} /// <summary>
/// 转换为pdf文件,适合(.ppt、pptx文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool PowerPointExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
PowerPoint.PpFixedFormatType fileFormat = PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF; PowerPoint.Application pptxApp = null;
if (pptxApp == null) pptxApp = new PowerPoint.Application();
PowerPoint.Presentation presentation = null; try
{
presentation = pptxApp.Presentations.Open(fileName, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
presentation.ExportAsFixedFormat(outputFileName, fileFormat);
isSucceed = true;
} finally
{
if (presentation != null)
{
presentation.Close();
presentation = null;
}
if (pptxApp != null)
{
pptxApp.Quit();
pptxApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
}
这个东西在64位操作系统中通过,就是每次转换的时间很长,Excel没排版好,也乱打印成PDF。
还有一种是模仿网易邮箱的附件预览的。参考:http://www.officeweb365.com/docview.aspx,把office文档原版呈现,能把这个地址利用起来倒是挺好
office文件在线预览,模仿网易邮箱在线预览的的更多相关文章
- Office系列(1)---将Office文件(Word、PPT、Excel)转换为PDF文件
需求: 将Office文件作为文章并在网页上预览,主要为(Word.PPT.Excel)3种类型文件. 研究了一下,找到了两种解决方案 直接调用微软的在线预览功能实现(预览前提:预览资源必须可以直接通 ...
- jQuery封装的表单验证,模仿网易或者腾讯登录的风格
模仿网易邮箱做了一个登录表单验证,不太好,请指教 上代码 <form action="" name="" id="form1"> ...
- 在线预览Office文件【效果类似百度文库】
引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好. 提到Offic ...
- 在线预览Office文件【效果类似百度文库】(转载)
转载地址:http://www.cnblogs.com/sword-successful/p/4031823.html 引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前 ...
- Java实现word文档在线预览,读取office文件
想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...
- 浏览器在线预览pdf、txt、office文件
//使用文件预览的原因是:TMD微信浏览器屏蔽掉文件下载链接,只好折中使用文件在线预览功能//要点:1.office文件用微软的插件打开 http://view.officeapps.live.com ...
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- js调用本地office打开服务器的office文件预览
本来是想做成直接在网页上在线预览office文件的,但是找了好多,要不是收费,要不就是要调用别人的API不安全,所以纠结了好久还是用调用本地的office预览office文件. 废话不多说,那么怎么调 ...
- 使用FlexPaper实现office文件的预览(C#版)
需求很简单,用户上传office文件(word.excel.ppt)后,可以预览上传的这些文件.搜索的相关的资料后.整理如下: Step1.用户上传office文件. Step2.把Office文件转 ...
随机推荐
- html5异步上传图片显示上传文件进度条
<html> <head> </head> <body> <p> emo_album_id:<input type="tex ...
- ThinkPHP下使用Ueditor
在做课程设计的时候想到用百度的Ueditor,可在配置的时候出现了一些问题 Ueditor感觉不是很难,以前有个人定制的,现在取消了这项服务,但是我们可以自己进行配置 下载地址:http://uedi ...
- gulp最佳实践(包含js,css,html预编译,合并,压缩,浏览器自动刷新)
gulp是基于流的自动化构建工具官方网址:http://www.gulpjs.com.cn/ 一.安装需要的模块 1.新建package.json,输入下面的内容 { "name" ...
- “\n”与“\r”的区别
ASCII中“\n”代表着换行,“\r”代表着将光标移动到当前显示行的最左边.
- 点评VHDL语言
(1)VHDL的描述风格及语法十分类似于一般的计算机高级语言,但是它是一种硬件描述语言.学好VHDL的关键是充分理解VHDL语句和硬件电路的关系.编写VHDL,就是在描述一个电路,我们写完一段程序后, ...
- CSS3------background-size(背景图片尺寸属性)
background-size 可以设置背景图片的大小,数值包括 长度length和百分比percentage. 并且会根据背景原点位置 background-origin 设置其图片覆盖的范围.那么 ...
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- 【技术贴】note8 N5100刷机 双清 落雨
双清模式:开机键 + [音量+] + HOME键 刷机模式:开机键 + [音量- ]+ HOME键 1.双清步骤: 关机时.长按音量上键+home键+开机键,直到进入recovery模式,然后选择wi ...
- http://jinnianshilongnian.iteye.com/blog/1996071
http://jinnianshilongnian.iteye.com/blog/1996071 http://my.oschina.net/jkcui/blog/388400 http://tian ...
- Android用户界面 UI组件--TextView及其子类(一) TextView
1.TextView android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none /web/email/phone/map/a ...