• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > 直接在线预览Word、Excel、TXT文件之ASP.NET

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

作者:秋荷雨翔 字体:[增加 减小] 来源:互联网 时间:2017-05-28

秋荷雨翔 通过本文主要向大家介绍了asp预览,asp文件怎么预览,asp怎么预览,asp网页预览,asp转换成word等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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



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
  }
}
</div>

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28C#使用开源驱动连接操作MySQL数据库
  • 2017-05-28C#判断字符串是否存在字母及字符串中字符的替换实例
  • 2017-05-28C#设置软件开机自动运行的方法(修改注册表)
  • 2017-05-28c#实现pdf的另存为功能
  • 2017-05-28C#将html table 导出成excel实例
  • 2017-05-28C#.net中的类型转换详细介绍
  • 2017-05-28C#连接操作 MySQL 数据库实例(使用官方驱动)
  • 2017-05-28c#图片上传和显示的实现方法
  • 2017-05-28C#进度轴控件分享
  • 2017-05-28C#中 城市线路图的纯算法以及附带求极权值

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#模拟链表数据结构的实例解析
    • C#创建数据库及导入sql脚本的方法
    • 解析C#面向对象编程中方法(method)的使用
    • C# 如何判断两个文件内容是否相同的方法
    • C#使用Windows Service的简单教程(创建、安装、卸载、调试)
    • C#中事件的动态调用实现方法
    • C#索引属性用法实例分析
    • C#从数据库读取数据到DataSet并保存到xml文件的方法
    • c#中的常用ToString()方法总结
    • C#接口(Interface)用法分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有