ASP.NET MVC在线预览Excel、Word、TXT、PDF文件

代码:

using System;
 System.Collections.Generic;
 System.Linq;
 System.Web;
 System.Web.Mvc;
 Microsoft.Office.Interop.Excel;
 System.Diagnostics;
 System.IO;
 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":
                .xlsx:
                    htmlUrl = PreviewExcel(physicalPath,url);
                    break;
                .doc.docx PreviewWord(physicalPath,1)">.txt PreviewTxt(physicalPath,1)">.pdf PreviewPdf(physicalPath,1)">.jpg.jpeg.bmp.gif.png PreviewImg(physicalPath,1)">default PreviewOther(physicalPath,1)">;
            }

            return Redirect(Url.Content(htmlUrl));
        }
        #endregion

        #region 预览Excel
         预览Excel
        </summary>
        string PreviewExcel(string physicalPath, url)
        {
            Microsoft.Office.Interop.Excel.Application application = null;
            Microsoft.Office.Interop.Excel.Workbook workbook = ;
            application = new Microsoft.Office.Interop.Excel.Application();
            object missing = Type.Missing;
            object trueObject = true;
            application.Visible = false;
            application.DisplayAlerts = ;
            workbook = application.Workbooks.Open(physicalPath,missing,trueObject,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,XlSaveAsAccessMode.xlNoChange,missing);
            workbook.Close();
            application.Quit();
            return Path.GetDirectoryName(Server.UrlDecode(url)) +  htmlName;
        }
        #region 预览Word
         预览Word
        string PreviewWord( url)
        {
            Microsoft.Office.Interop.Word._Application application = ;
            Microsoft.Office.Interop.Word._Document doc =  Microsoft.Office.Interop.Word.Application();
            ;
            application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            doc = application.Documents.Open(physicalPath,1)"> Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
             htmlName;
            doc.SaveAs(outputFile,missing);
            doc.Close();
            application.Quit();
            #region 预览Txt
         预览Txt
        string PreviewTxt( Server.UrlDecode(url);
        }
        #region 预览Pdf
         预览Pdf
        string PreviewPdf(#region 预览图片
         预览图片
        string PreviewImg(#region 预览其他文件
         预览其他文件
        string PreviewOther(#endregion

    }
}
View Code

 

相关文章

引言 本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个...
引言: 多线程编程/异步编程非常复杂,有很多概念和工具需要...
一. 宏观概念 ASP.NET Core Middleware是在应用程序处理管道...
背景 在.Net和C#中运行异步代码相当简单,因为我们有时候需要...
HTTP基本认证 在HTTP中,HTTP基本认证(Basic Authenticatio...
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续...