直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
具體實(shí)現(xiàn)過(guò)程不多說(shuō)了,直接貼代碼了。
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>
/// 在線預(yù)覽Office文件
/// </summary>
public class OfficeViewController : Controller
{
#region Index頁(yè)面
/// <summary>
/// Index頁(yè)面
/// </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 預(yù)覽Excel
/// <summary>
/// 預(yù)覽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 預(yù)覽Word
/// <summary>
/// 預(yù)覽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 預(yù)覽Txt
/// <summary>
/// 預(yù)覽Txt
/// </summary>
public string PreviewTxt(string physicalPath, string url)
{
return Server.UrlDecode(url);
}
#endregion
#region 預(yù)覽Pdf
/// <summary>
/// 預(yù)覽Pdf
/// </summary>
public string PreviewPdf(string physicalPath, string url)
{
return Server.UrlDecode(url);
}
#endregion
}
}
以上就是針對(duì)直接在線預(yù)覽word、excel、text、pdf文件的全部?jī)?nèi)容,希望大家喜歡。
- ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
- asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
- ASP.NET操作EXCEL的總結(jié)篇
- .NET讀取Excel文件的三種方法的區(qū)別
- ASP.NET(C#)讀取Excel的文件內(nèi)容
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的方法
- ASP.NET導(dǎo)出Excel打開時(shí)提示:與文件擴(kuò)展名指定文件不一致解決方法
- .Net Core使用OpenXML導(dǎo)出、導(dǎo)入Excel
相關(guān)文章
C#創(chuàng)建及訪問(wèn)網(wǎng)絡(luò)硬盤的實(shí)現(xiàn)
本文主要介紹了C#創(chuàng)建及訪問(wèn)網(wǎng)絡(luò)硬盤的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
C#設(shè)置自定義文件圖標(biāo)實(shí)現(xiàn)雙擊啟動(dòng)(修改注冊(cè)表)
這篇文章介紹的是利用C#設(shè)置自定義文件圖標(biāo),然后實(shí)現(xiàn)雙擊啟動(dòng)的功能,文章給出了示例代碼,介紹的很詳細(xì),有需要的可以參考借鑒。2016-08-08
C#事務(wù)處理(Execute Transaction)實(shí)例解析
這篇文章主要介紹了C#事務(wù)處理(Execute Transaction)實(shí)例解析,對(duì)于理解和學(xué)習(xí)事務(wù)處理有一定的幫助,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
C#?中?List?與?List?多層嵌套不改變?cè)档膶?shí)現(xiàn)方法(深度復(fù)制)
這篇文章主要介紹了C#?中?List?與?List?多層嵌套不改變?cè)档膶?shí)現(xiàn)方法,使用?BinaryFormatter?將原始?List?序列化為字節(jié)流,然后再反序列化得到新的?List,實(shí)現(xiàn)了深度復(fù)制,需要的朋友可以參考下2024-03-03

