当前位置: 代码迷 >> Office >> 【项目实战】-Office文件预览
  详细解决方案

【项目实战】-Office文件预览

热度:304   发布时间:2016-05-05 05:37:33.0
【项目实战】--Office文件预览
上篇文章已经介绍了office文件的上传,这次来简单介绍一下Office文件的预览。实现如下:**1、工具准备**Pdf2swf:用于把PDF格式文件转换为swf文件Flexpaper:flash播放器,可播放一帧一帧的flash**2、添加引用**在项目中添加引用:选择COM里面Microsoft Office 15.0 object Library和Microsoft Word 15.0 object Library。引用里就会自动添加Microsoft.Office.Interop.Word引用注意*****报错*****类型“Microsoft.Office.Interop.Word.ApplicationClass"未定义构造函数。无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。******解决******解决方案资源管理器 -> 引用 -> "Microsoft.Office.Interop.Word" -> 右键选择属性 -> 嵌入互操作类型的值改为"false"即可。**3、实现代码**
  • View:
<script src="../../Scripts/MyScript/flexpaper_flash.js"></script><div id="dlgPreview" closed="true" class="easyui-dialog" style="width: 750px; height: 750px;padding-top: 30px;padding-bottom: 30px;padding-left: 25px;"         buttons="#dlg-buttons" title ="添加">        <form  id="PreviewHomeWork" method="post" >   @*method="post" action="/TeaQueryHomework/AddHomeWork" *@            @*添加按钮弹出框*@            <a id="HomeworkBack" class="easyui-linkbutton l-btn"  data-options="plain:true,iconCls:'icon-back'" style="margin-left :80px;" href="#" onclick="HomeworkBack()">                    <span >返回</span>             </a>             <div style="position: absolute; left: 60px; top: 100px;">            <a id="viewerPlaceHolder" style="width: 650px; height: 600px; display: block;"></a>            </div>        </form>  </div> 
  • Controller:

    WordToPDF:

/// <summary>/// 将office文件转化为pdf文件,文件名称不变/// </summary>/// <param name="pdf2swfPath">pdf2swfPath工具所在路径</param>/// <param name="OfficePath">office存储路径</param>/// <param name="OfficeName">office文件名称</param>/// <param name="destPath">pdf存储路径</param>/// <returns>返回生成pdf的文件名,无效则返回空</returns>private string OfficeToPdf(string OfficePath, string OfficeName, string destPath){    string fullPathName = OfficePath + OfficeName;//包含 路径 的全称    string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(OfficeName);//不包含路径,不包含扩展名    string extendName = System.IO.Path.GetExtension(OfficeName).ToLower();//文件扩展名    string saveName = destPath + fileNameWithoutEx + ".pdf";    string returnValue = fileNameWithoutEx + ".pdf";    Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;    Microsoft.Office.Interop.Word.ApplicationClass application = null;    Microsoft.Office.Interop.Word.Document document = null;    try    {        application = new Microsoft.Office.Interop.Word.ApplicationClass();        application.Visible = false;        document = application.Documents.Open(fullPathName);        document.SaveAs();        document.ExportAsFixedFormat(saveName, exportFormat);        }    catch (Exception e)    {        Console.WriteLine(e.Message);    }    finally    {        if (document != null)        {            document.Close();            document = null;        }        if (application != null)        {            application.Quit();            application = null;        }        GC.Collect();        GC.WaitForPendingFinalizers();        GC.Collect();        GC.WaitForPendingFinalizers();    }    return returnValue;}
PDFToswf
/// <summary>/// 将pdf文件转化为swf文件,文件名称不变/// </summary>/// <param name="pdf2swfPath">pdf2swfPath工具所在路径</param>/// <param name="PdfPath">pdf存储路径</param>/// <param name="PdfName">pdf文件名称</param>/// <param name="destPath">swf存储路径</param>/// <returns></returns>private string PdfToSwf(string pdf2swfPath, string PdfPath, string PdfName, string destPath){    string fullPathName = PdfPath + PdfName;//包含 路径 的全称    string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(PdfName);//不包含路径,不包含扩展名    string extendName = System.IO.Path.GetExtension(PdfName).ToLower();//文件扩展名    string saveName = destPath + fileNameWithoutEx + ".swf";    string returnValue = fileNameWithoutEx + ".swf"; ;    if (extendName != ".pdf")    {        returnValue = "";    }    else    {        Process pc = new Process();        string cmd = pdf2swfPath;        string args = " -t " + fullPathName + " -s flashversion=9 -o " + saveName;        try        {            ProcessStartInfo psi = new ProcessStartInfo(cmd, args);            psi.WindowStyle = ProcessWindowStyle.Hidden;            pc.StartInfo = psi;            pc.Start();            pc.WaitForExit();        }        catch (Exception ex)        {            throw new Exception(ex.Message);        }        finally        {            pc.Close();            pc.Dispose();        }    }    return returnValue;}
  • JS代码:
var target = "../../Content/TeaFile/SWF/成绩管理.swf";var fp = new FlexPaperViewer(                        '../../FlexPaper/FlexPaperViewer',                        'viewerPlaceHolder', {                            config: {                                //SwfFile: escape($("#file").val()),  //                                SwfFile: escape(target),                                Scale: 0.6,                                ZoomTransition: 'easeOut',                                ZoomTime: 0.5,                                ZoomInterval: 0.2,                                FitPageOnLoad: true,                                FitWidthOnLoad: false,                                PrintEnabled: true,                                FullScreenAsMaxWindow: false,                                ProgressiveLoading: false,                                MinZoomSize: 0.2,                                MaxZoomSize: 5,                                SearchMatchAll: false,                                InitViewMode: 'Portrait',                                ViewModeToolsVisible: true,                                ZoomToolsVisible: true,                                NavToolsVisible: true,                                CursorToolsVisible: true,                                SearchToolsVisible: true,                                localeChain: 'en_US'                            }                        });

预览效果图:

这里写图片描述

至此为止,Office的上传和预览就完成了。如果发现更好的方法,欢迎大家交流、批评、指正。

1楼u0103756634小时前
学习了,加油!
  相关解决方案