栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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



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
{
  /// 
  /// 在线预览Office文件
  /// 
  public class OfficeViewController : Controller
  {
    #region Index页面
    /// 
    /// Index页面
    /// 
    /// 例:/uploads/......XXX.xls
    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
    /// 
    /// 预览Excel
    /// 
    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
    /// 
    /// 预览Word
    /// 
    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
    /// 
    /// 预览Txt
    /// 
    public string PreviewTxt(string physicalPath, string url)
    {
      return Server.UrlDecode(url);
    }
    #endregion
    #region 预览Pdf
    /// 
    /// 预览Pdf
    /// 
    public string PreviewPdf(string physicalPath, string url)
    {
      return Server.UrlDecode(url);
    }
    #endregion
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号