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文件转 ...
随机推荐
- CentOS 6.4 安装思维到图工具TheBrain
最近学习中需要使用思维导图的工具,但是使用的系统是CentOS,在网上找到了一个比较好的思维导图工具:TheBrain,安装完成后还是汉化版的不错啊,由于用的是linux系统,还没有找到合适的截图软件 ...
- 表格table样式布局设置
<style> table{ border-collapse:collapse; margin:0 auto;} table tr td{ border:1px solid #000; l ...
- lnmp安装--php与nginx结合
软件环境: linux:centos5. nginx:.tar.gz php:.tar.gz lnmp与lamp的区别? lnmp(linux+nginx+mysql+php)的提法相对于lamp(l ...
- 【java版坦克大战---准备篇】 java 绘图
要写坦克大战当然要先画出坦克.java画图是基础. package com.game; import java.awt.*; import javax.swing.*; public class Pr ...
- #Leet Code# Convert Sorted Array to Binary Search Tree
描述:递归 代码: class Solution: # @param num, a list of integers # @return a tree node def sortedArrayToBS ...
- 斗地主你什么时候才会托管?(.NET中的托管于非托管)
文章部分引自<.NET4.0面向对象编程漫谈(基础篇)>第1章.NET面向对象编程基础(作者:金旭亮) 无意间看到一位四五岁左右小朋友在玩斗地主,总开始到结束,她一直都在使用“提示”(托管 ...
- MOS管应用之放反接电路
一.典型电路 1.电路1 说明: GND-IN 为电源接口的负极 GND 为内部电路的公共地 原理分析 正向接: VCC-IN通过R1.R2.MOS体二极管,最后回到GND-IN;然后GS电压升高,紧 ...
- Spring之Spring MVC
Spring调配半天没搞定,原来是web.xml应该放在WEB-INF的目录下,而不是webcontent目录下: java.lang.ClassNotFoundException: org.spri ...
- 7 -- Spring的基本用法 -- 8...
7.8 深入理解容器中的Bean 7.8.1 抽象Bean与子Bean 把多个<bean.../>配置中相同的信息提取出来,集中成配置模版------这个配置模版并不是真正的Bean,因此 ...
- UITableView中复用cell显示信息错乱
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...