zoukankan      html  css  js  c++  java
  • Web里面展现Office、PDF文档

    一直在想基于Web如何更完美、更经济、更便捷的展现Office、PDF文档。

    从其他一个系统看到的,先保存下来以备后用。谢谢原创。

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <link rel="stylesheet" rev="stylesheet" href="/style/public.css?v=V6_6_18_17498" type="text/css" media="all" />
      <link rel="stylesheet" rev="stylesheet" href="/style/skin1.css?v=V6_6_18_17498" type="text/css" media="all" />
      <script language="javascript" src="../js/jquery-1.3.2.min.js?v=V6_6_18_17498" type=""></script>
       
      <title>在线预览 - 111111111111.xlsx</title>
      <link href="http://www.cnblogs.com/style/viewstl.css?v=V6_6_18_17498" rel="stylesheet" type="text/css" />
       
      <script type="text/javascript">
          function resize() {
          if(!document.getElementById("divContent")){
          return false;
          }
          var win = document.documentElement.clientHeight;
          var hdr = document.getElementById("divHeader").clientHeight;
          var ctn = 0;
          if(win - hdr > 0) {
          ctn = win - hdr;
          }
          document.getElementById("divContent").style.height = ctn + "px";
          }
       
          window.onresize = resize;
       
      //2012-4-11 zny IE6/7 最大化窗口后高度自适应
      $(document).ready(function(){
      //调用函数
      var pagestyle = function() {
      var iframe = $("#FM_Content");
      var h = $(window).height() - iframe.offset().top;
      iframe.height(h);
      }
      //注册加载事件
      $("#FM_Content").load(pagestyle);
      //注册窗体改变大小事件
      $(window).resize(pagestyle);
      });
       
          function loaded() {
          resize();
          }
        function show() {
        document.getElementById("loading_remainder").style.display="none";
        }
      function showView()
      {
      if($("#PIC_Content").length>0)
      {
        $(".operate").hide();
        $("#name").hide();
        if (!document.all){//Firefox   
          window.print();
          window.setTimeout("$('.operate').show();$('#name').show();",2500);return;
        }
      window.print();
      $("#name").show();
      $(".operate").show();
      }
      else
      {
      frames["FM_Content"].frSheet.focus();
      frames["FM_Content"].frSheet.print();
      }
     
      </script>
       
      </head>
       
      <body onload="loaded()">
       
      <div id="divPage">
      <!--头部-->
      <div class="header1" id="divHeader">
        <div class="operate">
             <div style="" class="btn_bg"><a oncontextmenu="this.href='download.php?ent_id=2&amp;mailbox=INBOX&amp;passed_id=4010&amp;file_id=&amp;absolute_dl=true&amp;&amp;right_click=1';return true;" onclick="this.href='download.php?ent_id=2&amp;mailbox=INBOX&amp;passed_id=4010&amp;file_id=&amp;absolute_dl=true';return true;" href="download.php?"><span><img src="http://images.cnblogs.com/blank.gif" class="ico-xiazai"/>下载</span></a></div>
             <div style="" class="btn_bg"><a href="####" onclick="showView();"><span><img src="http://images.cnblogs.com/blank.gif" class="ico-daying"/>打印</span></a></div>
             <div class="btn_bg"><a href="####" onclick="window.close(); "><span><img src="http://images.cnblogs.com/blank.gif" class="ico-close"/>关闭</span></a></div>
        </div>
      <table border="0" cellspacing="0" cellpadding="0" class="pathz_w"><tr><td>
          <div class="pathz" id="name">文件名称:11111111.xlsx<span>*部分格式或图片可能无法预览,请下载附件查看</span></div>
          </td></tr></table> 
          </div>
       
      <div id="loading_remainder" style="text-align: center;">
      <br/>
      <br/>
      正在加载,请稍候 ...
      </div>
      <div class="content2" id="divContent" >
      <iframe name="FM_Content" id="FM_Content" width="100%" height="10" frameborder="0" src="http://www.cnblogs.com/onlineview1/1111111/2.htm" onload="show()"></iframe>
      </div>
       
       
      </div>
      </body>
      </html>
     

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

      <html xmlns="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:x="urn:schemas-microsoft-com:office:excel">
        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <meta Name="Excel Workbook Frameset">
          <meta Name="ProgId" content="Excel.Sheet">
          <title>2</title>
          <!--[if gte mso 9]>
            <xml>
              <o:DocumentProperties>
                <o:TotalTime>0</o:TotalTime>
                <o:Created>2006-09-13T19:21:51Z</o:Created>
                <o:LastSaved>2012-12-12T14:02:45Z</o:LastSaved>
              </o:DocumentProperties>
              <o:OfficeDocumentSettings>
                <o:Colors>
                  <o:Color>
                    <o:Index>0</o:Index>
                    <o:RGB>#000000</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>1</o:Index>
                    <o:RGB>#FFFFFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>2</o:Index>
                    <o:RGB>#FF0000</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>3</o:Index>
                    <o:RGB>#00FF00</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>4</o:Index>
                    <o:RGB>#0000FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>5</o:Index>
                    <o:RGB>#FFFF00</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>6</o:Index>
                    <o:RGB>#FF00FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>7</o:Index>
                    <o:RGB>#00FFFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>8</o:Index>
                    <o:RGB>#800000</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>9</o:Index>
                    <o:RGB>#008000</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>10</o:Index>
                    <o:RGB>#000080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>11</o:Index>
                    <o:RGB>#808000</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>12</o:Index>
                    <o:RGB>#800080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>13</o:Index>
                    <o:RGB>#008080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>14</o:Index>
                    <o:RGB>#C0C0C0</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>15</o:Index>
                    <o:RGB>#808080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>16</o:Index>
                    <o:RGB>#9999FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>17</o:Index>
                    <o:RGB>#993366</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>18</o:Index>
                    <o:RGB>#FFFFCC</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>19</o:Index>
                    <o:RGB>#CCFFFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>20</o:Index>
                    <o:RGB>#660066</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>21</o:Index>
                    <o:RGB>#FF8080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>22</o:Index>
                    <o:RGB>#0066CC</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>23</o:Index>
                    <o:RGB>#CCCCFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>24</o:Index>
                    <o:RGB>#000080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>25</o:Index>
                    <o:RGB>#FF00FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>26</o:Index>
                    <o:RGB>#FFFF00</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>27</o:Index>
                    <o:RGB>#00FFFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>28</o:Index>
                    <o:RGB>#800080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>29</o:Index>
                    <o:RGB>#800000</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>30</o:Index>
                    <o:RGB>#008080</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>31</o:Index>
                    <o:RGB>#0000FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>32</o:Index>
                    <o:RGB>#00CCFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>33</o:Index>
                    <o:RGB>#CCFFFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>34</o:Index>
                    <o:RGB>#CCFFCC</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>35</o:Index>
                    <o:RGB>#FFFF99</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>36</o:Index>
                    <o:RGB>#99CCFF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>37</o:Index>
                    <o:RGB>#FF99CC</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>38</o:Index>
                    <o:RGB>#CC99FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>39</o:Index>
                    <o:RGB>#FFCC99</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>40</o:Index>
                    <o:RGB>#3366FF</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>41</o:Index>
                    <o:RGB>#33CCCC</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>42</o:Index>
                    <o:RGB>#99CC00</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>43</o:Index>
                    <o:RGB>#FFCC00</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>44</o:Index>
                    <o:RGB>#FF9900</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>45</o:Index>
                    <o:RGB>#FF6600</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>46</o:Index>
                    <o:RGB>#666699</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>47</o:Index>
                    <o:RGB>#969696</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>48</o:Index>
                    <o:RGB>#003366</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>49</o:Index>
                    <o:RGB>#339966</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>50</o:Index>
                    <o:RGB>#003300</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>51</o:Index>
                    <o:RGB>#333300</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>52</o:Index>
                    <o:RGB>#993300</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>53</o:Index>
                    <o:RGB>#75923C</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>54</o:Index>
                    <o:RGB>#DBE5F1</o:RGB>
                  </o:Color>
                  <o:Color>
                    <o:Index>55</o:Index>
                    <o:RGB>#EAF1DD</o:RGB>
                  </o:Color>
                </o:Colors>
              </o:OfficeDocumentSettings>
            </xml>
          <![endif]-->
          <link rel="File-List" href="./2.files/filelist.xml">
          <link rel="OLE-Object-Data" href="./2.files/oledata.mso">
          <link id="shLink" href="./2.files/Sheet1.htm">
          <link id="shLink" href="./2.files/Sheet2.htm">
          <link id="shLink" href="./2.files/Sheet3.htm">
          <link id="shLink" href="./2.files/Sheet4.htm">
          <!--[if !mso]>
            <STYLE>V\:* {behavior:url(#default#VML);}O\:* {behavior:url(#default#VML);}W\:* {behavior:url(#default#VML);}X\:* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);}</STYLE>
          <![endif]-->
          <!--[if gte mso 9]>
            <xml>
              <x:ExcelWorkbook>
                <x:ExcelWorksheets>
                  <x:ExcelWorksheet>
                    <x:Name>222</x:Name>
                    <x:WorksheetSource HRef="./2.files/Sheet1.htm"></x:WorksheetSource>
                  </x:ExcelWorksheet>
                  <x:ExcelWorksheet>
                    <x:Name>3333</x:Name>
                    <x:WorksheetSource HRef="./2.files/Sheet2.htm"></x:WorksheetSource>
                  </x:ExcelWorksheet>
                  <x:ExcelWorksheet>
                    <x:Name>44444</x:Name>
                    <x:WorksheetSource HRef="./2.files/Sheet3.htm"></x:WorksheetSource>
                  </x:ExcelWorksheet>
                  <x:ExcelWorksheet>
                    <x:Name>55555</x:Name>
                    <x:WorksheetSource HRef="./2.files/Sheet4.htm"></x:WorksheetSource>
                  </x:ExcelWorksheet>
                </x:ExcelWorksheets>
                <x:StyleSheet HRef="./2.files/stylesheet.css"></x:StyleSheet>
                <x:WindowHeight>11633</x:WindowHeight>
                <x:WindowWidth>19191</x:WindowWidth>
                <x:WindowTopX>0</x:WindowTopX>
                <x:WindowTopY>77</x:WindowTopY>
                <x:DonotCalcuLateBeforeSave></x:DonotCalcuLateBeforeSave>
              </x:ExcelWorkbook>
            </xml>
          <![endif]-->
        </head>
        <frameset rows="*,38" border="0" width="0" frameborder="no" framespacing="0">
          <frame src="2.files/Sheet1.htm" Name="frSheet"></frame>
          <frame src="2.files/tabscript.htm" Name="frTabs" marginwidth="0" marginheight="0"></frame>
        </frameset>
        <body></body>
      </html>
     

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    using System; using System.Configuration; using System.IO; using Microsoft.Office.Interop.Excel; using System.Diagnostics; using Microsoft.Office.Interop.PowerPoint; using Microsoft.Office;

    namespace e {     public class FileHtml     {         public static string FileToHtml(string filePath)         {             string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();             string strss = "|doc|docx|";             string strss2 = "|xls|xlsx|";             string strss3 = "|ppt|pptx|";             if (strss.IndexOf("|" + fileExtension.Substring(1) + "|") > -1)             {                 return wordToHtml(filePath);             }             else if (strss2.IndexOf("|" + fileExtension.Substring(1) + "|") > -1)             {                 return ExcelToHtml(filePath);             }             else if (strss3.IndexOf("|"+fileExtension.Substring(1)+"|") > -1)             {                 return PPTToHtlm(filePath);             }             else             { return ""; }

            }          /// <summary>          ///          /// </summary>          /// <param name="filePath">上传后文件的路径</param>          /// <returns></returns>         public static string wordToHtml(string filePath)         {             try             {                 Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();                 Type wordType = word.GetType();                 Microsoft.Office.Interop.Word.Documents docs = word.Documents;

                    // 打开文件                 Type docsType = docs.GetType();

                    object fileName = filePath;

                    Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",                 System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

                    // 转换格式,另存为html                 Type docType = doc.GetType();

                    string filename = System.DateTime.Now.ToString() + " " + System.DateTime.Now.Millisecond.ToString() + ".html";

                    string fpath = "html/" + System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Day + "/";

                    string path = (ConfigurationSettings.AppSettings["txtpath"] + fpath);

                    Directory.CreateDirectory(path);

                    //被转换的html文档保存的位置

                    object saveFileName = path + filename.Replace(":", "-");

                    /*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:                 * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,                 * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});                 * 其它格式:                 * wdFormatHTML                 * wdFormatDocument                 * wdFormatDOSText                 * wdFormatDOSTextLineBreaks                 * wdFormatEncodedText                 * wdFormatRTF                 * wdFormatTemplate                 * wdFormatText                 * wdFormatTextLineBreaks                 * wdFormatUnicodeText                 */                 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,                 null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

                    // 退出 Word                 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);                 //返回文件名                 return (fpath + filename.Replace(":", "-"));             }             catch (Exception ex)             {                 return ex.ToString();             }             //finally             //{             //    Process[] myProcesses = Process.GetProcessesByName("WINWORD");             //    foreach (Process myProcess in myProcesses)             //    {             //        myProcess.Kill();             //    }

                //}

            }

            /// <summary>         /// 出现错误,解决不了         /// </summary>         /// <param name="filePath">上传后文件的路径</param>         /// <returns></returns>         public static string PPTToHtlm(string filePath)         {             try             {          

                    // 转换格式,另存为html                 string filename = System.DateTime.Now.ToString() + " " + System.DateTime.Now.Millisecond.ToString() + ".html"; string fpath = "html/" + System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Day + "/";

                    string path = (ConfigurationSettings.AppSettings["txtpath"] + fpath);

                    Directory.CreateDirectory(path);

                    //被转换的html文档保存的位置

                    string saveFileName = path + filename.Replace(":", "-");

                    Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();                 Microsoft.Office.Core.MsoTriState m1 = new MsoTriState();                 Microsoft.Office.Core.MsoTriState m2 = new MsoTriState();                 Microsoft.Office.Core.MsoTriState m3 = new MsoTriState();                 Microsoft.Office.Interop.PowerPoint.Presentation pp = ppt.Presentations.Open(filePath, m1, m2, m3);                 pp.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);                 pp.Close();            

                    //返回文件名                 return (fpath + filename.Replace(":", "-"));             }             catch (Exception ex)             {                 return ex.ToString();             }             //finally             //{             //    Process[] myProcesses = Process.GetProcessesByName("POWERPNT");             //    foreach (Process myProcess in myProcesses)             //    {             //        myProcess.Kill();             //    }            
                //}         }         /// <summary>         ///
            /// </summary>         /// <param name="filePath">上传后文件的路径</param>         /// <returns></returns>         public static string ExcelToHtml(string filepath)         {             try             {                 // 转换格式,另存为html                 string filename = System.DateTime.Now.ToString() + " " + System.DateTime.Now.Millisecond.ToString() + ".html";                 string fpath = "html/" + System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Day + "/";

                    string path = (ConfigurationSettings.AppSettings["txtpath"] + fpath);

                    Directory.CreateDirectory(path);

                    //被转换的html文档保存的位置

                    object saveFileName = path + filename.Replace(":", "-");

                    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();                 app.Visible = false;                 object o = System.Reflection.Missing.Value;                 // object xlsFile=xlsPath;
                    // _Workbook xls=app.Workbooks.Open(xlsPath,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
                    _Workbook xls = app.Workbooks.Open(filepath, o, true, o, o, o, o, o, o, o, o, o, o, o, o);                 object fileName = saveFileName;                 object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;//Html

                    // xls.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);                 xls.SaveAs(fileName, format, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o);                 app.Quit();

                    return fpath + filename.Replace(":", "-");             }             catch (Exception ex)             {                 return ex.ToString();             }             //finally             //{             //    Process[] myProcesses = Process.GetProcessesByName("EXCEL");             //    foreach (Process myProcess in myProcesses)             //    {             //        myProcess.Kill();             //    }             //}

                            }            }            
        }

    博客地址: http://www.cnblogs.com/defineconst/
    博客版权: 本文以学习、研究和分享为主,欢迎转载和各类爬虫,但必须在文章页面明显位置给出原文链接。 如果文中有不妥或者错误的地方还望高手的您指出,以免误人子弟。如果您有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
  • 相关阅读:
    Docker容器启动时初始化Mysql数据库
    使用Buildpacks高效构建Docker镜像
    Mybatis 强大的结果集映射器resultMap
    Java 集合排序策略接口 Comparator
    Spring MVC 函数式编程进阶
    换一种方式编写 Spring MVC 接口
    【asp.net core 系列】6 实战之 一个项目的完整结构
    【asp.net core 系列】5 布局页和静态资源
    【asp.net core 系列】4. 更高更强的路由
    【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的创建
  • 原文地址:https://www.cnblogs.com/defineconst/p/2878103.html
Copyright © 2011-2022 走看看