最近研究了半天,代码是倾情奉送啊,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文件在线预览,模仿网易邮箱在线预览的的更多相关文章

  1. Office系列(1)---将Office文件(Word、PPT、Excel)转换为PDF文件

    需求: 将Office文件作为文章并在网页上预览,主要为(Word.PPT.Excel)3种类型文件. 研究了一下,找到了两种解决方案 直接调用微软的在线预览功能实现(预览前提:预览资源必须可以直接通 ...

  2. jQuery封装的表单验证,模仿网易或者腾讯登录的风格

    模仿网易邮箱做了一个登录表单验证,不太好,请指教 上代码 <form action="" name="" id="form1"> ...

  3. 在线预览Office文件【效果类似百度文库】

    引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好. 提到Offic ...

  4. 在线预览Office文件【效果类似百度文库】(转载)

    转载地址:http://www.cnblogs.com/sword-successful/p/4031823.html 引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前 ...

  5. Java实现word文档在线预览,读取office文件

    想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...

  6. 浏览器在线预览pdf、txt、office文件

    //使用文件预览的原因是:TMD微信浏览器屏蔽掉文件下载链接,只好折中使用文件在线预览功能//要点:1.office文件用微软的插件打开 http://view.officeapps.live.com ...

  7. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  8. js调用本地office打开服务器的office文件预览

    本来是想做成直接在网页上在线预览office文件的,但是找了好多,要不是收费,要不就是要调用别人的API不安全,所以纠结了好久还是用调用本地的office预览office文件. 废话不多说,那么怎么调 ...

  9. 使用FlexPaper实现office文件的预览(C#版)

    需求很简单,用户上传office文件(word.excel.ppt)后,可以预览上传的这些文件.搜索的相关的资料后.整理如下: Step1.用户上传office文件. Step2.把Office文件转 ...

随机推荐

  1. php异步请求(可以做伪线程)

    $fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30); stream_set_blocking($fp,0);     ...

  2. Avoiding “will create implicit index” NOTICE

    执行PgSql避免 notice 信息,执行之前加入以下语句调整报错级别即可: SET CLIENT_MIN_MESSAGES = ‘WARNING’;

  3. 【 java版坦克大战--事件处理】 坦克动起来了

    折腾了这么久,坦克总算能动了.只贴代码编辑不给上首页,花了半个小时的时间写了n多注释. 再顺便把绘图的原理发在这里: 绘图原理 Component类提供了两个和绘图有关的重要方法: ①   paint ...

  4. python【第二十一篇】Django模板继承、分页、cookie验证

    1.模板继承 母版master.html {% block title %}{% endblock %}2 {% block table-cont %}{% endblock %} 子板 {% ext ...

  5. Github readme语法-- markdown

    README 该文件用来测试和展示书写README的各种markdown语法.GitHub的markdown语法在标准的markdown语法基础上做了扩充,称之为GitHub Flavored Mar ...

  6. CocoaPods安装和使用及问题:Setting up CocoaPods master repo-b

    目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaPods? 场景1:利用CocoaPods,在项目中导入AFNetworking类库 场景2:如何正确编译运行一 ...

  7. log.isDebugEnabled()的使用

    转自: http://huangxx.iteye.com/blog/190693 在使用log4j,common-log这样的log框架时,发现很多代码中这样写 if   (log.isDebugEn ...

  8. 【Xamarin挖墙脚系列:多窗口之间的导航】

    原文:[Xamarin挖墙脚系列:多窗口之间的导航] 在Android中:Intent对象,通知松散耦合的Activity等组件 在IOS中:Segue对象连接视图 <button opaque ...

  9. 同一個Loader對象傳入不同參數時,从数据库中查询的結果每次都一樣

    發現問題: LoaderManager().initLoader()方法調用時會根據第一個參數ID去判斷是否已經存在一個Loader加載器,如果存在則複 用,不存在則建一個新的加載器.由於我第一次已經 ...

  10. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...