asp.net txt2html,直接在线预览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文件的全部内容,希望大家喜欢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值