欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

直接在线预览Word、Excel、TXT文件之ASP.NET

程序员文章站 2023-11-16 23:21:22
具体实现过程不多说了,直接贴代码了。 using system; using system.collections.generic; using sy...

具体实现过程不多说了,直接贴代码了。



using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using microsoft.office.interop.excel;
using system.diagnostics;
using system.io;
using microsoft.office.interop.word;
namespace suya.web.apps.areas.pmp.controllers
{
  /// <summary>
  /// 在线预览office文件
  /// </summary>
  public class officeviewcontroller : controller
  {
    #region index页面
    /// <summary>
    /// index页面
    /// </summary>
    /// <param name="url">例:/uploads/......xxx.xls</param>
    public actionresult index(string url)
    {
      string physicalpath = server.mappath(server.urldecode(url));
      string extension = path.getextension(physicalpath);
      string htmlurl = "";
      switch (extension.tolower())
      {
        case ".xls":
        case ".xlsx":
          htmlurl = previewexcel(physicalpath, url);
          break;
        case ".doc":
        case ".docx":
          htmlurl = previewword(physicalpath, url);
          break;
        case ".txt":
          htmlurl = previewtxt(physicalpath, url);
          break;
        case ".pdf":
          htmlurl = previewpdf(physicalpath, url);
          break;
      }
      return redirect(url.content(htmlurl));
    }
    #endregion
    #region 预览excel
    /// <summary>
    /// 预览excel
    /// </summary>
    public string previewexcel(string physicalpath, string url)
    {
      microsoft.office.interop.excel.application application = null;
      microsoft.office.interop.excel.workbook workbook = null;
      application = new microsoft.office.interop.excel.application();
      object missing = type.missing;
      object trueobject = true;
      application.visible = false;
      application.displayalerts = false;
      workbook = application.workbooks.open(physicalpath, missing, trueobject, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing, missing);
      //save excel to html
      object format = microsoft.office.interop.excel.xlfileformat.xlhtml;
      string htmlname = path.getfilenamewithoutextension(physicalpath) + ".html";
      string outputfile = path.getdirectoryname(physicalpath) + "\\" + htmlname;
      workbook.saveas(outputfile, format, missing, missing, missing,
               missing, xlsaveasaccessmode.xlnochange, missing,
               missing, missing, missing, missing);
      workbook.close();
      application.quit();
      return path.getdirectoryname(server.urldecode(url)) + "\\" + htmlname;
    }
    #endregion
    #region 预览word
    /// <summary>
    /// 预览word
    /// </summary>
    public string previewword(string physicalpath, string url)
    {
      microsoft.office.interop.word._application application = null;
      microsoft.office.interop.word._document doc = null;
      application = new microsoft.office.interop.word.application();
      object missing = type.missing;
      object trueobject = true;
      application.visible = false;
      application.displayalerts = wdalertlevel.wdalertsnone;
      doc = application.documents.open(physicalpath, missing, trueobject, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
      //save excel to html
      object format = microsoft.office.interop.word.wdsaveformat.wdformathtml;
      string htmlname = path.getfilenamewithoutextension(physicalpath) + ".html";
      string outputfile = path.getdirectoryname(physicalpath) + "\\" + htmlname;
      doc.saveas(outputfile, format, missing, missing, missing,
               missing, xlsaveasaccessmode.xlnochange, missing,
               missing, missing, missing, missing);
      doc.close();
      application.quit();
      return path.getdirectoryname(server.urldecode(url)) + "\\" + htmlname;
    }
    #endregion
    #region 预览txt
    /// <summary>
    /// 预览txt
    /// </summary>
    public string previewtxt(string physicalpath, string url)
    {
      return server.urldecode(url);
    }
    #endregion
    #region 预览pdf
    /// <summary>
    /// 预览pdf
    /// </summary>
    public string previewpdf(string physicalpath, string url)
    {
      return server.urldecode(url);
    }
    #endregion
  }
}

以上就是针对直接在线预览word、excel、text、pdf文件的全部内容,希望大家喜欢。